You must enable the transaction support (<tx:annotation-driven>
or @EnableTransactionManagement
) and declare the transactionManager
and it should work through the SessionFactory
.
You must add @Transactional
into your @Repository
With @Transactional
in your @Repository
Spring is able to apply transactional support into your repository.
Your Student
class has no the @javax.persistence.*
annotations how @Entity
, I am assuming the Mapping Configuration for that class has been defined through XML.
I have had the same issue, but in a class that was not a part of the service layer. In my case, the transaction manager was simply obtained from the context by the getBean()
method, and the class belonged to the view layer - my project utilizes OpenSessionInView
technique.
The sessionFactory.getCurrentSession()
method, has been causing the same exception as the author's. The solution for me was rather simple.
Session session;
try {
session = sessionFactory.getCurrentSession();
} catch (HibernateException e) {
session = sessionFactory.openSession();
}
If the getCurrentSession()
method fails, the openSession()
should do the trick.
Add the annotation @Transactional of spring in the class service
In your xyz.DAOImpl.java
Do the following steps:
//Step-1: Set session factory
@Resource(name="sessionFactory")
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf)
{
this.sessionFactory = sf;
}
//Step-2: Try to get the current session, and catch the HibernateException exception.
//Step-3: If there are any HibernateException exception, then true to get openSession.
try
{
//Step-2: Implementation
session = sessionFactory.getCurrentSession();
}
catch (HibernateException e)
{
//Step-3: Implementation
session = sessionFactory.openSession();
}
I added these configuration in web.xml and it works well for me!
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Additionally, the most ranked answer give me clues to prevent application from panic at the first run.
You need to allow transaction to your DAO method. Add,
@Transactional(readOnly = true, propagation=Propagation.NOT_SUPPORTED)
over your dao methods.
And @Transactional
should be from the package:
org.springframework.transaction.annotation.Transactional
I had this error too because in the file where I used @Transactional
annotation, I was importing the wrong class
import javax.transaction.Transactional;
Instead of javax, use
import org.springframework.transaction.annotation.Transactional;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With