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();
}
}
完~