| Author |
Double insertion using entity lifecycle listener
|
Joao Longo
Greenhorn
Joined: Jan 04, 2012
Posts: 8
|
|
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?
|
 |
Vijitha Kumara
Bartender
Joined: Mar 24, 2008
Posts: 3670
|
|
Welcome to the CodeRanch!
TestEntity - entity with a @PrePersist method. This entity is staying in the transaction after it is committed by the container
Can you elaborate this little more?
Looking at the error it seems your second insert for auditing also tries to insert the previous entity. Do you have a separate table for Auditing? Just add the table name for the @Entity annotation as well.
|
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
|
 |
Joao Longo
Greenhorn
Joined: Jan 04, 2012
Posts: 8
|
|
Hi!
Vijitha Kumara wrote:
TestEntity - entity with a @PrePersist method. This entity is staying in the transaction after it is committed by the container
Can you elaborate this little more?
Yes. Relying on the fact that everything works fine if I clear the entity manager before the insertion of the Auditing entity, I believe that the TestEntity is getting stuck in the transaction.
Looking at this article http://simboss.blogspot.com/2008/10/entity-lifecycle-listeners.html , the autor wrote:
"Listener classed do not support dependency injection, they are not managed classes. Note that you can still use JNDI and fallback onto pre-EJB3 mechanism to get resources such as EJB interfaces, Transactions and the like."
In fact, I'm making a lookup, but it's not working properly
Looking at the error it seems your second insert for auditing also tries to insert the previous entity. Do you have a separate table for Auditing? Just add the table name for the @Entity annotation as well.
Yes, Auditing is in a separate table. I tried what you suggest but I got the same error
|
 |
Vijitha Kumara
Bartender
Joined: Mar 24, 2008
Posts: 3670
|
|
In fact, I'm making a lookup, but it's not working properly
What do you mean, lookup failed or something else? If the former then I think you should fix that first.
|
 |
Joao Longo
Greenhorn
Joined: Jan 04, 2012
Posts: 8
|
|
Vijitha Kumara wrote:
In fact, I'm making a lookup, but it's not working properly
What do you mean, lookup failed or something else? If the former then I think you should fix that first.
Sorry, lookup is not failing, but I got an error as explained in the topic
|
 |
 |
|
|
subject: Double insertion using entity lifecycle listener
|
|
|