mysql - How efficient are Hibernate's interceptors compared to database triggers? -


since i can't make mysql update column on update, i'm thinking of using hibernate's interceptors, update updated table each time row updated.

my concern is, how of performance penalty imply, compared ideal case of having mysql updating it?

there's no significant performance penalty , don't need interceptor either.

i created example on github this.

you need create jpa callback listener:

public class updatablelistener {      @prepersist     @preupdate     private void setcurrenttimestamp(object entity) {         if(entity instanceof updatable) {             updatable updatable = (updatable) entity;             updatable.settimestamp(new date());         }     }  } 

which uses interface that's defined this:

public interface updatable {      void settimestamp(date timestamp);      date gettimestamp(); } 

you can define base class entities:

@mappedsuperclass @entitylisteners(updatablelistener.class) public class baseentity implements updatable {      @column(name = "update_timestamp")     private date timestamp;      @override     public void settimestamp(date timestamp) {         this.timestamp = timestamp;     }      @override     public date gettimestamp() {         return timestamp;     } } 

and have entities extend it:

@entity(name = "post") @table(name = "post") public class post extends baseentity {     ...   }  @entity(name = "postcomment") @table(name = "post_comment") public static class postcomment extends baseentity {     ...   } 

when modify these entities:

doinjpa(entitymanager -> {     post post = entitymanager.find(post.class, 1l);     post.settitle("post");     for(postcomment comment : post.getcomments()) {         comment.setreview("review");     } }); 

hibernate take care of setting timestamp column:

update  post set     update_timestamp = '2016-02-06 17:03:26.759' ,         title = 'post'   id = 1 update  post_comment set     update_timestamp = '2016-02-06 17:03:26.76' ,         post_id = 1 ,         review = 'review'   id = 1 update  post_comment set     update_timestamp = '2016-02-06 17:03:26.76' ,         post_id = 1 ,         review = 'review'   id = 2 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -