【发布时间】:2014-08-17 11:35:14
【问题描述】:
我有一个多对多的关系:
customer、product、customer_product 带有额外的列 quantity
我想用hibernate映射它们我的问题是:
如果我只是像两个 一对多 一样正常映射它们会丢失什么?customer_product 将只有一个主键 (id_customer_product) 而不是复合主键密钥 (id_product + id_customer)
customer实体:
@Entity
@Table(name = "customer", catalog = "XX")
public class Customer implements java.io.Serializable {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
private Set customer_product = new HashSet<Customer_Product>(0);
}
product实体:
@Entity
@Table(name = "product", catalog = "XX")
public class Customer implements java.io.Serializable {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "product")
private Set customer_product = new HashSet<customer_product>(0);
}
customer_product实体:
@Entity
@Table(name = "customer_product", catalog = "XX")
public class Customer_Product implements java.io.Serializable {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "ID_customer_product")
private Integer ID_customer_product;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID_customer")
private Customer customer;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID_product")
private Product product;
}
或者我必须像这样answer 并创建一个复合主键。
【问题讨论】:
标签: sql database hibernate jpa