java - Any way to obtain generated id for new entity instance in HibernateInterceptor? -


for sake of discussion, consider following hibernate annotated object:

@entity public class testmodelobject {     @id     @generatedvalue     @column     private int id;      @column     private string name;      public int getid() {         return id;     }      public void setid(int id) {         this.id = id;     }      public string getname() {         return name;     }      public void setname(string name) {         this.name = name;     }     } 

the question is, how can obtain generated id value, new instance, via hibernateinterceptor (persisted via session.save)? outline of methods available, i've tested, , explain why each 1 insufficient:

  • onsave - not work because id null, new instance being inserted. makes sense because hibernate doesn't know id ahead of time in case because it's generated @ insertion time rdbms.
  • postflush - has data i'm after, unusable. contains iterator of many objects being flushed, including 1 you're interested in, there's no reasonably simple way pull out newly saved instance iterator, without doing terribly hackish adding "marker" boolean , populating in onsave, , looking here.
  • instantiate - sort of has data, unusable. gets called every time hibernate instantiates object, , includes id parameter. gets called newly persisted entity, there's no reasonable correlate entity itself, since includes class name, type (pojo in case), , id.

i hoping postsave, fire after insert completes, newly created entity, have generated id populated. wasn't able find such thing. there way accomplish i'm trying do, without resorting hacks such saving twice , performing interceptor "work" on second save?

have tried jpa entity listeners?
using entity listeners can annotate entity class this:

@entity @entitylisteners({ userentitylistener.class, userpasswordvalidator.class }) public class book {      @id     @generatedvalue     @column(name = "id")     private long id;      @columnt(name = "login")     private string login;      @column(name = "password")     private string password;      // getters, setters ommited  } 

then implement listeners:

public class bookentitylistener {      @postpersist     public void postpersist(user user) {         // logic     }  }  public class passwordvalidator {      @prepersist     @preupdate     public void validatepassword(user user) {         if (passwordtooshort(user.password())             throw new persistenceexception("password short");     }  } 

there multiple lifecycle annotations available:

  • @prepersist
    executed before entity manager persist operation executed or cascaded. call synchronous persist operation.

  • @postpersist
    executed after entity manager persist operation executed or cascaded. call invoked after database insert executed.

  • @preremove
    executed before entity manager remove operation executed or cascaded. call synchronous remove operation.

  • @postremove
    executed after entity manager remove operation executed or cascaded. call synchronous remove operation.

  • @preupdate
    executed before database update operation.

  • @postupdate
    executed after database update operation.

  • @postload
    executed after entity has been loaded current persistence context or entity has been refreshed.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -