hibernate关联关系一对多

1. 什么是关联(association)
1.1 关联指的是类之间的引用关系。如果类A与类B关联,那么被引用的类B将被定义为类A的属性

首先建2个实体类

Order.java

package com.jt3.entity;

import java.util.ArrayList;
import java.util.List;

public class Order {

     private Integer orderId;
        private String orderNo;
        private List<OrderItem> orderItems = new ArrayList<>();
        private Integer initChildren = 1;//1代表开启懒加载,默认。0则代表关闭懒加载
        
        public Integer getInitChildren() {
            return initChildren;
        }
        public void setInitChildren(Integer initChildren) {
            this.initChildren = initChildren;
        }
        public List<OrderItem> getOrderItems() {
            return orderItems;
        }
        public void setOrderItems(List<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;
        }
        @Override
        public String toString() {
            return "Order [orderId=" + orderId + ", orderNo=" + orderNo + ", orderItems=" + orderItems + "]";
        }
        public Order(Integer orderId, String orderNo) {
            super();
            this.orderId = orderId;
            this.orderNo = orderNo;
        }
        public Order() {
            super();
            // TODO Auto-generated constructor stub
        }
        
    
}

 OrderItem.java

package com.jt3.entity;

public class OrderItem {

    private Integer orderItemId;
    private Integer productId;
    private Integer quantity;
    private Integer oid;
    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 + ", order=" + order + "]";
    }
    public OrderItem(Integer orderItemId, Integer productId, Integer quantity, Integer oid) {
        super();
        this.orderItemId = orderItemId;
        this.productId = productId;
        this.quantity = quantity;
        this.oid = oid;
    }
    public OrderItem() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    
}

 

然后是映射文件

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.jt3.entity.Order" table="t_hibernate_order">
        <id name="orderId" type="java.lang.Integer" column="order_id">
            <generator class="increment" />
        </id>
        <property name="orderNo" type="java.lang.String" column="order_no">
        </property>
        <!--需要在映射文件中进行关联关系的维护   这里描述的是一对多的关系 -->
        <bag name="orderItems" cascade="save-update" inverse="true">
        <!-- 从表的外键 -->
        <key column="oid"></key>
        <!-- 查询从表数据,然后形成list集合填充到orderItems属性中 -->
        <one-to-many class="com.jt3.entity.OrderItem"></one-to-many>
        <!--  
        selct * from t_hibernate_order_item where oid=1
        
        -->
        
        
        </bag>
    </class>

</hibernate-mapping>

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.jt3.entity.OrderItem" table="t_hibernate_order_item">
        <id name="orderItemId" type="java.lang.Integer" column="order_item_id">
            <generator class="increment" />
        </id>
        <property name="productId" type="java.lang.Integer" column="product_id">
        </property>
        <property name="quantity" type="java.lang.Integer" column="quantity">
        </property>
        <property name="oid" type="java.lang.Integer" column="oid" insert="false" update="false">
        </property>
        
        <!--需要在映射文件中进行关联关系的维护   这里描述的是多对一的关系 -->
        <!--Repeated column in mapping for entity: com.jt3.entity.OrderItem column: oid (should be mapped with insert="false" update="false")  -->
        <many-to-one name="order" class="com.jt3.entity.Order" column="oid"></many-to-one>
    </class>
</hibernate-mapping>

 

要把映射文件配置进

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
       <session-factory>
       <!-- 1. 数据库相关 -->
        <property name="connection.username">root</property>
        <property name="connection.password">123</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/sys?useUnicode=true&amp;characterEncoding=UTF-8
        </property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- 配置本地事务(No CurrentSessionContext configured!) -->
        <property name="hibernate.current_session_context_class">thread</property>

        <!-- 2. 调试相关 -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>

        <!-- 3. 添加实体映射文件 -->
        <mapping resource="com/jt/entity/User.hbm.xml"/>
        <!--主键生成策略  -->
        <mapping resource="com/jt2/entity/Student.hbm.xml"/>
        <mapping resource="com/jt2/entity/Worker.hbm.xml"/>
        <!-- 一对多 -->
        <mapping resource="com/jt3/entity/Order.hbm.xml"/>
        <mapping resource="com/jt3/entity/OrderItem.hbm.xml"/>
       </session-factory>
    </hibernate-configuration>

 

然后就是写dao层

DemoDao.java

package com.jt3.dao;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.jt2.util.SessionFactoryUtils;
import com.jt3.entity.Order;
import com.jt3.entity.OrderItem;

public class DemoDao {
      /**
     * 为了测试关系型映射文件配置准确
     *     讲解insert=false,update=false的用途
     * @param order
     * @return
     */
    public Integer addOrder(Order order) {
        Session session = SessionFactoryUtils.openSession();
        Transaction transaction = session.beginTransaction();
        Integer oid = (Integer)session.save(order);
        transaction.commit();
        session.close();
        return oid;
    }
    
    public Integer addOrderItem(OrderItem orderItem) {
        Session session = SessionFactoryUtils.openSession();
        Transaction transaction = session.beginTransaction();
        Integer otid = (Integer)session.save(orderItem);
        transaction.commit();
        session.close();
        return otid;
    }
    
    
    
