• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate_springs

 
subha nair
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have developed a simple application in spring.
I wanted to add hibernate capabilities to it.
Can you tell how many files are needed.
I am using hibernate annotations.
do I have to write hibernate.cfg.xml and applicationcontext.xml
please help me
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spring provides various templates for interacting with Hibernate, but Java is Java. From any line of Java code, be it a spring managed bean, or s JSP (not recommended of course!), you can call Hibernate classes and methods. All you need is the required jar files on the classpath of the classloader that invokes them. Sometimes this is as easy as putting the hibernate core jar file and the jar files associated with Hibernate Annotations and JPA annotations in the lib directory of the war file.

Have fun, and keep asking questions!

-Cameron McKenzie
 
subha nair
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your valuable help..

I am facing a different kind of problem.

I am writing a spring application and has not tied hibernate to it.
The application runs fine displaying the pages.
but when I add the below lines to my web-xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>


It is showing the error.
description The requested resource (/SpringWeb/) is not available.

I think it is not able to locate the applicationcontext.xml.
Without locating that I dont think I can go with adding hibernate to it.
Please help me out..This is very urgent
 
subha nair
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I have sorted this out. Now it's not giving error.

I have added hibernate to the existing application.

applicationContext.xml
*************************

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/samplesdb</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>

<property name="mappingResources">
<list>
<value>Staff.hbm.xml</value>
</list>
</property>

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>

<bean id="transactionManager"
class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>

<bean id="StaffDAO" class="com.palnar.college.DAO.StaffDAOHibernate">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>


StaffDAOHibernate
*******************


import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.palnar.college.service.StaffReg;

public class StaffDAOHibernate extends HibernateDaoSupport implements StaffDAO{

public List getStaff() {
return getHibernateTemplate().find("from StaffReg");
}

public void saveRecord(StaffReg staffreg) {
getHibernateTemplate().saveOrUpdate(staffreg);
}

}


When I am running it is giving the below error

org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.hibernate.LocalSessionFactoryBean] for bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate.LocalSessionFactoryBean


Please help me out
 
Rajan Nath
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just check the spring jars are available in the lib of your war file.
One more thing due to listner in web.xml file, ApplicationContext.xml loaded in application. But you can write all the configuration details of context file, in -servlet.xml file, and with out listener details can be loaded in application.
 
Kashif Mughal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you should check the version of the hibernate you are using if its 3.2 then you need to update the SessionFactory Bean in application Context file.



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic