The moose likes Object Relational Mapping and the fly likes hibernate_springs Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply Bookmark "hibernate_springs" Watch "hibernate_springs" New topic
Author

hibernate_springs

subha nair
Greenhorn

Joined: Jun 19, 2008
Posts: 23
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

Joined: Aug 26, 2006
Posts: 4967

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


Author of Hibernate Made Easy, What is WebSphere???, JSF 2.0 Made Easy and the SCJA Certification Guides
subha nair
Greenhorn

Joined: Jun 19, 2008
Posts: 23
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

Joined: Jun 19, 2008
Posts: 23
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

Joined: Nov 27, 2007
Posts: 22
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

Joined: Jun 19, 2008
Posts: 44
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.





Thanks in Advance,
Kash.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel
 
subject: hibernate_springs
 
Similar Threads
struts vs spring framework
EJB 3.0 and Hibernate
Getting GenericJDBCException
Why two xml files?
Hibernate Interview Questions