因为hibernate load出来的时候只是一个proxy,并不是真正的admin,这个proxy只有和admin相同的id属性,而其他属性都是null。
所以如果你在事务外面读取其他属性(除id)都会产生LazyInitializationException(大概吧,记不清了 )。但是,当你在事务里
读取了其他属性,(这里是password)那么,hibernate会足够聪明的把这个proxy转化成真正的admin,所以这时候你得到的是真正的
admin。如果你不想这么麻烦,想要在读取admin时就得到一个真正的实体,那么可以设置lazy = "false", (当然还有其他的办法);

解决方案 »

  1.   

    在hibernate.cfg里面设置lazy??如何设置呢?thx!!
      

  2.   

    在具体的属性上设置<class name="Document">
           <id name="id">
            <generator class="native"/>
        </id>
        <property name="name" not-null="true" length="50"/>
        <property name="summary" not-null="true" length="200" lazy="true"/>
        <property name="text" not-null="true" length="2000" lazy="true"/>
    </class>
      

  3.   

    在具体的属性上设置<class name="Document">
           <id name="id">
            <generator class="native"/>
        </id>
        <property name="name" not-null="true" length="50"/>
        <property name="summary" not-null="true" length="200" lazy="true"/>
        <property name="text" not-null="true" length="2000" lazy="true"/>
    </class>
      

  4.   

    哈哈……我真是菜鸟咯!在各个表的hbm里面设置都不会哦!^_^
      

  5.   


    为什么不在<class name="Document" lazy="false">呢?
      

  6.   

    hibernate-mapping
    class
    property
    subclass
    component
    等等元素都有跟lazy相关的设置,可以根据自己的选择适当的地方来设置。