• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Double insertion using entity lifecycle listener

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Joao Longo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic