2013-01-16 24 views
2

我最近开始使用hibernate和spring。什么是与弹簧一起使用休眠的正确方法

在开始时,我被告知使用sessionFactory和openSession与beginTransaction一起执行数据库调用。

后来我听说了dao的,所以我开始使用它创建一个接口,在类中实现这个接口,然后让这个类扩展HibernateDAOSupport。

我觉得这是相当稳定的,直到一位同事告诉我这种方式已被弃用,并且我不应该使用HibernateDAOSupport类。但是改为在我的dao实现类中使用SessionFactory实例,并在新的独立服务类中使用该类的实例。既然这看起来也是一个很好的方法,我决定走这条道路。

刚才我读到这个方法也被新版本的春天弃用了...... 所以我的问题是:究竟什么才是将冬眠和春天结合在一起的正确的最新方式? ?

我也听说过一个实体经理,那是什么?

我在寻找使用它们的一般方法,如果有一般规则的例外,请也提供这些例外的例子。

回答

4

这里有很多方法可以休眠融入一个春天......

您服务(或DAO)类看起来应该像这样简单: -

// annotate this class so that Spring is aware of it 
@Service 
public class EmployeeServiceImpl implements EmployeeService { 

    @Autowired 
    private SessionFactory sessionFactory; 

    // this is straight-up HQL... really, no magic here 
    @Override 
    @SuppressWarnings("unchecked") 
    public Collection<Employee> getAllEmployees() { 
     return sessionFactory.getCurrentSession() 
       .createQuery("from Employee e order by e.lastName, e.firstName") 
       .list(); 
    } 
} 

现在,你需要配置sessionFactory否则自动装配将失败。所以,这是你如何配置它: -

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:jee="http://www.springframework.org/schema/jee" 
     xsi:schemaLocation=" 
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
      http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd"> 

    <!-- 
     When creating the session factory, point it to your existing hibernate.cfg.xml. 
     You can also port your entire Hibernate configuration and HBM mappings here, but 
     for simplicity sake, I'll reference the existing hibernate.cfg.xml here so that we 
     are not cluttering Spring configuration file with Hibernate-specific configuration. 
    --> 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="configLocation" value="classpath:hibernate.cfg.xml"/> 
    </bean> 

    <!-- If you are running in production, you will want to use JNDI connection --> 
    <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/myapp"/> 

    <!-- If you are running testcases, you might want to use JDBC instead --> 
    <!-- 
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
     <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/> 
     <property name="url" value="jdbc:jtds:sqlserver://machine/myapp"/> 
     <property name="username" value="myapp"/> 
     <property name="password" value="myapp"/> 
    </bean> 
    --> 

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 

    <tx:advice id="txAdvice"> 
     <tx:attributes> 
      <tx:method name="*" propagation="REQUIRED"/> 
     </tx:attributes> 
    </tx:advice> 

    <aop:config proxy-target-class="true"> 
     <aop:advisor pointcut="execution(* com.myapp..*.*(..))" advice-ref="txAdvice"/> 
    </aop:config> 

</beans> 

有配置交易方式有两种:1)使用@Transactional,您可以签注,需要适当的事务处理,以及2)使用类(或特定的方法) AOP并用交易包装所有代码。

在我上面的示例中,我将包含我的所有代码与基本包com.myapp与交易。您可以更改(或添加)切入点来减少事务包装器。

+0

感谢您的详细答案,但有一个问题:您会直接使用带有sessionFactory的服务,而不是使用具有带有sessionFactory的DOA的服务?这样做的任何理由? – Mike

+0

这取决于你。我将会话工厂放在我的服务中以简化我的示例。 – limc

4

关于Spring中的Hibernate,至于2013,HibernateDaoSupport已被弃用(并且HibernateTemplate也是如此)。它在Hibernate 4.0中不起作用。

我觉得用@Transactional注释类与sessionFactory.getCurrentSession()对Hibernate具体@PersistenceContext注释EntityManager属性自动装配为JPA配置持久化上下文是当前的默认选择。我们可以看到JPA是一个标准,而Hibernate不是,但大多数Hibernate特定的东西都可以在JPA配置中工作(当然这意味着你不再遵守标准,而且切换到另一个JPA实现将会更加困难) ,只有少数可用于纯Hibernate,例如,您无法从会话prior to JPA 2.0中分离独立对象。在专有框架中实现一些新功能比改变标准更简单,但我相信JPA 2.0适用于大多数情况。

StackOveflow上有很多类似的问题123等等。

使用JPA配置的一个很好的参数是它可以与Spring Data JPA一起使用 - 这是一个简化实现存储库的框架(您只需要声明接口,而不是在接口和基于通用存储库的类之间保持并行层次结构)。