Я занимаюсь разработкой проекта j2ee.В моем проекте я хочу сохранить подкатегорию Product, используя критерии Hibernate.
Это мой код сервлета:
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)