<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="hbm"> <class name="EmylinkUpcode" table="emylink_upcode"> <id name="id" type="integer" column="plink_code" > <generator class="org.hibernate.id.IncrementGenerator"/> </id>
<property name="account" column="account" type="string" not-null="true" length="20" /> <property name="plinkName" column="plink_name" type="string" not-null="true" length="50" /> <property name="plinkExplain" column="plink_explain" type="string" not-null="false" length="100" /> <property name="typeCode" column="type_code" type="string" not-null="false" length="8" /> <set name="emylinkUlinks" inverse="true" lazy="true" cascade="all"> <key column="plink_code"/> <one-to-many class="EmylinkUlink"/> </set>
</class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="hbm"> <class name="EmylinkUlink" table="emylink_ulink" > <id name="id" type="integer" column="id" > <generator class="org.hibernate.id.IncrementGenerator"/> </id>
<property name="account" column="account" type="string" not-null="true" length="20" /> <property name="link" column="link" type="string" not-null="true" length="200" /> <property name="linkName" column="link_name" type="string" not-null="true" length="20" /> <property name="explain" column="explain" type="string" not-null="false" length="500" /> <property name="indate" column="indate" type="date" not-null="true" length="16" insert="false" //不参与插入操作 update="false" //不参与更新操作 /> <property name="sort" column="sort" type="string" not-null="false" length="3" /> <property name="private" column="private" type="string" not-null="false" length="1" /> <many-to-one name="plinkCode" column="plink_code" class="EmylinkUpcode" not-null="true" > </many-to-one> </class> </hibernate-mapping>
* 延迟加载 如果lazy=true(延迟加载), 加载EmylinkUpcode时,hibernate不会立即加载EmylinkUlink,只有当(Iterator<EmylinkUlink> it = ul.iterator();)执行时,hibernate才加载EmylinkUlink实例; 如果在加载前就关闭session,则报异常LazyInitializationException ; 可以使用Hibernate.initialize(Object o)强制及联加载 *J2SE5.0的泛型
emylinkUlinks集合中只能存放EmylinkUlink对象,从集合中获取对象无需再类型转换 struts+spring+hibernate关于hibernate中lazy="true"的问题。web.xml中用
它的原理就是:打开页面的时候打开session,一直到页面装载完毕才关闭session,这样就解决了lazy="true"时session is closed的问题。 关于OpenSessionInViewFilter 延迟加载失效问题 http://www.javaeye.com/topic/15057 OpenSessionInView的效率问题 http://www.javaeye.com/topic/17501