2009-12-15 137 views
2

我正在開發一個使用hibernate作爲ORM的java web應用程序。是否有可能將Hibernate.cfg.xml與applicaion-config.xml合併?休眠配置Xml

+0

從Spring應用程序-confif ??? – 2009-12-15 05:25:01

+0

是的。從春天開始 – cedric 2009-12-15 05:28:08

回答

2

這是可能的,因爲,在幕後,當你建立了一個SessionFactory如下背後(讓我們雙方合併的hibernate.cfg.xml和applicationContext.xml的屬性)

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
    <property name="configLocation" value="classpath:hibernate.cfg.xml"/> 
    <property name="mappingResources"> 
     <list> 
      <value>br/com/myApp/domain/User.hbm.xml</value> 
      <value>br/com/myApp/domain/Group.hbm.xml</value> 
     </list> 
    </property> 
</bean> 

Spring將調用

// configure load hibernate.cfg.xml from the classpath 
Configuration configuration = new Configuration().configure("hibernate.cfg.xml"); 

configuration.addResource("br/com/myApp/domain/User.hbm.xml"); 
configuration.addResource("br/com/myApp/domain/Group.hbm.xml"); 

SessionFactory sessionFactory = configuration.buildSessionFactory(); 

照顧LocalSessionFactoryBean API說:

配置設置可以爲r從Hibernate XML文件中讀取,指定爲「configLocation」,或完全通過此類。一個典型的本地配置由一個或多個「mappingResources」,各種「hibernateProperties」(非嚴格必要)和SessionFactory應使用的「dataSource」組成。後者也可以通過Hibernate屬性來指定,但「dataSource」支持任何Spring配置的DataSource,而不是依賴於Hibernate自己的連接提供者。

問候,