So I have "OrderItem" class. Its primary key its suppose to be made of "productID" and "orderID" (both FK reference from another entities). How can I set that up using annotations?
Thanks in advance!!!!
Table : ordersItems
Attributes : productID (PK-PK) ------------------> Product
orderID (PK-PK) ------------------> Order
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.
Here is the docs for EmbeddedId
http://docs.oracle.com/javaee/6/api/javax/persistence/EmbeddedId.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.