Fixed
Details
Assignee
Marco BelladelliMarco BelladelliReporter
Nathan XuNathan XuComponents
Affects versions
Priority
Major
Details
Details
Assignee
Marco Belladelli
Marco BelladelliReporter
Nathan Xu
Nathan XuComponents
Affects versions
Priority
Created last week
Updated 3 days ago
Resolved 3 days ago
When I implement an id generator overriding
allowAssignedIdentifiers
method to return true as below:public static class IdGenerator implements BeforeExecutionGenerator { @Override public Object generate( SharedSessionContractImplementor session, Object owner, Object currentValue, EventType eventType) { ... ... } @Override public EnumSet<EventType> getEventTypes() { return EnumSet.of( EventType.INSERT ); } @Override public boolean allowAssignedIdentifiers() { return true; }
When an id has been assigned to
owner
, to my surprise thecurrentValue
is still null, not the assigned value, which means I have to work around this by the following code pattern:@Override public Object generate( SharedSessionContractImplementor session, Object owner, Object currentValue, EventType eventType) { Object id = session.getEntityPersister( null, owner ).getIdentifier( owner ); if (id != null) { // id has been assigned } else { // not assigned, need to generate } // return id }
The above workaround seems unnecessary, tedious, error-prone (no clue on entity name, so
null
is used), defeating the purpose ofcurrentValue
parameter.Seems we can modify upstream code logic to ensure that if the id has been assigned, the
currentValue
should not be null. Then we could enjoy the following code pattern intuitively:@Override public Object generate( SharedSessionContractImplementor session, Object owner, Object currentValue, EventType eventType) { if (currentValue != null) { return currentValue; // assigned } // generate id then }