database - Hibernate batching operation not working as expected -


i have person class following fields-

id, hashedid, description

id primary key generated sequence , hashedid not null.

i following:

  1. session.saveorupdate(person)
  2. person.sethashedid(hash(person.getid()))

the id autogenerated in db. when this, shouldnt expect 2 statements

  1. select next sequence id (person id)
  2. insert insert person record?

however, trying insert right after step 1(during final transaction commit, ofcourse) null hashedid - constraint violation error - hashedid cannot null.

when call session.save() or similar, hibernate generate ids , insert rather queueing saved later. there's no gap in element has id assigned before it's inserted. 'identity' id generation strategy, it's impossible split these anyway...

in experience, safest , simplest way handle kind of case use interceptor (or maybe eventlistener?) trap entity being inserted hashedid property unset, , generate before save. it's bit unpleasant, imho better pulling id generation application code.

here's example generate 'reference' property of new ticket entity (using interceptor):

public boolean onsave(object entity, serializable id, object[] state, string[] propertynames,     type[] types) {     boolean changed = super.onsave(entity, id, state, propertynames, types);      if (entity instanceof ticket) {         (int = 0; < propertynames.length; i++) {             if (propertynames[i].equals("reference") && state[i] == null) {                 state[i] = generateticketreference((integer) id);                 changed = true;             }         }     }      return changed; } 

Comments

Popular posts from this blog

400 Bad Request on Apache/PHP AddHandler wrapper -

Add email recipient to all new Trac tickets -

php - Change action and image src url's with jQuery -