If I wanted to utilize the HibernateUtil class from CaveatEmptor with Hibernate 3, is there any additional logic that is required except for changing the library names?
The reason I ask is that within my DAO, I call the getSession with the following code:
Code:
List goalsList;
Session session = HibernateUtil.getSession();
Query query = session.getNamedQuery("selectGoals");
query.setInteger("taskId", taskId);
query.setString("timeframe", goalType);
goalsList = query.list();
session.close();
return goalsList;
This causes an exception to be thrown that the Session is already closed.
Quote:
5 threw exception: org.hibernate.HibernateException: Session is closed
org.hibernate.HibernateException: Session is closed
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:97)
at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:86)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1096)
at org.hibernate.loader.Loader.doQuery(Loader.java:367)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:208)
at org.hibernate.loader.Loader.doList(Loader.java:1522)
at org.hibernate.loader.Loader.list(Loader.java:1505)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:103)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1343)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:151)
But if I utilize the following code in my DAO, the query works without problems
Code:
List goalsList;
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
Query query = session.getNamedQuery("selectGoals");
query.setInteger("taskId", taskId);
query.setString("timeframe", goalType);
goalsList = query.list();
session.close();
return goalsList;
Thanks for your help!
-jay