Announcement Announcement Module
Collapse
No announcement yet.
Problem with Spring and Hibernate. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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.

    <CODE>

    -----------------
    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>
    <!-- Database connection settings -->
    <property name="connection.driver_class">com.microsoft.sqlse 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="hbm2ddl.auto">create</property>-->

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

    </session-factory>

    </hibernate-configuration>

    ------------
    Book.hbm.xml
    ------------

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

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

    ---------
    Book.java
    ---------

    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) {
    this.id = id;
    }

    public String getBookName() {
    return bookName;
    }

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

    ------------------
    HibernateUtil.java
    ------------------

    package util;

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

    public class HibernateUtil
    {

    private static final SessionFactory sessionFactory;

    static
    {
    try
    {
    // 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 TradeManager.java
    -------------------------------

    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();
    mgr.createAndStoreEvent("Book");
    HibernateUtil.getSessionFactory().close();
    }

    // Method to store data into DB

    private void createAndStoreEvent(String strTable) {

    // Creating the session

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

    // Inserting into the BOOK table

    if(strTable.equalsIgnoreCase("Book")){
    System.out.println("Inside BOOK");
    Book objBook = new Book();
    objBook.setBookName("AA pricing");
    System.out.println("SAVINGGGG");
    session.save(objBook);
    }

    session.getTransaction().commit();
    }
    }


    </CODE>

    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

    --------------------
    spring-hibernate.xml
    --------------------

    <?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-2.0.xsd">

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" >
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDrive 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>

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

    </beans>


    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.

    Cheers,
    aaa

  • #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.

    Thanks,
    Mark

    Comment

    Working...
    X