Hi people!
I have a weird error of double insert in the DB. I'll summarize the problem and then put the codes.
I have the following classes:
- TestEntity - entity with a @PrePersist method. This entity is staying in the transaction after it is committed by the container
- Auditing - audit entity
- Dataset<T> - interface of DatasetBean<T>
- DatasetBean<T> - Stateless bean which implements Dataset
- DatasetFactory - instance an
EJB of Dataset (lookup)
- PersistenceLifeCycleListener - an entity lifecycle listener, defined in orm.xml file (following the example in
http://docs.jboss.org/hibernate/core/4.0/hem/en-US/html/listeners.html)
I put the problem in a
junit test (I'm using embedded Glassfish):
The flow of the test is the following:
1. After gettint a Dataset object, I try to insert a TestEntity object
2. After insertion, the PostPersist listener is called, and I try to insert an auditing entity. Here, if I uncomment //dataset.getEntityManager().clear(), the test passes. Otherwise I got an error (log below).
Log - look out lines 6 and 10, hibernate inserted the same entity twice:
persistence.xml
orm.xml
TestEntity
Auditing
MyEntity
What am I doing wrong?