随笔-86  评论-33  文章-0  trackbacks-0
EmylinkUpcode  1...................n  EmylinkUlink   一对多关系

1.hbm.xml描述
  EmylinkUpcode.hbm.xml

<?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>

操作级联(cascade)关系。
可选值:
all : 所有情况下均进行级联操作。
none:所有情况下均不进行级联操作。
save
-update:在执行save-update时
进行级联操作。
delete:在执行delete时进行级联操作。

 EmylinkUlink.hbm.xml

<?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>


2.POJO对象
  EmylinkUpcode.java
//
    
// primary key
    private java.lang.Integer id;

    
// fields
    private java.lang.String account;
    
private java.lang.String plinkName;
    
private java.lang.String plinkExplain;
    
private java.lang.String typeCode;

    
// collections
    private java.util.Set<hbm.EmylinkUlink> emylinkUlinks;

//

  EmylinkUlink.java
//
private int hashCode = Integer.MIN_VALUE;

    
// primary key
    private java.lang.Integer id;

    
// fields
    private java.lang.String account;
    
private java.lang.String link;
    
private java.lang.String linkName;
    
private java.lang.String explain;
    
private java.util.Date indate;
    
private java.lang.String sort;
    
private java.lang.String m_private;

    
// many to one
    private hbm.EmylinkUpcode plinkCode;
//

通过EmylinkUpcode加载EmylinkUlink(注意EmylinkUpcode.hbm.xml中声明了延迟加载)
Session session = EmylinkSessionFactory.currentSession();
         Query q 
= session.createQuery("from EmylinkUpcode where account='derek'");
                List l 
= q.list();
               
                   EmylinkUpcode eu 
=  (EmylinkUpcode)l.get(0);
                   System.out.println(eu.getPlinkName());
                   
//Hibernate.initialize(eu.getEmylinkUlinks());//当延迟加载lazy=true时,强制加载及联对象
                   
                   Set ul 
= eu.getEmylinkUlinks(); 
                                     
                   Iterator
<EmylinkUlink> it = ul.iterator();//lazy=true并且不使用强制加载,则此时才真真及联加载
                   
                   
while(it.hasNext()){
                    System.out.println(it.next().getLinkName());
                   }
 EmylinkSessionFactory.closeSession();  

* 延迟加载   
   如果lazy=true(延迟加载), 加载EmylinkUpcode时,hibernate不会立即加载EmylinkUlink,只有当(Iterator<EmylinkUlink> it = ul.iterator();)执行时,hibernate才加载EmylinkUlink实例; 如果在加载前就关闭session,则报异常LazyInitializationException ; 可以使用Hibernate.initialize(Object o)强制及联加载

*J2SE5.0的泛型

public void setEmylinkUlinks (java.util.Set<hbm.EmylinkUlink> emylinkUlinks) {
        
this.emylinkUlinks = emylinkUlinks;
    }
Iterator<EmylinkUlink> it = ul.iterator();                   
                   
while(it.hasNext()){
                    EmylinkUlink el 
= it.next();//无需进行强制类型转换
                    System.out.println(el.getLinkName());
                   }

emylinkUlinks集合中只能存放EmylinkUlink对象,从集合中获取对象无需再类型转换
 
struts+spring+hibernate关于hibernate中lazy="true"的问题。web.xml中用   

          <filter>   
                  
<filter-name>openSessionInViewFilter</filter-name>   
                  
<filter-class>org.springframework.orm.hibernate.support.OpenSessionInViewFilter</filter-class>   
          
</filter>   
    
          
<filter-mapping>   
                  
<filter-name>openSessionInViewFilter</filter-name>   
                  
<url-pattern>*.do</url-pattern>   
          
</filter-mapping>

  它的原理就是:打开页面的时候打开session,一直到页面装载完毕才关闭session,这样就解决了lazy="true"时session   is   closed的问题。
  关于OpenSessionInViewFilter 延迟加载失效问题 http://www.javaeye.com/topic/15057
          OpenSessionInView的效率问题  http://www.javaeye.com/topic/17501

posted on 2006-09-07 16:20 Derek.Guo 阅读(4323) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航:
 
MSN:envoydada@hotmail.com QQ:34935442