简体   繁体   English

JPA eclipselink坚持没有主键但有两个外键的实体

[英]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 Sale_id Product_id Product_id
Sale_details销售详情
Saleid_fk Saleid_fk
Productid_fk (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;

what am I missing here?我在这里想念什么?
sale_details 表:

You can just set a primary id as autoincrement and not use it.您可以将主 ID 设置为自动增量而不使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Hibernate实体注释中使用两个外键作为主键 - How to use two foreign keys as primary key on Hibernate entity annotation JPA中的映射键:主键是外键 - Mapping keys in JPA: Primary key is a foreign key JPA 实体没有主键? - JPA entity has no primary key? JPA 2 - 如何使用Spring Data JPA构建具有主键的实体,该主键也是外键? - JPA 2 - How to build entity that has Primary key that is also a foreign key using Spring Data JPA? JPA 实体外键是来自其他实体的主键 - JPA entity foreign key is a primary key from other entity 实体没有定义主键属性:JPA - entity has no primary key attribute defined : JPA JPA如何从单个表的复合主键的两个不同表部分制作复合外键 - JPA how to make composite Foreign Keys from two different table part of composite Primary Key of a single table 向复合主键添加外键,并在JPA Entity类中进行更改 - Add a foreign key to composite primary key and changes in JPA Entity class 使用Eclipselink / JPA,我可以使用与主复合键共享字段的外部复合键吗? - With Eclipselink/JPA, can I have a Foreign Composite Key that shares a field with a Primary Composite Key? 如何在实体中声明两个外键作为主键 - How to declare two foreign keys as primary keys in an entity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM