2012-04-09 30 views
3

這裏我面臨的一個問題是,我在application.xml文件中配置了hibernate sessionfactory,以及如何在HibDao類中獲取sessionFactory(用戶定義的類)爲使用戶友好像()開始,提交(),延伸的HibernateDaoSupport類回滾()..如何在我的班級中使用Spring進行會話工廠休眠

這裏是我的應用程序hibernate.xml文件:

<beans> 

<bean id="sessionFactory"  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
    <property name="configLocation" value="/WEB-INF/hibernate.cfg.xml" /> 
</bean> 



<!-- Transaction manager for a single Hibernate SessionFactory (alternative 
    to JTA) --> 
<bean id="transactionManager" 
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory"> 
     <ref local="sessionFactory" /> 
    </property> 
</bean> 


<!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= --> 

<!-- Data access object: Hibernate implementation. --> 

<bean id="HibernateSpringDaoTarget" class="com.netprofit.dao.HibernateSpringDAOImpl"> 
    <property name="sessionFactory"> 
     <ref local="sessionFactory" /> 
    </property> 
</bean> 

<!-- - Transactional proxy for Application's central data access object. 
    - - Defines specific transaction attributes with "readOnly" markers, - which 
    is an optimization that is particularly valuable with Hibernate - (to suppress 
    unnecessary flush attempts for read-only operations). - - Note that in a 
    real-life app with multiple transaction proxies, - you will probably want 
    to use parent and child bean definitions - as described in the manual, to 
    reduce duplication. --> 

<bean id="SpringHibernateDao" 
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
    <property name="transactionManager"> 
     <ref local="transactionManager" /> 
    </property> 
    <property name="target"> 
     <ref local="HibernateSpringDaoTarget" /> 
    </property> 
    <property name="transactionAttributes"> 
     <props> 
      <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> 
      <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> 
      <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop> 
      <prop key="store*">PROPAGATION_REQUIRED</prop> 
      <prop key="add*">PROPAGATION_REQUIRED</prop> 
     </props> 
    </property> 
</bean> 

這裏hibernate-cfg.xml文件:

  <?xml version="1.0" encoding="UTF-8"?> 
     <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
    <hibernate-configuration> 
<session-factory> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/netprofit</property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.connection.password">test</property> 
    <property name="hibernate.connection.autocommit">false</property> 
    <property name="hibernate.hbm2ddl.auto">update</property> 
    <property name="hibernate.show_sql">true</property> 
    <property name="hibernate.format_sql">true</property> 
    <property name="hibernate.jdbc.batch_size">50</property> 


    <property name="hibernate.c3p0.max_size">1</property> 
    <property name="hibernate.c3p0.min_size">0</property> 
    <property name="hibernate.c3p0.timeout">5000</property> 
    <property name="hibernate.c3p0.max_statements">1000</property> 

    <property name="hibernate.c3p0.idle_test_period">300</property> 
    <property name="hibernate.c3p0.acquire_increment">1</property> 


    <mapping class="com.netprofit.dao.hibernate.HUserRegistrationPojo" /> 
</session-factory> 

這裏是HibDao類(用戶定義的類爲簡單起見):

public class HibDao extends HibernateDaoSupport { 
private static final ThreadLocal THREAD = new ThreadLocal(); 

protected HibDao() { 
} 

public static Session session() throws Exception { 
    Session session = (Session) HibDao.THREAD.get(); 
    if (session == null) {    
     session = getSessionFactory().openSession(); **--->>>Here how can i open the Session** 
     HibDao.THREAD.set(session); 
     session().setFlushMode(FlushMode.COMMIT); 
    } 
    return session; 
} 

protected static void begin() throws Exception { 
    session().beginTransaction(); 

} 

protected static void commit() throws Exception { 
    session().getTransaction().commit(); 
} 

protected static void rollback() throws Exception { 
    session().getTransaction().rollback(); 
    session().close(); 
    HibDao.THREAD.set(null); 
} 

protected static void flush() throws Exception { 
    session().flush(); 
} 

protected static void close() throws Exception { 

    session().close(); 
    HibDao.THREAD.set(null); 
} 
} 

之後,我剛擴展HibDao類和使用begin(),提交()和rollback()方法來處理休眠事務......並且我對Spring不熟悉...... ......

謝謝....

回答

2

沒有指定具體問題(沒有評論欄)。我讀到這樣的問題 - 「我已經將SessionFactory bean注入到xml文件中了,現在我想在Java程序中對其進行回憶。」

簡單!

ApplicationContext context = new FileSystemXmlApplicationContext("c:/../application-hibernate.xml"); 
SessionFactory sessionFact = (SessionFactory) context.getBean("sessionFactory"); 

現在你可以這樣做:

Session session = sessionFact.openSession();  

注:我被直接解析XML文件獲取豆,它是不是一個好的做法。在理想情況下,您將從Servlet/Action類的請求對象中獲取ApplicationContext對象。

+0

感謝您的回覆,實際上我對Spring框架,這是最好的方法糾正上述問題沒有更多的想法... – 2012-04-09 09:33:41

+0

喜Rafeek,這是你的工作Web應用程序或獨立的Java項目與? – tusar 2012-04-09 09:35:51

+0

是他們的任何方式使用瓷磚或類似的彈簧jsf集成....我怎樣才能導航到瓷磚定義?我知道jsf(使用tomahawk)和spring提供了tile技術......哪一個更適合這種情況(spring-jsf),我怎樣才能映射結果類型..... ??? – 2012-04-09 09:37:41