1 2 3 4 5 |
org.hibernate.HibernateException: get is not valid without active transaction org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352) com.sun.proxy.$Proxy26.get(Unknown Source) |
org.hibernate.HibernateException: get is not valid without active transaction
The hibernate code snippet where the exception was getting thrown was:
1 2 3 4 5 |
Session session = sessionFactory.getCurrentSession(); //below line throws exception Employee emp = (Employee) session.get(Employee.class, empId); |
After some debugging I found out that we need to start the transaction to solve this problem. So I changed my above code snippet to;
1 2 3 4 5 6 |
Session session = sessionFactory.getCurrentSession(); Transaction tx = session.beginTransaction(); Employee emp = (Employee) session.get(Employee.class, empId); tx.commit(); |
And the problem was gone and everything was working fine. Further reading: Hibernate Tutorial for Beginners
I hope this information will save someone else time in debugging the root cause of the issue.