使用Hibernate Synchronizer3.19集成spring和Hibernate3

来源:互联网 发布:淘宝九块九秒杀是什么 编辑:程序博客网 时间:2024/05/29 21:33

本文指导你如何使用最新的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

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

#end

#if ($class.ParentRoot.Id)
    public java.util.List loadAll()
#if ($exceptionClass)
        throws ${exceptionClass} {
#else
    {
#end
        return loadAll(getReferenceClass());
    }

#end


d) 替换为下面的导入:

import ${class.DAOPackage}.${class.DAOClassName};

e) 替换 Required方法为(从snippets去掉JRE 1.5 特有的属性)

    public Class getReferenceClass () {
        return ${class.AbsoluteValueObjectClassName}.class;
    }

#if ($class.ConfigFile)

    /**
     * Return the name of the configuration file to be used with this DAO
     
*/

    public String getConfigurationFileName () {
        return "${class.ConfigFile}";
    }

#end


    /**
     * Cast the object as a ${class.AbsoluteValueObjectSignatureClassName}
     
*/

    public ${class.AbsoluteValueObjectSignatureClassName} cast (Object object) {
        return (${class.AbsoluteValueObjectSignatureClassName}) object;
    }

f) 类的构造函数替换为:

public ${class.BaseDAOClassName} () {}

5) 更新 Root DAO Snippets
a) 打开Root DAO
b) 构造函数替换为:

public _RootDAO () {}

6) 更新 Base Root DAO Snippets
a) 打开Base Root DAO
b) 构造函数替换为:

public _BaseRootDAO () {}

7) 更新DAO Snippets
a) 打开DAO
b) 类定义替换为:

public class ${class.DAOClassName} extends ${class.BaseDAOClassName} {

c) 类构造函数替换为:

public ${class.DAOClassName} () {}

第三步生成配置Hibernate mapping文件

1) 在类路径生成Hibernate Mapping 文件. 参考文章: Generating a Hibernate Mapping file with Hibernate Synchronizer in Eclipse .

2) 打开 Hibernate Mapping文件修改元素 sync-DAO 属性 false 为true

3) 右键点击Hibernate Mapping 文件, 选择 Hibernate Synchronizer -〉Synchronize Files. 在资源文件夹里对应包路径下生成java文件.

第四步删除接口

删除生成的借口文件。例如,如果base包是com.codesponsors.data,删除com.codesponsors.data.dao.iface包。

虽然使用接口和实现由众多的优点. 但是, 借口已经在 DAO 模式中被滥用尤其在使用 Hibernate时. 概念上, Hibernate是实现,DAO 类是接口. Hibernate Synchronizer 最新版本中的接口在我的项目中没有发挥作用,这个问题以后在讨论

第五步配置spring文件

1)配置DAO(s) 为Hibernate模板的 “sessionFactory” 属性加入 LocalSessionFactory (SpringBaseRootDAO 扩展 Hibernate模板). 例如,  applicationContext.xml 可能是

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="mappingResources">
        <list>
            <value>Users.hbm.xml</value>
        </list>
    </property>        
    <property name="hibernateProperties">
        <props>    
            <!-- prop key="hibernate.connection.pool_size"></prop -->
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.use_outer_join">true</prop>
            <prop key="hibernate.connection.release_mode">on_close</prop>
            <!-- <prop key="hibernate.cglib.use_reflection_optimizer">false</prop> -->
        </props>
    </property>        
   <property name="dataSource">
          <ref bean="dataSource"/>
   </property>
</bean>

<bean id="usersDataAccessObject" class="com.codesponsors.data.dao.UsersDAO">
    
<property name="sessionFactory><ref local="mySessionFactory"/></property>
</bean>


转载地址:http://www.supergloo.com/blog/index.php/2006/11/16/using-hibernate-synchronizer-319-with-spring-and-hibernate-3/
0 0
原创粉丝点击