-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Hibernate and Spring Integration problem
PostPosted: Fri Oct 16, 2009 6:24 am 
Beginner
Beginner

Joined: Thu Dec 11, 2008 2:30 am
Posts: 47
hi all,

I tried to integrate hibernate with Spring and still trying it. I m using hibernate 3 and Spring 2. my application server is jboss and my DB is sql server. Ill not go further details but post my code here. If anyone can give me a solution, ill be very thankful.

I use annotations here.

this is my spring configuration file and its name is application_config.xml and it is in /src directory.

Code:
<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="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton">
      <property name="jndiName" value="jdbc/sample" />
      <property name="resourceRef" value="true" />
   </bean>

   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="dataSource" ref="dataSource"></property>
      <property name="annotatedClasses">
         <list>
            <value>com.trn.Caller.java</value>
            <value>com.trn.Passenger.java</value>
            <value>com.trn.Question.java</value>
            <value>com.trn.CallerQuestions.java</value>
         </list>
      </property>
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
         </props>
      </property>
   </bean>
   
   <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
       <property name="mySessionFactory">
           <ref bean="sessionFactory"/>
       </property>
   </bean>
   
   <bean id="callerDao" class="com.dao.CallerDAOImpl">
       <property name="hibernateTemplate">
           <ref bean="hibernateTemplate"/>   
       </property>
   </bean>
   
   
</beans>



here is my code for accessing those beans.

Code:
package com.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.dao.CallerDAO;
import com.dao.CallerDAOImpl;
import com.trn.Caller;

/**
* Servlet implementation class ControllerServlet
*/
public class ControllerServlet extends HttpServlet {
   private static final long serialVersionUID = 1L;

   

   /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      PrintWriter out = response.getWriter();
      
      Resource resource = new ClassPathResource("application_config.xml");
      BeanFactory factory = new XmlBeanFactory(resource);
      
      out.println(resource.toString());
      String userAction  = request.getParameter("useraction");
      out.println(userAction);
      String firstName = request.getParameter("firstname");
      String familyName = request.getParameter("familyname");
      String street = request.getParameter("streetadr");
      String city = request.getParameter("city");
      String postal = request.getParameter("postalcode");
      String country = request.getParameter("country");
      String primaryPhone = request.getParameter("primaryphone");
      String alternatePhone = request.getParameter("altphone");
      String email = request.getParameter("email");
      String age = request.getParameter("callerage");
      String nationality = request.getParameter("nationality");
      String otherInfo = request.getParameter("otherinfo");
      
      if(userAction.equals("add")){
         Caller caller = new Caller();
         CallerDAOImpl callerDAOImpl = (CallerDAOImpl) factory.getBean("callerDao");
         
         caller.setCallerId(1);
         caller.setFirstname(firstName);
         caller.setFamilyname(familyName);
         caller.setStreet(street);
         caller.setCity(city);
         caller.setPostal(postal);
         caller.setCountry(country);
         caller.setPrimaryPhone(primaryPhone);
         caller.setAlternatePhone(alternatePhone);
         caller.setEmail(email);
         caller.setAge(age);
         caller.setNationality(nationality);
         caller.setOtherInfo(otherInfo);
         
         callerDAOImpl.addCaller(caller);
      }
      
   }

}



this is my jboss-web.xml file. it is in applications WEB-INF folder

Code:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
   <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/sample</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <jndi-name>java:comp/jdbc/sample</jndi-name>
      <res-auth>Container</res-auth>
   </resource-ref>
</jboss-web>



this is my web.xml file

Code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   <display-name>
   Sample_Project1</display-name>
   <servlet>
      <description>
      </description>
      <display-name>
      ControllerServlet</display-name>
      <servlet-name>ControllerServlet</servlet-name>
      <servlet-class>
      com.util.ControllerServlet</servlet-class>
   </servlet>
   <servlet-mapping>
      <servlet-name>ControllerServlet</servlet-name>
      <url-pattern>/ControllerServlet</url-pattern>
   </servlet-mapping>
   <welcome-file-list>
      <welcome-file>index.html</welcome-file>
      <welcome-file>index.htm</welcome-file>
      <welcome-file>index.jsp</welcome-file>
      <welcome-file>default.html</welcome-file>
      <welcome-file>default.htm</welcome-file>
      <welcome-file>default.jsp</welcome-file>
   </welcome-file-list>
   <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/sample</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
   </resource-ref>
</web-app>



this is mssql-ds.xml file inside server\default\deploy directory in jboss distribution.
Code:
<?xml version="1.0" encoding="UTF-8"?>


