J2EE之巅

 

Spring2.0与Hibernate3整合

                                                      蔡超

北京天融信,软件架构师

SUN certified Enterprise Architect

Microsoft certified Solution Developer

IBM certified RUP Specialist

联系方式 :cai_chao@topsec.com.cn,chaocai2001@yahoo.com.cn

010-82776427

 

Sping2 Hibernate3 都是如今流行的请量级框架,如何将两者进行整合呢,目前很多资料讨论的都是老版本的整合方式,下面给出这两种框架的新版整合方式。

1 配置数据源

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

    <property name="driverClassName">

        <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>

    </property>

    <property name="url">

        <value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=webguide</value>

    </property>

    <property name="username">

        <value>sa</value>

    </property>

    <property name="password">

        <value>talent</value>

    </property>

   </bean>

2 配置 Hibernate SessionFactory

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

       <property name="mappingResources">

       <!—Hibernate 映射文件列表 -->

           <list>

               <value>Greeting.hbm.xml</value>

           </list>

       </property>

       <property name="hibernateProperties">

           <props>

               <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>

                <prop key="hibernate.show_sql">true</prop>

                <prop key="hbm2ddl.auto">update</prop>

           </props>

       </property>

       <property name="dataSource">

           <ref bean="myDataSource"/>

       </property>

      

   </bean>

3 配置事务

  <bean id="myTranAttri" class="org.springframework.transaction.interceptor.DefaultTransactionAttribute">

    <property name="propagationBehaviorName">

        <value>PROPAGATION_REQUIRED</value>

    </property>

    </bean>

    <bean id="transactionAttributeSource" class="org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource">

    <property name="transactionAttribute">

        <ref bean="myTranAttri"/>

    </property>

    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

         <property name="sessionFactory">

            <ref bean="mySessionFactory"/>

        </property>

    </bean>

          

    <bean id="txInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor" lazy-init="true">

       <property name="transactionManager">

           <ref bean="transactionManager"/>

       </property>

       <property name="transactionAttributeSource">

           <ref bean="transactionAttributeSource"/>

       </property>

    </bean>

 

4 使用 Hibernate 实体

方法一:采用 AOP 的方式 package hibernate.integration.entity;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.orm.hibernate3.SessionFactoryUtils;

 

/**

 * @author chao cai

 *

 * TODO To change the template for this generated type comment go to

 * Window - Preferences - Java - Code Style - Code Templates

 */

public class GreetingLogic3 implements IGreetingLogic{

    private SessionFactory sessionFactory;

    /* (non-Javadoc)

     * @see hibernate.integration.entity.IGreetingLogic#loadGreeting(java.lang.Integer)

     */

    public Greeting loadGreeting(Integer id) {

       Session session = sessionFactory.getCurrentSession();

       Greeting greeting=(Greeting) session.get(Greeting.class,id);

       return greeting;

   

    }

 

 

    /**

     * @return Returns the sessionFactory.

     */

    public SessionFactory getSessionFactory() {

       return sessionFactory;

    }

    /**

     * @param sessionFactory The sessionFactory to set.

     */

    public void setSessionFactory(SessionFactory sessionFactory) {

       this.sessionFactory = sessionFactory;

    }

}

代码样例:

package hibernate.integration.entity;

 

/**

 * @author chao cai

 *

 * TODO To change the template for this generated type comment go to

 * Window - Preferences - Java - Code Style - Code Templates

 */

public interface IGreetingLogic {

    public Greeting loadGreeting(Integer id);

}

 

配置

    <bean id="myGreetingLogic" class="org.springframework.aop.framework.ProxyFactoryBean">

        <property name="proxyInterfaces">

            <value>hibernate.integration.entity.IGreetingLogic</value>

        </property>

        <property name="interceptorNames">

            <list>

                <value>txInterceptor</value>

                <value>myHibernateInterceptor</value>

             

            </list>

        </property>

        <property name="target">

         <ref bean="greetingLogic3"/>

        </property>

</bean>

客户端代码:

AbstractApplicationContext context=new FileSystemXmlApplicationContext("spring-hibernate.xml");

IGreetingLogic gl=(IGreetingLogic) context.getBean("myProductDao");

       greeting=gl.loadGreeting(new Integer(1));

       System.out.println(greeting.getGreeting());

 

方法二:采用 HibernateDaoSupport

代码:

package hibernate.integration.entity;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.orm.hibernate3.SessionFactoryUtils;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

 

/**

 * @author Chao Cai

 *

 * TODO To change the template for this generated type comment go to

 * Window - Preferences - Java - Code Style - Code Templates

 */

public class GreetingLogic extends HibernateDaoSupport implements IGreetingLogic{

   

    public Greeting loadGreeting(Integer id){

       Session session = SessionFactoryUtils.getSession(getSessionFactory(), false);

       Greeting greeting=(Greeting) session.get(Greeting.class,id);

       return greeting;

      

    }

}

    <bean id="greetingLogic" class="hibernate.integration.entity.GreetingLogic">

          <property name="sessionFactory">

            <ref bean="mySessionFactory"/>

        </property>

    </bean>

posted on 2007-02-05 17:15 超越巅峰 阅读(6492) 评论(5)  编辑  收藏 所属分类: Java EE

评论

# re: Spring2.0与Hibernate3整合 2007-02-05 20:19 anikin

为何不用<aop:config>和<tx:advice>来配置事务呢?难道这是你说的老版本?  回复  更多评论   

# re: Spring2.0与Hibernate3整合 2007-02-05 21:53 Anubis

先保存,有空再学习  回复  更多评论   

# re: Spring2.0与Hibernate3整合[未登录] 2007-02-05 22:45 role0523

Spring 2.0增加了很多Schema的标记,用起来比以前顺手多了。
事务声明中使用<aop:config>和<tx:advice>可以节省很多笔墨。  回复  更多评论   

# re: Spring2.0与Hibernate3整合 2007-02-06 09:13 learner

既然都是spring 2.0了 没有体现aop的强大 感觉题目有些过了  回复  更多评论   

# re: Spring2.0与Hibernate3整合 2007-02-06 09:22 看客

晕了`~就这  回复  更多评论   


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


网站导航:
 

导航

统计

常用链接

留言簿(12)

随笔分类(54)

随笔档案(59)

文章分类(2)

文章档案(1)

相册

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