The first time I run the method, it works fine, the object is persisted. But the 2nd time it fails and give me the exception:
我第一次运行该方法,它工作正常,对象是持久的。但第二次失败并给我例外:
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)
That's my method to insert a new Note to an User:
这是我向用户插入新笔记的方法:
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");
}
It happens when try to execute the following line:
尝试执行以下行时会发生这种情况:
anotacaoDAO.persist(anotacao);
Any sugestion? Thanks in advance.
任何消化?提前致谢。
1 个解决方案
#1
Seems like you're trying to persist anotacao, which has a reference to a user object, but the user object itself is not managed by your entity manager, so it has no way to point to it in the database. Try to have the user object be pulled from the db, and then to set the anotacao's user object to that.
好像你试图坚持anotacao,它有一个用户对象的引用,但用户对象本身不是由你的实体管理器管理的,所以它无法在数据库中指向它。尝试从db中提取用户对象,然后将anotacao的用户对象设置为该用户对象。
#1
Seems like you're trying to persist anotacao, which has a reference to a user object, but the user object itself is not managed by your entity manager, so it has no way to point to it in the database. Try to have the user object be pulled from the db, and then to set the anotacao's user object to that.
好像你试图坚持anotacao,它有一个用户对象的引用,但用户对象本身不是由你的实体管理器管理的,所以它无法在数据库中指向它。尝试从db中提取用户对象,然后将anotacao的用户对象设置为该用户对象。