Hibernate has been used recently, encountered in the implementation of inquiry个"Session is closed!" Troublesome, is as follows:
org.hibernate.SessionException: Session is closed!
at org.hibernate.impl.AbstractSessionImpl.errorIfClosed (AbstractSessionImpl.java: 72)
at org.hibernate.impl.SessionImpl.getPersistenceContext (SessionImpl.java: 1850)
at org.hibernate.type.ManyToOneType.scheduleBatchLoadIfNeeded (ManyToOneType.java: 142)
at org.hibernate.type.ManyToOneType.hydrate (ManyToOneType.java: 128)
at org.hibernate.type.EntityType.nullSafeGet (EntityType.java: 227)
at org.hibernate.impl.IteratorImpl.next (IteratorImpl.java: 135)
at database.ValidationDAO.find (ValidationDAO.java: 35)
at test.testHibe.main (testHibe.java: 16)
At the beginning of my abstract Dao ri are written so:
protected Iterator find(Class clazz,String user){
Iterator it = null;
try {
startOperation();
Query q=session.createQuery("from "+clazz.getName()+" v where v.user = :user");
q.setParameter("user", user);
it=q.iterate();
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
return it;
} Dao examples, however, are:
public Validation find(String user) {
try {
Iterator it = this.find(Validation.class, user,null);
if(it.hasNext())
return (Validation) it.next();
} catch (Exception e) {
e.printStackTrace();
}
return null;
} In my search for a long time-line information, the solution is still not allowed. Suddenly switch to List rather than Iterator, then slightly changed under the two sections of code:
protected List find(Class clazz,String user){
List list = null;
try {
startOperation();
Query q=session.createQuery("from "+clazz.getName()+" v where v.user = :user");
q.setParameter("user", user);
list=q.list();
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
return list;
} public Validation find(String user) {
try {
List list=find(Validation.class,user);
Iterator it=list.iterator();
if(it.hasNext()){
return (Validation)it.next();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
} Unexpectedly, only one such change was not wrong! Puzzled ... ...







