检索策略 lazy 分为三个值,默认为 true
true:延迟检索。
false:立即检索。
extra:增强的延迟检索,读取值会尽可能的延迟集合初始化的时机。
User.hbm.xml:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2016-6-25 14:50:51 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <!-- 延迟检索 lazy="false": 延迟加载,仅对load()有效。默认为true true:延迟检索, false:立即检索, extra:增强的延迟检索,读取值会尽可能的延迟集合初始化的时机 --> <class name="com.shuoeasy.test.User" table="USER" lazy="true"> <id name="id" type="int"> <column name="ID" /> <!-- 指定主键的生成方式,native:使用数据库本地方式 --> <generator class="native" /> </id> <property name="userName" type="java.lang.String"> <column name="USERNAME" /> </property> <set name="orders" table="ORDERS" inverse="true" lazy="extra"> <key column="USER_ID"></key> <one-to-many class="com.shuoeasy.test.Orders"/> </set> </class> </hibernate-mapping>
User.java Order.java Order.hbm.xml代码略。。。
AppTest.java:
package com.shuoeasy.test; import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.junit.After; import org.junit.Before; import org.junit.Test; /** * Unit test for simple App. */ public class AppTest { Session session; SessionFactory sf; @Before public void init() { Configuration conf = new Configuration().configure(); ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build(); sf = conf.buildSessionFactory((org.hibernate.service.ServiceRegistry) sr); session = sf.openSession(); session.beginTransaction(); System.out.println("init"); } @After public void destory() { session.getTransaction().commit(); session.close(); sf.close(); System.out.println("dectory"); } /** * 插入数据 */ @Test public void testInsert(){ User user = new User(); user.setUserName("张三"); Orders order1 = new Orders(); order1.setTitle("张三的订单1"); order1.setUser(user); Orders order2 = new Orders(); order2.setTitle("张三的订单2"); order2.setUser(user); user.getOrders().add(order1); // inverse="false"会出现update user.getOrders().add(order2); // inverse="false"会出现update /** * 执行save操作时,先插入user,然后插入orders * 由于在一和多的维护关系会出现update,所以最好设置inverse="false" * 这样子插入数据就不会出现update的语句 */ session.save(user); session.save(order1); session.save(order2); } /** * 读取数据 */ @Test public void testGet() { // 多的一端使用延迟加载 User user = (User) session.get(User.class, 1); System.out.println(user); System.out.println(user.getOrders()); } /** * 类级别的检索策略(lazy) */ @Test public void testClassLever(){ User user = (User) session.load(User.class, 1); System.out.println(user.getClass()); System.out.println(user.toString()); } /** * 类级别的检索策略(lazy) */ @Test public void testOne2ManyLevelStrategy(){ User user = (User) session.load(User.class, 1); System.out.println(user.getUserName()); System.out.println(user.getOrders().size()); } /** * Hibernate.initialize() 强制改为立即检索 */ @Test public void testInitialize(){ User user = (User) session.load(User.class, 1); Hibernate.initialize(user); System.out.println(user.getClass()); } }