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.