    /**
     * 为了讲解懒加载的问题(hibernate3.0后所有查询方式默认采用的是懒加载方式)
     *     1、查单个时存在问题,代理对象已经关闭
     *     2、查多个存在问题,有性能的问题
     * @param order
     * @return
     */
    public Order getOrder(Order order) {
        Session session = SessionFactoryUtils.openSession();
        Transaction transaction = session.beginTransaction();
        Order o = session.get(Order.class, order.getOrderId());
        if(o != null && new Integer(0).equals(order.getInitChildren())) {
            //0代表程序员想要关闭懒加载,也就意味着需要强制加载关联关系对象
            Hibernate.initialize(o.getOrderItems());
//            System.out.println(o.getOrderItems());
        }
        transaction.commit();
        session.close();
        return o;
    }
    
    public List<Order> getOrderList() {
        Session session = SessionFactoryUtils.openSession();
        Transaction transaction = session.beginTransaction();
        List<Order> list = session.createQuery("from Order").list();
        transaction.commit();
        session.close();
        return list;
    }
    
    /**
     * z主表的数据不能随便删除,得先删除从表中对应信息,才能删除主表的信息。
     * @param order
     */
    public void delOrder(Order order) {
        Session session = SessionFactoryUtils.openSession();
        Transaction transaction = session.beginTransaction();
        Order order2 = session.get(Order.class, order.getOrderId());
        for (OrderItem oi : order2.getOrderItems()) {
            session.delete(oi);
        }
        session.delete(order2);
//        session.delete(order);
        transaction.commit();
        session.close();
    }
}

 

 

然后就是使用junit来测试CTRL+N建一个测试类

选择我们需要测试的DAO方法,就会自动生成一个类DemoDaoTest.java

package com.jt3.dao;

import java.util.List;

import org.junit.Test;

import com.jt3.entity.Order;
import com.jt3.entity.OrderItem;

public class DemoDaoTest {

     private DemoDao demoDao = new DemoDao();
        
        /**
         * 新增订单(单个对应多个订单项)
         */
        @Test
        public void testAddOrder() { 
            Order order = new Order();
            order.setOrderNo("jt111111");
            OrderItem oi = null;
            for (int i = 0; i < 3; i++) {
                oi = new OrderItem();
                oi.setProductId(10+i);
                oi.setQuantity(20+i);
                oi.setOrder(order);
                order.getOrderItems().add(oi);
            }
            demoDao.addOrder(order);
        }

        /**
         * 新增订单项(多个)
         */
        @Test
        public void testAddOrderItem() {
            Order order = new Order();
            order.setOrderId(5);
            OrderItem oi = null;
            for (int i = 0; i < 3; i++) {
                oi = new OrderItem();
                oi.setProductId(10+i);
                oi.setQuantity(20+i);
                oi.setOrder(order); 
                order.getOrderItems().add(oi);
                demoDao.addOrderItem(oi);
            }
        }

        /**
         * 查询单个
         * 重点:
         *    关于懒加载的问题
         *  hibernate3之后出现的;
         *   错误信息:
         * failed to lazily initialize a collection of role: 
         * com.yuan.three.entity.Order.orderItems, 
         * could not initialize proxy - no Session
         * 
         * orderNo数据来源于t_hibernate_order表
         * orderItems数据来源于t_hibernate_order_item表
         * 
         * 因为两表有关联关系所以不可以分开查询,
         * 而hibernate默认的策略是不查询关联关系对应关联表数据的
         * 
         * 处理方式:(第一种)
         * 在对应实体映射文件中的关联关系的维护中添加  lazy="false"
         * 如:<bag lazy="false" name="orderItems"  cascade="save-update" inverse="true">
         */
        @Test
        public void testGetOrder() {
            Order order = new Order();
            order.setOrderId(10);
            //关闭懒加载
            order.setInitChildren(0);
            Order o = this.demoDao.getOrder(order);
            System.out.println(o.getOrderNo());
            System.out.println(o.getOrderItems());
        }

        /**
         * 查询多个
         * 在查询单个中懒加载问题的解决方案
         * 会导致在查询多个的时候sql语句也会增多,
         * 当查询数据过多的时候导致代码性能变低
         * 
         * 总:如果将懒加载的功能关闭,即:lazy=false,
         * 那么在查询多条订单数据的情况下是非常影响性能的
         * 
         * 查单个需要关闭懒加载    目的是需要加载出关联表的数据
         * 查多个不能关闭懒加载    加载关联表的数据的查询次数过多
         * 
         * 处理方式:(第二种)
         * 在一对多的实体类中添加属性是否打开懒加载
         * 
         */
        @Test
        public void testGetOrderList() {
            List<Order> orderList = this.demoDao.getOrderList();
            for (Order order : orderList) {
                System.out.println(order.getOrderNo());
//                System.out.println(order.getOrderItems());
            }
        }
        /**
         * 删除订单,关联关系同时删除
         */
        @Test
        public void testDelOrder() {
            Order order = new Order();
            order.setOrderId(10);
            this.demoDao.delOrder(order);
        }

}

 

测试结果

 

 

 

再来演示下删除12

 

posted @ 2019-08-05 00:23  youshutup  阅读(123)  评论(0编辑  收藏  举报