I am doing this in each and every DAL method:
public static Product GetProductByPartNumber(String partNumber)
{
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Product product = (Product)session.createCriteria(Product.class)
.add(Restrictions.eq("partNumber", partNumber))
.uniqueResult();
session.getTransaction().commit();
return product;
}
This is causing problems, giving me 'session is closed' errors because of lazy loading etc.
How can I have getCurrentSession to not get a new session?
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration().configure();
//configuration.addClass(hsspider.Model.AMCategory.class);
return configuration.buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
I am guessing that is the problem, that the call to getCurrentSession is not getting the current session right?
Or does calling transaction.commit(); kill the session? Should I just use flush?