我第一次运行该方法时,它工作正常,对象是持久的。但第二次失败并给我例外:
$('ul.some-ul').each(function(i, ul) {
$(ul).find("li").each(function(j, li) {
// Now you can use $(ul) and $(li) to refer to the list and item
if ($(li).hasClass('some-li')) {
...
}
});
});
这是我向用户插入新笔记的方法:
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");
}
任何消化? 提前谢谢。
答案 0 :(得分:0)
似乎你正试图坚持anotacao,它有一个用户对象的引用,但用户对象本身不是由你的实体管理器管理的,所以它无法在数据库中指向它。尝试从db中提取用户对象,然后将anotacao的用户对象设置为该用户对象。