Hi, I'm new to Hibernate. Using version 3 and having the following issue-
I'm building out from my data model and running unit tests via JUnit via Ant.
I used the Hibernate tools for eclipse to generate my model classes and DAOs (side question - the tool generated all of the DAOs named PersonHome, AddressHome. Why not PersonDAO, etc?).
When I ran my first unit test, I got the following error:
Code:
No CurrentSessionContext configured!
org.hibernate.HibernateException: No CurrentSessionContext configured!
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:542)
So I did a google search and added this line to my hibernate.cfg.xml:
Code:
<property name="current_session_context_class">thread</property>
After that, I got the following error when trying to persist an object:
Code:
persist is not valid without active transaction
org.hibernate.HibernateException: persist is not valid without active transaction
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:297)
at $Proxy0.persist(Unknown Source)
Very well, i suppose that makes sense, an insert is an unsafe operation.
So i began and commited a transaction around my persist as such:
Code:
sessionFactory.getCurrentSession().beginTransaction();
sessionFactory.getCurrentSession().persist(transientInstance);
sessionFactory.getCurrentSession().getTransaction().commit();
Success!, however, I'm now getting the same error when I'm truing to retrieve that very record:
Code:
get is not valid without active transaction
org.hibernate.HibernateException: get is not valid without active transaction
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:297)
at $Proxy0.get(Unknown Source)
So, my questions are:
- what is a CurrentSessionContext? I supplied the value "thread". what does that mean? What are the other options?
- I'm guessing that all session operations that access the DB must be part of a transaction? The persist makes sense, but why the get?
- If, indeed, all operations must explicitly be part of a transaction, then why did the Hibernate tools code generator not put that in my DAO as well?
Sorry for the long post. Thanks for any help!