I’m doing a Web application using Spring 3.1.0.RELEASE, JSF 2.x, JPA 2 with Hibernate Provider, MySql 5.1.x. The application runs on Tomcat 7.X.
In my entities I have some date like last update date:
@Column(name = "last_update_date", insertable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date lastUpdateDate;
For the moment I have a trigger that updates:
CREATE TRIGGER upd_site BEFORE UPDATE ON site
FOR EACH ROW SET NEW.last_update_date = CURRENT_TIMESTAMP();
It works fine, but I just notice that there is some callbacks methods in JPA http://www.objectdb.com/java/jpa/persistence/event
What is the best between JPA Events and the MySql’s triggers ?
Thanks.
I have used it both ways with Triggers in the DB and with JPA listeners, I have settled on the JPA listeners because:
the only code talking to the database in JPA code so I don’t have to worry about the time stamp fields falling out of date. (If this changes in the future I can add triggers and change my mapped super calss)
JPA listeners are less complex in the sense that I did not have to go creating lots of triggers in my database so I had less things to maintain. Since I am actively developing and changing the db structure as I go along its great not to have to go and update triggers as I rapidly iterate through the development.
I have complete control over the database and made a rule for the db that every table was going to have a integer pkey, and an integer version, and that the time stamped tables would have
insert_tsandupdate_tscolumns these are universal rules in my db design so life is easy I have these two mapped superclases that make all my enitites simple to code since I extend from them.and