1. 什么是关联(association)
1.1 关联指的是类之间的引用关系。如果类A与类B关联,那么被引用的类B将被定义为类A的属性。例如:
public class A{
private B b = new B;
public A(){}
}
1.2 关联的分类:关联可以分为一对一、一对多/多对一、多对多关联
关联是有方向的
现在我们就讲的是一对多
把实体及配置文件导入
Order
/** * hibernate关联关系 * 一对多:一个订单对应多个订单项 * 多对一:多个订单项对应一个订单(注:一个订单项对应一个订单) * @author Administrator * */ public class Order implements Serializable { //订单id private Integer orderId; //订单编号 private String orderNO; //1.1 关联指的是类之间的引用关系。如果Order与类OrderItem关联, //那么被引用的OrderItem将被定义为Order 的属性。例如: private Set<OrderItem> orderitems=new HashSet<OrderItem>(); public Set<OrderItem> getOrderitems() { return orderitems; } public void setOrderitems(Set<OrderItem> orderitems) { this.orderitems = orderitems; } public Integer getOrderId() { return orderId; } public void setOrderId(Integer orderId) { this.orderId = orderId; } public String getOrderNO() { return orderNO; } public void setOrderNO(String orderNO) { this.orderNO = orderNO; } public Order() { super(); } @Override public String toString() { return "Order [orderId=" + orderId + ", orderNO=" + orderNO + ", orderitems=" + orderitems + "]"; } }
Order.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.hmc.hibernate02.entity.Order" table="t_order_hb"> <id name="orderId" type="java.lang.Integer" column="order_id"> <!--native:identity(标识列)+sequence(序列) --> <generator class="native"></generator> </id> <property name="orderNO" type="java.lang.String" column="order_no"/> <!--一方中的主键就是多方中的外键 --> <!-- select o.*,oi.* from t_order_hb o inner join t_order_item_hb oi on o.order_id=oi.oid --> <!--建立关联关系 --> <!-- name:实体类中定义的属性名,指向多方 cassade:用来控制如何操作关联的持久化对象的 --> <set name="orderitems" cascade="save-update" inverse="true" table="t_order_item_hb" > <!-- 指定连接的多方中的外键列 --> <key column="oid"></key> <!--指定实体之间的关联关系:一对多 --> <!--class:指向的是多方的实体类的全路径名 --> <one-to-many class="com.hmc.hibernate02.entity.OrderItem"/> </set> </class> </hibernate-mapping>
OrderItem
/** * 多个订单项对应一个订单(注意:一个订单项唯一对应一个订单) * @author Administrator * */ public class OrderItem implements Serializable{ //订单项id private Integer orderItemId; //产品id private Integer productId; //数量 private Integer quantity; //外键列 private Integer oid; //1.1 关联指的是类之间的引用关系。如果OrderItem与类Order关联, //那么被引用的Order将被定义为OrderItem 的属性。例如: private Order order; public Order getOrder() { return order; } public void setOrder(Order order) { this.order = order; } public Integer getOrderItemId() { return orderItemId; } public void setOrderItemId(Integer orderItemId) { this.orderItemId = orderItemId; } public Integer getProductId() { return productId; } public void setProductId(Integer productId) { this.productId = productId; } public Integer getQuantity() { return quantity; } public void setQuantity(Integer quantity) { this.quantity = quantity; } public Integer getOid() { return oid; } public void setOid(Integer oid) { this.oid = oid; } @Override public String toString() { return "OrderItem [orderItemId=" + orderItemId + ", productId=" + productId + ", quantity=" + quantity + ", oid=" + oid + "]"; }
OrderItem.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.hmc.hibernate02.entity.OrderItem" table="t_order_item_hb"> <id name="orderItemId" type="java.lang.Integer" column="order_item_id"> <!--native:identity(标识列)+sequence(序列) --> <generator class="native"></generator> </id> <property name="productId" type="java.lang.Integer" column="product_id"/> <property name="quantity" type="java.lang.Integer" column="quantity"/> <property insert="false" update="false" name="oid" type="java.lang.Integer" column="oid"/> <!--建立关联关系 --> <!-- name:实体类中定义的属性名,指向一方 --> <many-to-one name="order" cascade="save-update" class="com.hmc.hibernate02.entity.Order"> <!--多方的数据库表中的外键列,指向一方 --> <column name="oid"></column> </many-to-one> </class> </hibernate-mapping>
OrderDao
public void add(Order order) { Session session = SessionFactoryUtils.openSession(); Transaction transaction = session.beginTransaction(); session.save(order); transaction.commit(); SessionFactoryUtils.closeSession(); } public Order get(Order order) { Session session = SessionFactoryUtils.openSession(); Transaction transaction = session.beginTransaction(); Order o = session.get(Order.class, order.getOrderId()); //处理懒加载 if(null!=o) { Hibernate.initialize(o.getOrderitems()); } transaction.commit(); SessionFactoryUtils.closeSession(); return o; } public void update(Order order){ Session session = SessionFactoryUtils.openSession(); Transaction transaction = session.beginTransaction(); //先查,再更新 Order o = session.get(Order.class, order.getOrderId()); if(null!=o) { //拿到多方 Set<OrderItem> orderitems = o.getOrderitems(); OrderItem orderItem=null; for (int i = 0; i <=2; i++) { orderItem=new OrderItem(); orderItem.setProductId(i); orderItem.setQuantity(i+2); //建立关联关系 orderItem.setOrder(o); o.getOrderitems().add(orderItem); } session.update(o); } transaction.commit(); SessionFactoryUtils.closeSession(); } //级联删除 public void delete(Order order) { Session Session = SessionFactoryUtils.openSession(); Transaction Transaction = Session.beginTransaction(); //先查询,在删除 Order o = Session.get(Order.class, order.getOrderId()); if(null!=o) { /** * 解除关联关系:先删从表,再删主表 * 1)禁用联级删除 * 2)先删从表,再删主表 * 3)手动代码维护 */ Set<OrderItem> orderitems = o.getOrderitems(); for (OrderItem orderItem : orderitems) { Session.delete(orderItem); } Session.delete(o); } Transaction.commit(); SessionFactoryUtils.closeSession(); }
测试类进行测试OrderDaoTest
Hibernate: select order0_.order_id as order_id1_0_0_, order0_.order_no as order_no2_0_0_ from t_order_hb order0_ where order0_.order_id=? Hibernate: select orderitems0_.oid as oid4_1_0_, orderitems0_.order_item_id as order_it1_1_0_, orderitems0_.order_item_id as order_it1_1_1_, orderitems0_.product_id as product_2_1_1_, orderitems0_.quantity as quantity3_1_1_, orderitems0_.oid as oid4_1_1_ from t_order_item_hb orderitems0_ where orderitems0_.oid=? Hibernate: delete from t_order_item_hb where order_item_id=? Hibernate: delete from t_order_item_hb where order_item_id=? Hibernate: delete from t_order_item_hb where order_item_id=? Hibernate: delete from t_order_hb where order_id=?