MarCor
MarCor

Reputation: 11

JPA <pre-persist> <pre-update> are being ignored but the @PrePersist and @PreUpdate work fine

I ran into strange problem. I have the whole domain model defined in the orm.xml file. All my entities in my project are just simple POJOs (no jpa annotations at all). I want to save the last update and the insert timestamps of my entities and I've decided to use the "pre persist" and "pre update" like most of us. So I've defined a base entity class and let all my entities to extend it.

Strange is that the "pre persist" (and all others events) are being called only when I define them using annotations. When I define them in the orm.xml file instead - nothing happens, they are just ignored.

This works for me:

public abstract class BaseEntity {

  private Timestamp insertTimestamp;
  private Timestamp lastUpdateTimestamp;

  @PrePersist
  public void onPersist() {
  ...   
  }

  @PreUpdate
  public void onUpdate() {
  ...
  }
}

But after removing annotations and switching to the xml nothing works anymore:

<mapped-superclass class="com.my.model.BaseEntity">
   <pre-persist method-name="onPersist"/>
   <pre-update method-name="onUpdate"/>
   <post-load method-name="postLoad"/>
</mapped-superclass> 

According to the JPA specification the above declarations in xml seem to be correct.

I have no idea where to dig for the problem.

I'm using EclipseLink 2.2.0 with H2 in the SE environment.


UPDATE:

Thanks for your answer. There are no errors in log/console to see. Events just seem being ignored.

As you thought is might be a bug because moving the methods and XML declarations from the superclass to the subclass solves the problem. It is not a desired solution for me as I want to have a global solution for all entities but moved me a bit forward.

I've sent the bug report to the EclipseLink guys :)

As you suggested I've tried with entity listener and it works for me. so I will stick to this solution. It even looks better then the solution with base entity class ;)

Thanks !

Upvotes: 1

Views: 2037

Answers (1)

James
James

Reputation: 18379

Your XML looks correct. Do any errors occur in the logs?

It could be a bug with MappedSuperClass and entity events. Can you try setting the event on a subclass and see if it works? If it does, then it is probably a bug, please log the bug in Eclipse Bugzilla.

Another workaround would be to use an entity listener.

Upvotes: 1

Related Questions