if ( !lockable.isVersioned() ) {
throw new HibernateException( "write locks via update not supported for non-versioned entities [" + lockable.getEntityName() + "]" );
}
// todo : should we additionally check the current isolation mode explicitly?
SessionFactoryImplementor factory = session.getFactory();
try {
PreparedStatement st = session.getBatcher().prepareSelectStatement( sql );
try {
lockable.getVersionType().nullSafeSet( st, version, 1, session );
int offset = 2;
lockable.getIdentifierType().nullSafeSet( st, id, offset, session );
offset += lockable.getIdentifierType().getColumnSpan( factory );
if ( lockable.isVersioned() ) {
lockable.getVersionType().nullSafeSet( st, version, offset, session );
}
int affected = st.executeUpdate();
if ( affected < 0 ) {
factory.getStatisticsImplementor().optimisticFailure( lockable.getEntityName() );
throw new StaleObjectStateException( lockable.getEntityName(), id );
}
}
finally {
session.getBatcher().closeStatement( st );
}
}
catch ( SQLException sqle ) {
throw JDBCExceptionHelper.convert(
session.getFactory().getSQLExceptionConverter(),
sqle,
"could not lock: " + MessageHelper.infoString( lockable, id, session.getFactory() ),
sql
);
}