简体   繁体   English

休眠:主键由外键构成

[英]Hibernate: Primary key made of foreign keys

So I have "OrderItem" class. 所以我有“ OrderItem”类。 Its primary key its suppose to be made of "productID" and "orderID" (both FK reference from another entities). 它的主键假定由“ productID”和“ orderID”(来自另一个实体的FK引用)组成。 How can I set that up using annotations? 如何使用注释进行设置?

Thanks in advance!!!! 提前致谢!!!!

Table : ordersItems :ordersItems
Attributes : productID (PK-PK) ------------------> Product 属性 :productID(PK-PK)------------------>产品
orderID (PK-PK) ------------------> Order orderID(PK-PK)------------------>订单

Given these entities: 鉴于这些实体:

@Entity
@Table(name="products")
public class Product {

    @Id
    @Column(name="productID")
    private Integer id;
    private String description;
}


@Entity
@Table(name="orders")
public class Order{

    @Id
    private int orderID;

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinColumn(name="orderID")
    private List<OrderItem> items;
}

@Entity
@Table(name="ordersItems")
public class OrderItem{

    @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
    @JoinColumn(name="productID")
    private Product product;
    private int quantity;
    private float price;
}

You will have to make an embeddable domain class that has only the fields in your primary key built of foreign keys. 您将必须创建一个可嵌入的域类,该类仅在主键中由外键构建的字段中。 The annotation is at the class level, @Embeddable, on the foreign key domain class. 注释在外键域类的类级别@Embeddable上。

Here is the docs for EmbeddedId 这是EmbeddedId的文档

http://docs.oracle.com/javaee/6/api/javax/persistence/EmbeddedId.html http://docs.oracle.com/javaee/6/api/javax/persistence/EmbeddedId.html

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM