views:

173

answers:

2

Hi. I am developing a java web application using hibernate as ORM. Is it possible to merge Hibernate.cfg.xml with the applicaion-config.xml?

+1  A: 

Hi,

It is possible because, behind the scenes, when you set up a SessionFactory as follows (Let's merge both hibernate.cfg.xml and applicationContext.xml properties)

<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 will call

// 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();

Takes care LocalSessionFactoryBean API says:

Configuration settings can either be read from a Hibernate XML file, specified as "configLocation", or completely via this class. A typical local configuration consists of one or more "mappingResources", various "hibernateProperties" (not strictly necessary), and a "dataSource" that the SessionFactory should use. The latter can also be specified via Hibernate properties, but "dataSource" supports any Spring-configured DataSource, instead of relying on Hibernate's own connection providers.

regards,

Arthur Ronald F D Garcia
A: 

You can use spring with hibernate. You just need to make a bean for your hibernate configuration and load it using aop or bean name into your application startup.

Here is a tutorial : http://www.roseindia.net/struts/hibernate-spring/spring-context.shtml and another one : http://www.ibm.com/developerworks/web/library/wa-spring2/

madicemickael