Abhilasha
Abhilasha

Reputation: 67

Database Configuration In Spring As Well As Hibernate config files

I am a newbie to Spring and Hibernate.And while browsing through projects, I found database details like connection-url, username, password in both hibernate and spring xml config files. I need to understand why we do so ?

Upvotes: 0

Views: 1575

Answers (2)

Killer
Killer

Reputation: 590

Yes. If you using both Hibernate and Spring then you can also try like that. You need both hibernate.cfg.xml for hibernate and ApplicationContext.xml for spring.

do like this, at first create your hibernate.cfg.xml. i.e

  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

 <session-factory>

    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/country</property>
    <property name="connection.username">root</property>
    <property name="connection.password">password</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">false</property>
    <property name="hibernate.current_session_context_class">thread</property>

    <mapping class="com.hibernate.test" />

 </session-factory>

</hibernate-configuration>

now create your ApplictionContext.xml,and add your hibernate.cfg.xml as properties in sessionFactory bean. i.e

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

      //package to scan for Annotated classes 
    <context:component-scan base-package="com.hibernate.spring" />

     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
         //add and locate the hibernate.cfg.xml here
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    </bean>
  </beans>

by this you only need to call Application.xml,the hibernate.cfg.xml will automatically loaded.

Upvotes: 1

Desorder
Desorder

Reputation: 1539

You have two options.

You can have hibernate to manage your database connections or you can have Spring managing your connections.

When you have Hibernate managing your connections, you only need to tell Spring where hibernate configuration is. Sorry, I couldn't find very easily an example where Hibernate manages the connections and Spring only "using" hibernate.

The other option is to use Spring to manage your connections and hbm files/annotated entities.

Here is a snippet from Spring docs

<beans>

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
        <property name="url" value="jdbc:hsqldb:hsql://localhost:9001"/>
        <property name="username" value="sa"/>
        <property name="password" value=""/>
    </bean>

    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <property name="mappingResources">
            <list>
                <value>product.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.HSQLDialect
            </value>
        </property>
    </bean>

</beans>

and here the full documentation

http://docs.spring.io/spring/docs/4.0.5.RELEASE/spring-framework-reference/htmlsingle/#orm-hibernate

Besides very specific problems, I can't a lot of differences on using either way but I would prefer to let Spring to manage my database connections and I can let Spring to deal transactions for example and have everything centralized in the same configuration file.

Upvotes: 0

Related Questions