deleted entity passed to persist exception ???
elkner Jan 25, 2006 9:34 PMHmmm, is it not allowed to create and persist an entity and later to delete it within the same JTA?
What I [need to] do is:
1) read the header of a file and [re]populate/merge in tree nodes
2) read the remaining part of the file and fill the tree nodes with content/merge new content
3) finally remove all nodes, which have no children (i.e. no content and subnodes) - for max. reduction use deepest first strategy
The SLSB code snippet using 4.0.3SP1 vanilla:
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public Catalog importCatalog(CatalogImportProfile profile, byte[] data, String clientId) { if (profile == null || data == null || data.length == 0) { return null; } CatalogImporter ci = new CatalogImporter(em, profile); ci.readAndMerge(data); if (profile.getId() == 0) { em.persist(profile); } else { em.merge(profile); } em.flush(); if (profile.isEnabled(Option.G_DELETE_EMPTY)) { Query q = em.createQuery("SELECT COUNT(*) FROM Article a " + "WHERE a.group.id=:gid"); List tl = em.createQuery("FROM ProductGroup g " + "WHERE g.parentGroup IS NULL AND g.details.catalog.id=:id") .setParameter("id", profile.getCatalog().getId()) .getResultList(); for (int i=tl.size()-1; i >= 0; i--) { if (isEmpty((ProductGroup) tl.get(i), q)) { em.remove(tl.get(i)); } } em.flush(); } return profile.getCatalog(); }
But with this one I get:
org.hibernate.ObjectDeletedException: deleted entity passed to persist: [foo.bar.ProductGroup#]
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:90)
at org.hibernate.impl.SessionImpl.firePersistOnFlush(SessionImpl.java:610)
at org.hibernate.impl.SessionImpl.persistOnFlush(SessionImpl.java:604)
at org.hibernate.engine.CascadingAction$9.cascade(CascadingAction.java:225)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
at org.hibernate.engine.Cascade.cascade(Cascade.java:248)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:130)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:121)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:905)
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:185)
at org.jboss.ejb3.entity.InjectedEntityManager.flush(InjectedEntityManager.java:122)
at foo.bar.SLSB.importCatalog(SLSB.java:191)
...
where SLSB.java:191 is the second em.flush();
So is it a hibernate bug? At least in the specs I could not find, that it is forbidden to persist and delete the same entity withing the same JTA ...