Exception: java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session
Spring has dropped HibernateDaoSupport/HibernateTemplate from their Hibernate4 support package.
So anyone of you who is using above classes with Hibernate4 will get a java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session.
The simple solution for this is to, directly use hibernate classes instead of above mentioned Spring classes. This is illustrated in the following example.
Ex:
@Repository
public class UserDAOImpl implements UserDAO {
@Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Transactional
public void saveUser(User user) {
sessionFactory.getCurrentSession().saveOrUpdate(user);
}
@Transactional
public List<User> listUsers() {
return sessionFactory.getCurrentSession().createCriteria(User.class).list();
}
}
Here @Transactional spring annotation added to the data base access methods for transaction handling. For example if a runtime exception is thrown in middle of a data base access operation the changes done within the transaction will be rolled back in the data base.
When using @Transactional one needs to add following jars to the lib folder and to the class path.
1. aopalliance.jar
2. spring-aop-3.2.1.jar
And should add following tags to the configuration file
1. For beans <beans xsi:schemaLocation="..."> property append following
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
Ex:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
2. Add following tags after the sessionFactory bean definition
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<context:annotation-config/>
<context:component-scan base-package="example" />
Note: Any miss configuration will throw an exception: No session found for current thread
Exception: java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider
If you use org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean with Hibernate4,
Ex:
<bean id="sessionFactoryId" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
you will end up with a java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider exception.
Reason for this is CacheProvider class is removed from Hibernate4. So you should use org.springframework.orm.hibernate4.LocalSessionFactoryBean instead.
EX:
<bean id="sessionFactoryId" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
Exception: java.lang.NoSuchFieldError: TRACE
This is due to a class confliction caused by log4j-1.2.8.jar. Removing it from the lib folder and class path will solve this issue.
great.. that was very helpful.
ReplyDeleteCheers
It's a pleasure know this was helpful :)
Deletei spent around 5 hours to set up this transaction working environment.But, was no luck. Thank you so much..with this i could set up..& working fine.....!
ReplyDeleteYou are welcome and thanks for letting me know
Delete