把多对多关联分解为两个一对多关联,具有更好的可扩展性和操作性。

以商品、订单及商品订单关联项为例:
商品和订单为多对多关系
可以拆分为两个一对多关系
商品对商品订单关联项为一个一对多
订单对商品订单关联项为一个一对多。

商品实体类及映射文件

package com.bjsxt.hibernate;

import java.util.Set;

public class Item {

private int id;

private String name;

private double basePrice;

private Set lineItems;

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 Set getLineItems() {
return lineItems;
}

public void setLineItems(Set lineItems) {
this.lineItems = lineItems;
}

public double getBasePrice() {
return basePrice;
}

public void setBasePrice(double basePrice) {
this.basePrice = basePrice;
}

}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name=
"com.bjsxt.hibernate.Item" table="t_item" lazy="false">
<id name=
"id">
<generator class=
"native"/>
</id>
<property name=
"name"/>
<property name=
"basePrice"/>

<set name=
"lineItems" lazy="false" cascade="save-update">
<key column=
"item_id"/>
<one-to-many class=
"com.bjsxt.hibernate.LineItem"/>
</set>
</class>
</hibernate-mapping>
订单实体类及配置文件
package com.bjsxt.hibernate;

import java.util.Set;

public class Order {

private int id;

private String orderNumber;

private double price;

private Set lineItems;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getOrderNumber() {
return orderNumber;
}

public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public Set getLineItems() {
return lineItems;
}

public void setLineItems(Set lineItems) {
this.lineItems = lineItems;
}

}
<?xml version=
"1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name=
"com.bjsxt.hibernate.Order" table="t_order" lazy="false">
<id name=
"id">
<generator class=
"native"/>
</id>
<property name=
"orderNumber"/>
<property name=
"price"/>

<set name=
"lineItems" lazy="false" cascade="save-update">
<key column=
"order_id"/>
<one-to-many class=
"com.bjsxt.hibernate.LineItem"/>
</set>
</class>
</hibernate-mapping>
订单和商品关联实体类及配置文件
package com.bjsxt.hibernate;


public class LineItem {

private int id;

private int quantity;

private double basePrice;

private Order order;

private Item item;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public double getBasePrice() {
return basePrice;
}

public void setBasePrice(double basePrice) {
this.basePrice = basePrice;
}

public Order getOrder() {
return order;
}

public void setOrder(Order order) {
this.order = order;
}

public Item getItem() {
return item;
}

public void setItem(Item item) {
this.item = item;
}

}
<?xml version=
"1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name=
"com.bjsxt.hibernate.LineItem" table="t_line_items" lazy="false">
<id name=
"id">
<generator class=
"native"/>
</id>
<property name=
"quantity"/>
<property name=
"basePrice"/>

<many-to-one name=
"order" column="order_id" class="com.bjsxt.hibernate.Order" not-null="true"/>
<many-to-one name=
"item" column="item_id" class="com.bjsxt.hibernate.Item" not-null="true"/>
</class>
</hibernate-mapping>

测试类:
package com.bjsxt.hibernate;

import java.util.HashSet;

import junit.framework.TestCase;

import org.hibernate.Session;

public class Many2ManyTest extends TestCase {

public void testSave2() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();

Item item = new Item();
item.setBasePrice(1.0);
item.setName(
"<<struts2详解>>");
item.setLineItems(new HashSet());
session.save(item);

Order order = new Order();
order.setOrderNumber(
"001");
order.setPrice(1.0);
order.setLineItems(new HashSet());
session.save(order);

LineItem lineItem = new LineItem();
lineItem.setBasePrice(item.getBasePrice());
lineItem.setItem(item);
lineItem.setOrder(order);
lineItem.setQuantity(10);

session.save(lineItem);

session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}
了哈哈 @2011-03-10 11:00:49  哈哈了
Hibernate是我最喜欢的技术之一。非常想学好这门技术。
lehaha @2011-03-10 10:51:37  hahale
海波奈特是我最喜欢的技术之一了。谢谢楼主的无私奉献给

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

认证码:
(数字如不清楚,请刷新一次)


Copyright@2004-2010 powered by YuLog