Hibernate延时加载:属性延时加载与关系延时加载

Hibernate延迟加载:

1.属性的延时加载

package com.joey.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.joey.pojo.Employee;
import com.joey.pojo.Position;

public class Hibernate_lazyload {

	public static void main(String[] args) {
		SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

		Session session = sessionFactory.openSession();
		session.beginTransaction();

		// 属性延迟加载
		Employee employee = (Employee) session.load(Employee.class, 7);
		System.out.println("本行日志之后才开始执行sql语句(访问数据库),因为下一条getName()语句将访问属性");
		System.out.println(employee.getName());

		
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
	}
}


2.关系的延时加载

未开启延时加载:Position.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">

<!-- Posotion.hbm.xml 提供对象与表的映射关系 -->
<hibernate-mapping package="com.joey.pojo">
	<class name="Position" table="position">
		<id name="id" column="id">
			<generator class="native">
			</generator>
		</id>
		<property name="positionName" />
		
		<set name="employees" lazy="false"><----------------lazy="false"()
		  <key column="cid" not-null="false"></key>
		  <one-to-many class="Employee" />
		</set>
	</class>
</hibernate-mapping>

package com.joey.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.joey.pojo.Employee;
import com.joey.pojo.Position;


public class Hibernate_lazyload {
	
	public static void main(String[] args) {
		SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
		
		Session session=sessionFactory.openSession();
		session.beginTransaction();
		
		/*// 属性延迟加载
		Employee employee=(Employee)session.load(Employee.class, 7);
		System.out.println("本行日志之后才开始执行sql语句(访问数据库),因为下一条getName()语句将访问属性");
		System.out.println(employee.getName());*/
		
		// 关系延迟加载
		Position position=(Position) session.get(Position.class, 3);
		
		System.out.println("程序将在本行日志之前访问数据库");
		System.out.println(position.getEmployees());
		
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
	}
}

开启延时加载: Position.xml

<set name="employees" lazy="true">
package com.joey.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.joey.pojo.Employee;
import com.joey.pojo.Position;


public class Hibernate_lazyload {
	
	public static void main(String[] args) {
		SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
		
		Session session=sessionFactory.openSession();
		session.beginTransaction();
		
		/*// 属性延迟加载
		Employee employee=(Employee)session.load(Employee.class, 7);
		System.out.println("本行日志之后才开始执行sql语句(访问数据库),因为下一条getName()语句将访问属性");
		System.out.println(employee.getName());*/
		
		// 关系延迟加载
		Position position=(Position) session.get(Position.class, 3);
		
		System.out.println("程序将在本行日志会后访问employee表,因为开启了延时加载并且getEmployee()在下一行");
		System.out.println(position.getEmployees());
		
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
	}
}


完~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值