hibernate lazy属性对load的影响

User user = (User) session.load(
     "com.jaeson.hibernatestudy.bean.User", "402881e5529691a701529691a8920000");
user.getUserId();
user.getUserName();
user.getDepartment().getDeptName();
user.getIdCard().getCardNo();
user.getRoles().toArray();

 User.hbm.xml:

    <many-to-one name="department" class="com.jaeson.hibernatestudy.bean.Department" fetch="select">

        <column name="deptId" length="32" not-null="true" />
    </many-to-one>

    <one-to-one name="idCard" property-ref="user"></one-to-one>

    <set name="roles" table="user_role"  catalog="db4myeclipse">
        <key>
            <column name="userId" length="32" not-null="true" />
        </key>
        <many-to-many entity-name="com.jaeson.hibernatestudy.bean.Role">
            <column name="roleId" not-null="true" />
        </many-to-many>
    </set>

 

默认值:

<class lazy=(true/false)> 默认为true

<component lazy=(true/false)> 默认为false

<many-to-one lazy=(proxy/no-proxy/false)> 默认为proxy

<one-to-one lazy=(proxy/no-proxy/false)> 默认为proxy

<set lazy=(true/extra/false)> 默认为true

 

使用默认值load时:hibernate返回的是User的代理对象,User对象的属性都没有被初始化,具体的初始化操作将有handler执行。

其中initialized是初始化标志,target则是在初始化时保存真正的User对象。

 

在当执行user.getUserId()时,hibernate直接返回handler属性的id值,不会进行数据库查询操作。

 
hibernate lazy属性对load的影响_第1张图片

 

当执行user.getUserName()时,hibernate开始真正执行数据库查询,初始化User,此时initialized=true,target持有真正的User对象。

Hibernate: select user0_.userId as userId4_1_, user0_.deptId as deptId4_1_, user0_.userName as userName4_1_, user0_.address as address4_1_, user0_.zipCode as zipCode4_1_, user0_.phone as phone4_1_, idcard1_.id as id0_0_, idcard1_.userId as userId0_0_, idcard1_.cardNo as cardNo0_0_, idcard1_.authDate as authDate0_0_ from db4myeclipse.user user0_ left outer join db4myeclipse.idcard idcard1_ on user0_.userId=idcard1_.userId where user0_.userId=?


hibernate lazy属性对load的影响_第2张图片
 
 target持有的User对象初始化了:基本属性userName,组件属性address,one2one属性idCard;

many2one属性department则返回hibernate的代理对象。

many2many的<set>类型属性为PersistentSet,并没有加载。


hibernate lazy属性对load的影响_第3张图片
 
当执行user.getDepartment().getDeptName()时,由department的handler进行初始化。
 Hibernate: select department0_.deptId as deptId3_0_, department0_.parentId as parentId3_0_, department0_.deptName as deptName3_0_ from db4myeclipse.department department0_ where department0_.deptId=?


hibernate lazy属性对load的影响_第4张图片
 

当执行user.getRoles().toArray()时,hibernate进行数据库操作对<set>进行初始化:
 Hibernate: select roles0_.userId as userId4_1_, roles0_.roleId as roleId1_, role1_.roleId as roleId1_0_, role1_.roleName as roleName1_0_, role1_.roleDesc as roleDesc1_0_ from db4myeclipse.user_role roles0_ inner join db4myeclipse.role role1_ on roles0_.roleId=role1_.roleId where roles0_.userId=?

 
hibernate lazy属性对load的影响_第5张图片
 

 

 <class lazy="false"> class属性设为不延迟加载,其他项使用默认值时:

在执行session.load()时返回的是User对象,hibernate初始化了:基本属性userName,组件属性address,one2one属性idCard;

many2one的department返回hibernate的代理对象。

many2many的<set>类型属性为PersistentSet,并没有加载。


hibernate lazy属性对load的影响_第6张图片

 

 <class lazy="false"> class属性设为不延迟加载,many2one显式设置不延迟加载:

        <many-to-one name="department" class="com.jaeson.hibernatestudy.bean.Department" lazy="false" fetch="select">
            <column name="deptId" length="32" not-null="true" />
        </many-to-one>

在执行session.load()时,many2one的department返回的是Department对象。
hibernate lazy属性对load的影响_第7张图片
 
 

 

你可能感兴趣的:(hibernate lazy属性对load的影响)