<datasources>
<local-tx-datasource>

   <jndi-name>jdbc/sample</jndi-name>

   <connection-url>jdbc:jtds:sqlserver://ipOfDBServer;DILAN_TEST;</connection-url>
   <driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
   <user-name>sa</user-name>
   <password>bdcadmin</password>
   <min-pool-size>1</min-pool-size>
   <max-pool-size>5</max-pool-size>
</local-tx-datasource>

</datasources>


All libraries are inside relevant folders. But it gives this error inside this ControllerServlet.java

Code:
1. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'callerDao' defined in class path resource [application_config.xml]: Cannot resolve reference to bean 'hibernateTemplate' while setting bean property 'hibernateTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in class path resource [application_config.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'mySessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [application_config.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder

2. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in class path resource [application_config.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'mySessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [application_config.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder

3. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [application_config.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder

4. org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
   org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:115)

5. java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder

6. java.lang.ClassNotFoundException: No ClassLoaders found for: org.slf4j.impl.StaticLoggerBinder

thanks in advance,
dil.





Top
 Profile  
 
 Post subject: Re: Hibernate and Spring Integration problem
PostPosted: Fri Oct 16, 2009 7:19 am 
Beginner
Beginner

Joined: Thu Dec 11, 2008 2:30 am
Posts: 47
ok now I have solved some issues here,

1. I removed slf4j-api-1.5.2.jar from lib and add

slf4j-api-1.5.6.jar and
slf4j-log4j12-1.5.6.jar

and I also add one line to my mssql-ds.xml file, that is

<use-java-context>false</use-java-context>


ok now I have two different errors,

1. java.lang.ClassNotFoundException: com.trn.Caller.java
2. java.lang.IllegalArgumentException: Cannot find class [com.trn.Caller.java]

and earlier 1, 2, 3 errors (these are error numbers which I mentioned earlier post)
so im still finding a solution.

please help me.

regards,
dil.


Top
 Profile  
 
 Post subject: Re: Hibernate and Spring Integration problem
PostPosted: Fri Oct 16, 2009 7:47 am 
Beginner
Beginner

Joined: Thu Dec 11, 2008 2:30 am
Posts: 47
Ok no reply, but ill reply myself........

ok,

Code:
<property name="annotatedClasses">
         <list>
            <value>com.trn.Caller.java</value>
            <value>com.trn.Passenger.java</value>
            <value>com.trn.Question.java</value>
            <value>com.trn.CallerQuestions.java</value>
         </list>
      </property>


I changed above code snippet like this..

Code:
<property name="annotatedClasses">
         <list>
            
            <value>com.trn.Caller</value>
            <value>com.trn.Passenger</value>
            <value>com.trn.Question</value>
            <value>com.trn.CallerQuestions</value>
         </list>
      </property>


just removed extension. so now there is a brand new problem....
here it is.

"org.hibernate.AnnotationException: No identifier specified for entity: com.trn.Caller"

and error number 1, 2, 3 are still same...

any help?

regards,
Dil.


Top
 Profile  
 
 Post subject: Re: Hibernate and Spring Integration problem
PostPosted: Wed Oct 21, 2009 10:55 pm 
Newbie

Joined: Wed Oct 21, 2009 6:24 pm
Posts: 3
Hi,
If you still have this issue.please below solution...
Please use @ID in your entity class just before getter method for below method..

Caller.java

private int callerId;
//setter
@Id
//getter
..


Top
 Profile  
 
 Post subject: Re: Hibernate and Spring Integration problem
PostPosted: Tue Dec 22, 2009 6:53 pm 
Newbie

Joined: Wed Dec 09, 2009 1:04 pm
Posts: 3
Take a look at Spring and Hibernate integration

Ahmad Reze Seddighi
Enterprise Software Designer and Architect
Author of Spring Persistence with Hibernate
Homepage: http://www.ahmadseddighi.com


Top
 Profile  
 
 Post subject: Re: Hibernate and Spring Integration problem
PostPosted: Tue Dec 22, 2009 11:39 pm 
Beginner
Beginner

Joined: Thu Dec 11, 2008 2:30 am
Posts: 47
Quote:
Take a look at Spring and Hibernate integration

Ahmad Reze Seddighi
Enterprise Software Designer and Architect
Author of Spring Persistence with Hibernate
Homepage: http://www.ahmadseddighi.com


This book is amazing, everything you want to find about Spring and Hibernate integration, use this book. I highly recommend this one and it is very useful.

I found this book before 'Ahmad Reze Seddighi' posted above post.

Regards,
Dil.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.