懒加载
通过asm和cglib二个包实现;Domain是非final的。
1.session.load懒加载。
2.one-to-one(元素)懒加载:
必需同时满足下面三个条件时才能实现懒加载
(主表不能有constrained=true,所以主表没有懒加载)
lazy!=false 2)constrained=true 3)fetch=select
3.one-to-many (元素)懒加载:1)lazy!=false 2)fetch=select
4.many-to-one (元素) :1)lazy!=false 2)fetch=select
5.many-to-many (元素) :1)lazy!=false 2)fetch=select
6.能够懒加载的对象都是被改写过的代理对象,当相关联的session没有关闭时,访问这些懒加载对象(代理对象)的属性(getId和getClass除外)hibernate会初始化这些代理,或用Hibernate.initialize(proxy)来初始化代理对象;当相关联的session关闭后,再访问懒加载的对象将出现异常。
一对一懒加载(对系统影响不算太大)
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.dzq.domain"> <class name="IDCard" table="id_card"> <id name="id" column="id"> <generator class="foreign"> <param name="property">person</param> </generator> <!-- <generator class="native"/> --> </id> <property name="name" column="name" /> <one-to-one name="person" constrained="true" lazy="proxy" fetch="join"/> <!-- <many-to-one name="person" column="person_id" unique="true"/> --> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.dzq.domain"> <class name="Person" table="person"> <id name="id" column="id"> <generator class="native" /> </id> <property name="name" column="name" /> <one-to-one name="idcard"/> <!-- <one-to-one name="idcard" property-ref="person"/> --> </class> </hibernate-mapping>
多对一懒加载(影响性能,最好不禁用懒加载,使用hibernate初始化代理,解决)
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.dzq.domain"> <class name="Department" table="department"> <id name="id" column="id"> <generator class="native" /> </id> <property name="name" column="name" /> <!-- <set name="empls" cascade="save-update"> <key column="depart_id"/> <one-to-many class="Employee"/> </set> --> <set name="empls" inverse="true"> <key column="depart_id"/> <one-to-many class="Employee"/> </set> <!-- <list name="empls"> <key column="depart_id"/> <list-index column="order_col"/> <one-to-many class="Employee"/> </list> --> <!-- <bag name="empls"> <key column="depart_id"/> <one-to-many class="Employee"/> </bag> --> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.dzq.domain"> <class name="Employee" table="employee" discriminator-value="0"> <id name="id" column="id"> <generator class="hilo" /> </id> <!-- <discriminator column="type"/> --> <property name="name" column="name" /> <many-to-one name="depart" column="depart_id" /> <!-- <subclass name="Skiller" discriminator-value="1"> <property name="skiller"/> </subclass> <subclass name="Sales" discriminator-value="2"> <property name="sell"/> </subclass> --> <!-- <joined-subclass name="Skiller" table="skiller"> <key column="emp_id"/> <property name="skiller"/> </joined-subclass> <joined-subclass name="Sales" table="sales"> <key column="emp_id"/> <property name="sell"/> </joined-subclass> --> <!-- <subclass name="Skiller" discriminator-value="1"> <property name="skiller"/> </subclass> <subclass name="Sales" discriminator-value="2"> <join table="sales"> <key column="emp_id"/> <property name="sell"/> </join> </subclass> --> <union-subclass name="Skiller"> <property name="skiller"/> </union-subclass> <union-subclass name="Sales"> <property name="sell"/> </union-subclass> </class> </hibernate-mapping>
public static Department queryDepart(int id){ Session s=null; try{ s=HibernateUntils.getSession(); Department depart=(Department) s.get(Department.class, id); Hibernate.initialize(depart.getEmpls());//hibernate初始化代理 System.out.println(depart.getEmpls().getClass()); return depart; }finally{ if(s!=null){ s.close(); } } }
多对多懒加载((更加影响性能,最好不禁用懒加载,使用hibernate初始化代理,解决))
时间: 2024-10-08 06:04:06