throws HibernateException {
final int span = getTableSpan();
if ( span > 1 ) {
throw new HibernateException( "Hibernate OGM does not yet support entities spanning multiple tables");
}
final EntityMetamodel entityMetamodel = getEntityMetamodel();
boolean isImpliedOptimisticLocking = !entityMetamodel.isVersioned() && isAllOrDirtyOptLocking();
Object[] loadedState = null;
if ( isImpliedOptimisticLocking ) {
// need to treat this as if it where optimistic-lock="all" (dirty does *not* make sense);
// first we need to locate the "loaded" state
//
// Note, it potentially could be a proxy, so doAfterTransactionCompletion the location the safe way...
org.hibernate.engine.spi.EntityKey key = session.generateEntityKey( id, this );
Object entity = session.getPersistenceContext().getEntity( key );
if ( entity != null ) {
EntityEntry entry = session.getPersistenceContext().getEntry( entity );
loadedState = entry.getLoadedState();
}
}
final EntityKey key = EntityKeyBuilder.fromPersister( this, id, session );
final Tuple resultset = gridDialect.getTuple( key, this.getTupleContext() );
final SessionFactoryImplementor factory = getFactory();
if ( isImpliedOptimisticLocking && loadedState != null ) {
// we need to utilize dynamic delete statements
for ( int j = span - 1; j >= 0; j-- ) {
boolean[] versionability = getPropertyVersionability();
//TODO do a diff on the properties value from resultset
GridType[] types = gridPropertyTypes;
for ( int i = 0; i < entityMetamodel.getPropertySpan(); i++ ) {
boolean include = isPropertyOfTable( i, j ) && versionability[i];
if ( include ) {
final GridType type = types[i];
final Object snapshotValue = type.nullSafeGet(
resultset, getPropertyColumnNames( i ), session, object
);
//TODO support other entity modes
if ( ! type.isEqual( loadedState[i], snapshotValue, factory ) ) {
if ( factory.getStatistics().isStatisticsEnabled() ) {
factory.getStatisticsImplementor()
.optimisticFailure( getEntityName() );
}
throw new StaleObjectStateException( getEntityName(), id );
}
}
}
}
}
else {
if ( entityMetamodel.isVersioned() ) {
checkVersionAndRaiseSOSE( id, version, session, resultset );
}
}
for ( int j = span - 1; j >= 0; j-- ) {