Hibernate可以通过*.hbm.xml配置文件能很好地把多对多关系拆分成两个一对多的关系,但Hibernate Annotation的文档中没有说到这个点上来。下面通过实例说明用注解来实现多对多拆分成两个一对多。
下面以商品Product和订单Order之间的多对多关系来说明。
Product的属性包括:
- id
- name
- price
Order的属性包括:
- id
- date(下订单的时间)
为什么要把多对多关系拆分成两个一对多?
因为多对多关系不能保存两个实体之间共有的属性。比如,如何记录订单A中购买的商品B的数量呢?如果以多对多映射就不能实现了。
中间实体----用来记录两个多对多实体之间共有关系
在Product和Order之间的关系中,可以用一个OrderItem实体来表示两者多对多中的众多关系中的一个关系。即Product与OrderItem,Order与OrderItem之间的关系为一对多的关系。
OrderItem的属性包括:
- product(假如当前记录的是商品C)
- order(假如当前记录的是订单D)
- quantity(这里记录的是订单D中商品C的数量)
实例代码(省略了类包引用):
复合主键类:
@Embeddable
public class OrderItemPK implements Serializable{
private Product product;
private Order order;
@ManyToOne
@JoinColumn(name="product_id",referencedColumnName="id")
public Product getProduct(){
return product;
}
public void setProduct(Product product){
this.product=product;
}
@ManyToOne
@JoinColumn(name="order_id",referencedColumnName="id")
public Order getOrder(){
return order;
}
public void setOrder(Order order){
this.order=order;
}
public boolean equals(Object object){...}
public int hashCode(){...}
}
OrderItem类:
@Entity
@org.hibernate.annotation.Entity(dynamicInsert=true,dynamicUpdate=true)
@Table(name="order_item")
@IdClass(OrderItemPK.class)
public class OrderItem implements Serializable{
private Product product;
private Order order;
private int quantity;
@Id
public Product getProduct(){
return product;
}
public void setProduct(Product product){
this.product=product;
}
@Id
public Order getOrder(){
return order;
}
public void setOrder(Order order){
this.order=order;
}
@Column(name="quantity")
public int getQuantity(){
return quantity;
}
public void setQuantity(int quantity){
this.quantity=quantity;
}
}
Product类:
@Entity
@org.hibernate.annotation.Entity(dynamicInsert=true,dynamicUpdate=true)
@Table(name="product")
public class Product implements Serializable{
private int id;
private String name;
private double price;
private Set<OrderItem> orderItems;
@Id
@GenericGenerator(name="g_id",strategy="increment")
@GeneratedValue(generator="g_id")
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public double getPrice(){
return price;
}
public void setPrice(double price){
this.price=price;
}
@OneToMany(mappedBy="product")
public Set<OrderItem> getOrderItems(){
return orderItems;
}
public void setOrderItems(Set<OrderItem> orderItems){
this.orderItems=orderItems;
}
}
Order类:
@Entity
@org.hibernate.annotation.Entity(dynamicInsert=true,dynamicUpdate=true)
@Table(name="tbl_order")
public class Order implements Serializable{
private int id;
private Calendar date;
private Set<OrderItem> orderItems;
@Id
@GenericGenerator(name="g_id",strategy="increment")
@GeneratedValue(generator="g_id")
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
public Calendar getDate(){
return date;
}
public void setDate(Calendar date){
this.date=date;
}
@OneToMany(mappedBy="order")
public Set<OrderItem> getOrderItems(){
return orderItems;
}
public void setOrderItems(Set<OrderItem> orderItems){
this.orderItems=orderItems;
}
}