Announcement Announcement Module
No announcement yet.
Problem with Spring and Hibernate. Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with Spring and Hibernate.

    Hi All,

    I'm very new to Hibernate and Spring. I'm using hibernate-3.2 and spring-framework-2.0.7. My db is SQLServer 2005. I've a small application which tries to insert 2 columns (Id and BookName) into a Table BOOK.

    I was able to insert data using hibernate. Now I want to use spring also. Pls find my code below.



    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

    <!-- Database connection settings -->
    <property name="connection.driver_class"> rver.jdbc.SQLServerDriver </property>
    <property name="connection.url">jdbc:sqlserver://GBLOND088SFL\SQLSERVER2005;DatabaseName=TradeServi ce;SelectMethod=cursor</property>
    <property name="connection.username">tradeservice</property>
    <property name="connection.password">abnamro1</property>
    <property name="connection.autocommit">true</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.SQLServerDial ect</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Disable the second-level cache -->
    <property name="cache.provider_class">org.hibernate.cache.No CacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <!--<property name="">create</property>-->

    <!-- Mapping file -->
    <mapping resource="tradeservice/Book.hbm.xml"/>




    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    <class name="tradeservice.Book" table="book">
    <id name="id" column="idBook">
    <generator class="increment"/>
    <property name="bookName" column="BookName"/>


    package tradeservice;

    // Class for the BOOK table

    public class Book{
    private int id;
    private String bookName;

    public Book(){

    public int getId() {
    return id;

    public void setId(int id) { = id;

    public String getBookName() {
    return bookName;

    public void setBookName(String strBookName){
    this.bookName = strBookName;


    package util;

    import org.hibernate.*;
    import org.hibernate.cfg.*;

    public class HibernateUtil

    private static final SessionFactory sessionFactory;

    // Create the SessionFactory from hibernate.cfg.xml
    sessionFactory = new Configuration().configure().buildSessionFactory();
    catch (Throwable ex)
    // Make sure you log the exception, as it might be swallowed
    System.err.println("Initial SessionFactory creation failed." + ex);
    throw new ExceptionInInitializerError(ex);

    public static SessionFactory getSessionFactory()
    return sessionFactory;


    My main class

    package tradeservice;

    //Importing the required packages

    import org.hibernate.Session;
    import java.util.List;
    import java.util.Date;
    import util.HibernateUtil;

    //Main class

    public class TradeManager {

    public static void main(String[] args) {

    TradeManager mgr = new TradeManager();

    // Method to store data into DB

    private void createAndStoreEvent(String strTable) {

    // Creating the session

    Session session = HibernateUtil.getSessionFactory().getCurrentSessio n();

    // Inserting into the BOOK table

    System.out.println("Inside BOOK");
    Book objBook = new Book();
    objBook.setBookName("AA pricing");



    The above code is working fine with hibernate. Now when I try to use spring also I've done the following.

    1) Created spring-hibernate.xml


    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns=""

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" >
    <property name="driverClassName" value=" r"/>
    <property name="url" value="jdbc:sqlserver://GBLOND088SFL\SQLSERVER2005;DatabaseName=TradeServi ce;SelectMethod=cursor"/>
    <property name="username" value="tradeservice"/>
    <property name="password" value="abnamro1"/>

    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="mappingResources">


    I've the following doubts. It may be very silly to you experts but I dont have no other option other than posting here

    1) As I've given the db connection details here can I delete those from hibernate.cfg.xml?
    2) I tried the above part but I was getting the error "hibernate config file is missing and should have a db connection defined".
    3) Do I need to change the java files or can I keep my old code?
    4) I dont understand the link between the spring and hibernate

    Pls help. Any help will be highly appreciated.

    Thnx in advance.


  • #2
    Please note that you are posting in the "Spring Integration" forum which is intended for discussion related to the Spring Integration project.

    You should be posting in the data access forum, and also please do not double post.