java - JPA eclipse 两个外键@IdClass 实现错误
问题描述
我对 Java EE 和 JPA/Eclipselink 很陌生,所以请多多包涵。我正在尝试为@Id
没有主键和两个复合键的表创建一个,我尝试通过使用@IdClass
将两个外键标记为复合键来从我在这里阅读的解决方案中应用,但是在编译和部署服务器。
异常描述:无效的复合主键规范。主键类[com.owl.server.objects.SaleDetails]和实体bean类[class com.owl.server.objects.SaleDetails]中的主键字段或属性的名称必须对应,并且它们的类型必须对应。是相同的。此外,请确保您已为 XML 中的相应属性指定 ID 元素和/或实体类的相应字段或属性上的 @Id。有关更多详细信息,请参阅 server.log。
我必须将字段类型更改为相同吗?还是有其他方法?
产品表和对应的实体
@Entity
@Table(name="products")
public class Product implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "product_id", nullable = false)
private String product_id;
// ...
}
销售表及对应实体
@Entity
@Table(name="Sales")
public class Sale implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "sale_id", nullable = false)
private int sale_id;
// ...
}
Sale_details 表和实体(导致错误)
public class SaleDetails implements Serializable {
private int saleid_fk;
private String productid_fk;
private int quantity_sold;
private String prescription;
public SaleDetails() {
}
public SaleDetails(int saleid_fk, String productid_fk, int quantity_sold){
this.saleid_fk = saleid_fk;
this.productid_fk = productid_fk;
this.quantity_sold = quantity_sold;
}
}
sale_details 实体类
@Entity
@IdClass(SaleDetails.class)
@Table(name = "sale_Details")
public class Sale_details {
@Id
private int saleid_fk;
@Id
private String productid_fk;
}
这是完整的堆栈跟踪错误:
[2020-12-24 11:43:50,553] Artifact server:war exploded: java.io.IOException: com.sun.enterprise.admin.remote.RemoteFailureException: Error occurred during deployment: Exception while preparing the app : Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.7.7.payara-p3): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [owlJPA] failed.
Internal Exception: Exception [EclipseLink-7150] (Eclipse Persistence Services - 2.7.7.payara-p3): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Invalid composite primary key specification. The names of the primary key fields or properties in the primary key class [com.owl.server.objects.SaleDetails] and those of the entity bean class [class com.owl.server.objects.Sale_details] must correspond and their types must be the same. Also, ensure that you have specified ID elements for the corresponding attributes in XML and/or an @Id on the corresponding fields or properties of the entity class.. Please see server.log for more details.
解决方案
Idclass 值不应与实体“SaleDetails”的同一类
@Entity
@IdClass(PK.class)
public static class SystemUser{
@Id
private String subsystem;
@Id
private String username;
public PK getId(){
return new PK(subsystem, username);
}
public void setId(PK id){
this.subsystem = id.subsystem;
this.username = id.username;
}
}
public static class PK implements Serializable {
private String subsystem;
private String username;
public PK(String subsystem, String username) {
this.subsystem = subsystem;
this.username = username;
}
}
推荐阅读
- powershell - 在 Powershell 上以编程方式进行身份验证时无法运行 Skype 命令
- c# - 在 Blazor Server 应用程序中实现用户会话变量的最佳做法是什么?
- python - Api 说余额不足,而我有的绰绰有余
- c++ - 我的 ostream 超载有什么问题?
- javascript - 在 HTML div 中打开 URL
- python-3.x - 更新数据时出错 - TypeError: put () got multiple values for argument 'connection'
- reactjs - 使用useEffect在react-native中获取Firebase错误
- angular - 单元测试 ngAfterViewInit
- assembly - VC++编译器在汇编中取模
- python - 保存到 GCP 存储桶时强制覆盖 (pd.to_csv)