1
我第一次運行該方法時,它工作正常,對象被持久化。但它失敗,第二次給我的例外:PersistentObjectException:傳遞給persist的分離實體 - 運行方法第二次得到這個異常
javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: br.edu.ifpb.calendario.models.Usuario
org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763)
org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1187)
br.edu.ifpb.calendario.dao.GenericDAO.persist(GenericDAO.java:29)
br.edu.ifpb.calendario.servlets.CalendarioServlet.cadastrarAnotacao(CalendarioServlet.java:122)
br.edu.ifpb.calendario.servlets.CalendarioServlet.doPost(CalendarioServlet.java:76)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
org.hibernate.PersistentObjectException: detached entity passed to persist: br.edu.ifpb.calendario.models.Usuario
org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:139)
org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:801)
org.hibernate.internal.SessionImpl.persist(SessionImpl.java:794)
org.hibernate.jpa.event.internal.core.JpaPersistEventListener$1.cascade(JpaPersistEventListener.java:97)
org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:350)
org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:293)
org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161)
org.hibernate.engine.internal.Cascade.cascade(Cascade.java:118)
org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:442)
org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:266)
org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:195)
org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126)
org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:84)
org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206)
org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149)
org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)
org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)
org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1181)
br.edu.ifpb.calendario.dao.GenericDAO.persist(GenericDAO.java:29)
br.edu.ifpb.calendario.servlets.CalendarioServlet.cadastrarAnotacao(CalendarioServlet.java:122)
br.edu.ifpb.calendario.servlets.CalendarioServlet.doPost(CalendarioServlet.java:76)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
這就是我的方法插入一個新的註釋到用戶:
public void cadastrarAnotacao(HttpServletRequest request, HttpServletResponse response) throws ParseException, IOException {
HttpSession session = request.getSession();
Usuario usuario = (Usuario) session.getAttribute("usuario");
AnotacaoDAO anotacaoDAO = new AnotacaoDAO();
Anotacao anotacao = new Anotacao();
String mensagem = request.getParameter("mensagem");
String dataString = request.getParameter("data");
Date data = new SimpleDateFormat("yyyy-MM-dd").parse(dataString);
anotacao.setData(data);
anotacao.setMensagem(mensagem);
anotacao.setUsuario(usuario);
usuario.setAnotacao(anotacao);
anotacaoDAO.persist(anotacao);
anotacaoDAO.close();
session.setAttribute("usuario", usuario);
response.sendRedirect("calendario.do?op=eventos");
}
它發生時試圖執行以下行:
anotacaoDAO.persist(anotacao);
任何消化? 在此先感謝。