gpt4 book ai didi

java - 如何对非 PK id 使用 @TableGenerator 注解

转载 作者:行者123 更新时间:2023-12-02 07:53:41 25 4
gpt4 key购买 nike

这是 JPA 实体架构的摘录



@Entity
@Table(name="customer")
public class Customer implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="customer_id")
private Long id;

@ManyToOne
@JoinColumn(name="company_id",nullable=false)
private Company company;

//...

}

@Entity
@Table(name="company")
public class Company implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="company_id")
private Long id;

//...

}

@Entity
@Table(name="order_header")
public class OrderHeader implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="order_id")
private Long id;


@ManyToOne
@JoinColumn(name="customer_id",nullable=false)
private Customer customer;

private Long internalCompanyOrderId;

//...

}

我需要为每个 Company_id 检索唯一的内部CompanyOrderId 序列。这意味着尽管如此,订单 PK 仍将是“order_id”,我需要每个公司的“内部”order_id。

最佳答案

您的代码示例中没有出现 @TableGenerator。我假设您想使用 @GenerateValue 生成值。

对于 JPA 2.0,根据规范,您不能对不属于主键的字段使用 @GenerateValue:

The GeneratedValue annotation may be applied to a primary key property or field of an entity or mapped superclass in conjunction with the Id annotation. [97]
...
[97] Portable applications should not use the GeneratedValue annotation on other persistent fields or properties.

关于java - 如何对非 PK id 使用 @TableGenerator 注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9914214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com