2013-01-16 25 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一起使用 - 這是一個簡化實現存儲庫的框架(您只需要聲明接口,而不是在接口和基於通用存儲庫的類之間保持並行層次結構)。