`
lkj107
  • 浏览: 104893 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用Hibernate Synchronizer3.19集成spring和Hibernate3

    博客分类:
  • J2EE
阅读更多

原文地址:http://www.supergloo.com/blog/index.php/2006/11/16/using-hibernate-synchronizer-319-with-spring-and-hibernate-3/

本文指导你如何使用最新的 Hibernate Synchronizer 来与 spring 框架集成。(认为你已经安装好 Hibernate Synchronizer )。

有五种实现方法 , 下面介绍如何把 Hibernate3 spring 以及 Hibernate Synchronizer 。希望对你有所帮助,可以参照 Hibernate Synchronizer and Spring

第一步 更新某一项目的 Hibernate Synchronizer 设置

设置Hibernate Synchronizer设置使用用户的base root。

菜单: Project->Properties->Hibernate Synchronizer->Data Access Objects tab->选中 “I would like to use a custom root” 并输入 SpringBaseRootDAO 全路径 ; 例如com.codesponsors.data.dao.SpringBaseRootDAO.

import  org.hibernate.Criteria;
import  org.hibernate.HibernateException;
import  org.hibernate.Session;

import  org.springframework.dao.DataAccessException;
import  org.springframework.orm.hibernate3.HibernateTemplate;

/** */ /**
 * Based on Hibernate Synchronizer
 * For more information or documentation, visit The Hibernate Synchronizer page
 * at 
http://www.binamics.com/hibernatesync  or contact Joe Hudson at joe@binamics.com.
 * 
 * 
@author  Joe Hudson
 * 
@author  Todd McGrath mcgrath@supergloo.com
 
*/

public   abstract   class  SpringHibernate3BaseRootDAO  extends  HibernateTemplate  ... {
 
    
/** */ /**
     * Return the name of the configuration file to be used with this DAO or null if default
     
*/

    
public  String getConfigurationFileName ()  ... {
        
return   null ;
    }


    
/** */ /**
     * Return the specific Object class that will be used for class-specific
     * implementation of this DAO.
     * 
@return  the reference Class
     
*/

    
protected   abstract  Class getReferenceClass();

    
/** */ /**
     * Return a Criteria object that relates to the DAO's table
     
*/

     
protected  Criteria createCriteria (Session s)  throws  DataAccessException  ... {
         
return  s.createCriteria(getReferenceClass());
     }


    
/** */ /**
     * Return a Criteria object that relates to the DAO's table
     
*/

     
public  Criteria createCriteria ()  throws  HibernateException  ... {
        Session s 
=  getSessionFactory().openSession();
        
return  s.createCriteria(getReferenceClass());
     }
     

    
/** */ /**
     * Return the property of the class you would like to use for default ordering
     * 
@return  the property name
     
*/

    
public  String getDefaultOrderProperty ()  ... {
        
return   null ;
    }



}
;.更新 Hibernate Synchronizer 使用用户异常类. 同一个界面. 输入 “org.springframework.dao.DataAccessException” 在DAO异常输入框

第二步 更新所有项目的 Hibernate Synchronizer 全局设置

 

1. Eclipse 菜单, 点击Window->Preferences

2. 在左边控制板上点击Hibernate Synchronizer

3. 选择Snippets

4. 更新 Base DAO Snippets

a) 打开Base DAO
b) 替换action方法为

/** */ /**
     * Persist the given transient instance, first assigning a generated identifier. (Or using the current value
     * of the identifier property if the assigned generator is used.) 
     * 
@param  ${class.VarName} a transient instance of a persistent class 
     * 
@return  the class identifier
     
*/

    
public  $ ... { class .ParentRoot.Id.Property.ObjectClass}  save($ ... { class .AbsoluteValueObjectSignatureClassName}  $ ... { class .VarName} )
#
if  ($exceptionClass)
        
throws  $ ... {exceptionClass}   ... {
#
else
    
... {
#end
        
return  ($ ... { class .ParentRoot.Id.Property.ObjectClass} super .save($ ... { class .VarName} );
    }


    
/** */ /**
     * Either save() or update() the given instance, depending upon the value of its identifier property. By default
     * the instance is always saved. This behaviour may be adjusted by specifying an unsaved-value attribute of the
     * identifier property mapping. 
     * 
@param  ${class.VarName} a transient instance containing new or updated state 
     
*/

    
public   void  saveOrUpdate($ ... { class .AbsoluteValueObjectSignatureClassName}  $ ... { class .VarName} )
#
if  ($exceptionClass)
        
throws  $ ... {exceptionClass}   ... {
#
else
    
... {
#end
        
super .saveOrUpdate($ ... { class .VarName} );
    }



    
/** */ /**
     * Update the persistent state associated with the given identifier. An exception is thrown if there is a persistent
     * instance with the same identifier in the current session.
     * 
@param  ${class.VarName} a transient instance containing updated state
     
*/

    
public   void  update($ ... { class .AbsoluteValueObjectSignatureClassName}  $ ... { class .VarName}
#
if  ($exceptionClass)
        
throws  $ ... {exceptionClass}   ... {
#
else
    
... {
#end
        
super .update($ ... { class .VarName} );
    }



c) 替换finder方法为

# if  ($ class .ParentRoot.Id)
    
public  $ ... { class .AbsoluteValueObjectSignatureClassName}  load($ ... { class .ParentRoot.Id.Property.AbsoluteSignatureClassName}  key)
#
if  ($exceptionClass)
        
throws  $ ... {exceptionClass}   ... {
#
else
    
... {
#end
#
if  ($ class .ParentRoot.Id.Property.isPrimitive())
        
return  ($ ... { class .AbsoluteValueObjectSignatureClassName} ) load(getReferenceClass(),  new  $ ... { class .ParentRoot.Id.Property.ObjectClass} (key));
#
else
        
return  ($ ... { class .AbsoluteValueObjectSignatureClassName} ) load(getReferenceClass(), key);
#end
    }

#end

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics