[英]JPA eclipselink persist entity that has no primary key but two foreign keys
I am very new to java EE and self-learning it, so please be patient with me.我对 java EE 和自学很陌生,所以请耐心等待。 So far I have 3 simple tables in my database:
到目前为止,我的数据库中有 3 个简单的表:
Sales![]() |
Products![]() |
---|---|
Sale_id ![]() |
Product_id ![]() |
Sale_details![]() |
---|
Saleid_fk ![]() |
Productid_fk (fk) ![]() |
Quantity_sold![]() |
How do I persist a new sale_details without creating a sale_details entity.如何在不创建 sale_details 实体的情况下保留新的 sale_details。 Because Java wont let me name sale_details as @Entity without a primary key.
因为 Java 不允许我在没有主键的情况下将 sale_details 命名为 @Entity。 this current code gives me an error stating i need to declare a primary key.
这个当前代码给了我一个错误,说明我需要声明一个主键。 I tried using Idclass based on the answers on stackoverflow but it doesn't do anything.
我尝试根据stackoverflow上的答案使用Idclass,但它没有做任何事情。
@Entity
@Table(name="sale_details")
@IdClass(value = SaleDetails.class)
public class SaleDetails implements Serializable {
@Column(name = "saleid_fk")
private int saleid_fk;
@Column(name = "productid_fk")
private String productid_fk;
@Column(name = "quantity_sold")
private int quantity_sold;
You can just set a primary id as autoincrement and not use it.您可以将主 ID 设置为自动增量而不使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.