在联接表JPA 2中映射额外的属性 [英] Mapping Extra Attribute in a Join Table JPA 2
问题描述
我正在尝试通过此链接 http:建立这种关系的模型: //www.javaworld.com/javaworld/jw-01-2008/images/datamodel.gif
I am trying to model this relationship following this link http://www.javaworld.com/javaworld/jw-01-2008/images/datamodel.gif
这是订单和产品之间通常的多对多关系,但我不知道如何在联接表中添加额外的列.
Its the usual Many to Many relationship between Order and Products but I dont know how to add the extra columns in the Join table.
@Entity
@Table(name = "Orders")
public class Order {
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "ORDER_LINES", joinColumns = { @JoinColumn(name = "ORDER_ID") }, inverseJoinColumns = { @JoinColumn(name = "PROD_ID") })
private Set<Product> products;
}
@Entity
@Table(name="PRODUCTS")
public class Product {
@ManyToMany(mappedBy="products")
private Set<Order> orders;
}
如何在JPA 2.0中添加Join Table Extra属性?
How to add Join Table extra attribute in JPA 2.0?
谢谢
推荐答案
在JPA(2.0)中,不存在在关系中具有其他持久属性的概念.这就是为什么与财产的关系实际上是中间实体.
There is no concept of having additional persistent attribute in relation in JPA (2.0). That's why relation with property is actually intermediate entity.
在Order和Product实体中,您都需要与新实体一对多的关系.由于双向关系,新实体将与订单和产品具有多对一关系.
From both Order and Product entities, you need one-to-many relationship to new entity. Because of bidirectional relationship, new entity will have many-to-one relationships to Order and Product.
您需要进行以下操作(仅显示关系,ID和其他映射被删除):
You need to go for something like this (shows only relationships, id's and other mappings stripped away):
@Entity
@Table(name="order_item")
public class OrderItem {
@ManyToOne
private Order order;
@ManyToOne
private Product product;
}
@Entity
public class Order {
@OneToMany (mappedBy = "order")
private Set<OrderItem> orderItems;
}
@Entity
public class Product {
@OneToMany(mappedBy = "product")
private Set<OrderItem> orderItems;
}
这篇关于在联接表JPA 2中映射额外的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!