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:
- session.saveorupdate(person)
- person.sethashedid(hash(person.getid()))
the id autogenerated in db. when this, shouldnt expect 2 statements
- select next sequence id (person id)
- 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
Post a Comment