我正在开发一个j2ee项目。在我的项目中,我想使用Hibernate标准保存Product子类别。
这是我的servlet代码:
try {
if (req.getParameter("ccategory").equals("0")) {
ProductCategory category = new ProductCategory();//saveing product category
category.setName(req.getParameter("cname"));
category.setStatus(status);
HibernateSession.save(HibernateSession.getSession(), category);
} else {
ProductCategory category = HibernateSession.Load(ProductCategory.class, req.getParameter("ccategory"));
ProductSubcategory productSubcategory = new ProductSubcategory();
productSubcategory.setProductCategory(category);
productSubcategory.setSubcategory(req.getParameter("cname"));
productSubcategory.setStatus(status);
HibernateSession.save(HibernateSession.getSession(), productSubcategory);
}
resp.sendRedirect("index.jsp");
} catch (Exception e) {
e.printStackTrace();
}
这是我的会话代码方法:
public class HibernateSession {
public static Session getSession() {
Session session = Connection.NewHibernateUtil.getSessionFactory().openSession();
return session;
}
public static void save(Session session, Object ob) {
Transaction transaction = session.beginTransaction();
session.save(ob);
session.flush();
transaction.commit();
session.close();
}
}
但是在保存子类别时出现错误。
org.hibernate.HibernateException:非法尝试将代理与两个打开的会话相关联
org.hibernate.HibernateException: illegally attempted to associate a proxy with two open Sessions
at org.hibernate.proxy.AbstractLazyInitializer.setSession(AbstractLazyInitializer.java:123)
at org.hibernate.engine.internal.StatefulPersistenceContext.reassociateProxy(StatefulPersistenceContext.java:568)
at org.hibernate.engine.internal.StatefulPersistenceContext.reassociateIfUninitializedProxy(StatefulPersistenceContext.java:533)
at org.hibernate.event.internal.ProxyVisitor.processEntity(ProxyVisitor.java:49)
at org.hibernate.event.internal.AbstractVisitor.processValue(AbstractVisitor.java:124)
at org.hibernate.event.internal.WrapVisitor.processValue(WrapVisitor.java:125)
at org.hibernate.event.internal.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:76)
at org.hibernate.event.internal.AbstractSaveEventListener.visitCollectionsBeforeSave(AbstractSaveEventListener.java:372)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:273)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:711)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:703)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:698)
at hibernate.HibernateSession.save(HibernateSession.java:28)
at Servlet.category.category_save.doPost(category_save.java:55)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
答案 0 :(得分:2)
实际上,您需要在加载类别后添加session.close()方法。
ProductCategory category = HibernateSession.Load(ProductCategory.class,
req.getParameter("ccategory"));
session.close();
由于创建了两个会话而发生了问题。加载类别后,您尚未关闭会话。因此,在上面的代码中定义了解决方案。