HHH-14464 Expose useful things to enable cleanup of HR code

- expose useful static-y methods in AbstractEntityPersister
- add methods to OptimisticLockStyle to reduce verbosity
- add a useful method to ValueGeneration
This commit is contained in:
Gavin King 2021-02-23 10:26:55 +01:00 committed by Sanne Grinovero
parent 578edbfd9f
commit e38df7ed59
5 changed files with 58 additions and 46 deletions

View File

@ -39,6 +39,26 @@ public enum OptimisticLockStyle {
return oldCode;
}
public boolean isAllOrDirty() {
return isAll() || isDirty();
}
public boolean isAll() {
return this == ALL;
}
public boolean isDirty() {
return this == DIRTY;
}
public boolean isVersion() {
return this == VERSION;
}
public boolean isNone() {
return this == NONE;
}
/**
* Given an old code (one of the int constants from Cascade), interpret it as one of the new enums.
*

View File

@ -1386,8 +1386,8 @@ public abstract class AbstractEntityPersister
}
public boolean isBatchable() {
return optimisticLockStyle() == OptimisticLockStyle.NONE
|| ( !isVersioned() && optimisticLockStyle() == OptimisticLockStyle.VERSION )
return optimisticLockStyle().isNone()
|| !isVersioned() && optimisticLockStyle().isVersion()
|| getFactory().getSessionFactoryOptions().isJdbcBatchVersionedData();
}
@ -1757,14 +1757,13 @@ public abstract class AbstractEntityPersister
// rather than trying to handle the individual generated portions.
String selectClause = concretePropertySelectFragment(
getRootAlias(),
new InclusionChecker() {
@Override
public boolean includeProperty(int propertyNumber) {
propertyNumber -> {
final InDatabaseValueGenerationStrategy generationStrategy
= entityMetamodel.getInDatabaseValueGenerationStrategies()[propertyNumber];
GenerationTiming timing = generationStrategy.getGenerationTiming();
return generationStrategy != null
&& timingsMatch( generationStrategy.getGenerationTiming(), generationTimingToMatch );
}
&& (generationTimingToMatch == GenerationTiming.INSERT && timing.includesInsert()
|| generationTimingToMatch == GenerationTiming.ALWAYS && timing.includesUpdate());
}
);
selectClause = selectClause.substring( 2 );
@ -1792,11 +1791,7 @@ public abstract class AbstractEntityPersister
protected String concretePropertySelectFragment(String alias, final boolean[] includeProperty) {
return concretePropertySelectFragment(
alias,
new InclusionChecker() {
public boolean includeProperty(int propertyNumber) {
return includeProperty[propertyNumber];
}
}
propertyNumber -> includeProperty[propertyNumber]
);
}
@ -2726,7 +2721,7 @@ public abstract class AbstractEntityPersister
update.addPrimaryKeyColumns( getKeyColumns( j ) );
}
if ( j == 0 && isVersioned() && entityMetamodel.getOptimisticLockStyle() == OptimisticLockStyle.VERSION ) {
if ( j == 0 && isVersioned() && entityMetamodel.getOptimisticLockStyle().isVersion() ) {
// this is the root (versioned) table, and we are using version-based
// optimistic locking; if we are not updating the version, also don't
// check it (unless this is a "generated" version column)!
@ -2738,12 +2733,11 @@ public abstract class AbstractEntityPersister
else if ( isAllOrDirtyOptLocking() && oldFields != null ) {
// we are using "all" or "dirty" property-based optimistic locking
boolean[] includeInWhere = entityMetamodel.getOptimisticLockStyle() == OptimisticLockStyle.ALL
?
getPropertyUpdateability()
boolean[] includeInWhere = entityMetamodel.getOptimisticLockStyle().isAll()
//optimistic-lock="all", include all updatable properties
:
includeProperty; //optimistic-lock="dirty", include all properties we are updating this time
? getPropertyUpdateability()
//optimistic-lock="dirty", include all properties we are updating this time
: includeProperty;
boolean[] versionability = getPropertyVersionability();
Type[] types = getPropertyTypes();
@ -3470,14 +3464,14 @@ public abstract class AbstractEntityPersister
);
// Write any appropriate versioning conditional parameters
if ( useVersion && entityMetamodel.getOptimisticLockStyle() == OptimisticLockStyle.VERSION ) {
if ( useVersion && entityMetamodel.getOptimisticLockStyle().isVersion()) {
if ( checkVersion( includeProperty ) ) {
getVersionType().nullSafeSet( update, oldVersion, index, session );
}
}
else if ( isAllOrDirtyOptLocking() && oldFields != null ) {
boolean[] versionability = getPropertyVersionability(); //TODO: is this really necessary????
boolean[] includeOldField = entityMetamodel.getOptimisticLockStyle() == OptimisticLockStyle.ALL
boolean[] includeOldField = entityMetamodel.getOptimisticLockStyle().isAll()
? getPropertyUpdateability()
: includeProperty;
Type[] types = getPropertyTypes();
@ -3893,8 +3887,7 @@ public abstract class AbstractEntityPersister
}
protected boolean isAllOrDirtyOptLocking() {
return entityMetamodel.getOptimisticLockStyle() == OptimisticLockStyle.DIRTY
|| entityMetamodel.getOptimisticLockStyle() == OptimisticLockStyle.ALL;
return entityMetamodel.getOptimisticLockStyle().isAllOrDirty();
}
protected String[] generateSQLDeleteStrings(Object[] loadedState) {
@ -5378,12 +5371,11 @@ public abstract class AbstractEntityPersister
}
private boolean isValueGenerationRequired(NonIdentifierAttribute attribute, GenerationTiming matchTiming) {
if ( attribute.getType() instanceof ComponentType ) {
public static boolean isValueGenerationRequired(NonIdentifierAttribute attribute, GenerationTiming matchTiming) {
if ( attribute.getType() instanceof ComponentType) {
final ComponentType type = (ComponentType) attribute.getType();
final ValueGeneration[] propertyValueGenerationStrategies = type.getPropertyValueGenerationStrategies();
for ( ValueGeneration propertyValueGenerationStrategie : propertyValueGenerationStrategies ) {
if ( isReadRequired( propertyValueGenerationStrategie, matchTiming ) ) {
for ( ValueGeneration valueGenerationStrategy : type.getPropertyValueGenerationStrategies() ) {
if ( isReadRequired( valueGenerationStrategy, matchTiming ) ) {
return true;
}
}
@ -5397,16 +5389,10 @@ public abstract class AbstractEntityPersister
/**
* Whether the given value generation strategy requires to read the value from the database or not.
*/
private boolean isReadRequired(ValueGeneration valueGeneration, GenerationTiming matchTiming) {
return valueGeneration != null &&
valueGeneration.getValueGenerator() == null &&
timingsMatch( valueGeneration.getGenerationTiming(), matchTiming );
}
private boolean timingsMatch(GenerationTiming timing, GenerationTiming matchTiming) {
return
( matchTiming == GenerationTiming.INSERT && timing.includesInsert() ) ||
( matchTiming == GenerationTiming.ALWAYS && timing.includesUpdate() );
private static boolean isReadRequired(ValueGeneration valueGeneration, GenerationTiming matchTiming) {
return valueGeneration != null
&& valueGeneration.getValueGenerator() == null
&& valueGeneration.timingMatches( matchTiming );
}
public String getIdentifierPropertyName() {
@ -5587,7 +5573,7 @@ public abstract class AbstractEntityPersister
}
}
private boolean[] determineValueNullness(Object[] naturalIdValues) {
public static boolean[] determineValueNullness(Object[] naturalIdValues) {
boolean[] nullness = new boolean[naturalIdValues.length];
for ( int i = 0; i < naturalIdValues.length; i++ ) {
nullness[i] = naturalIdValues[i] == null;

View File

@ -13,7 +13,6 @@ import org.hibernate.QueryException;
import org.hibernate.boot.model.relational.Database;
import org.hibernate.cache.spi.access.EntityDataAccess;
import org.hibernate.cache.spi.access.NaturalIdDataAccess;
import org.hibernate.engine.OptimisticLockStyle;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle;
import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -200,7 +199,7 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
discriminatorSQLString = null;
}
if ( optimisticLockStyle() == OptimisticLockStyle.ALL || optimisticLockStyle() == OptimisticLockStyle.DIRTY ) {
if ( optimisticLockStyle().isAllOrDirty() ) {
throw new MappingException( "optimistic-lock=all|dirty not supported for joined-subclass mappings [" + getEntityName() + "]" );
}

View File

@ -53,4 +53,13 @@ public interface ValueGeneration extends Serializable {
* @return The column value to be used in the SQL.
*/
public String getDatabaseGeneratedReferencedColumnValue();
/**
* Does this value generation occur with the given timing?
*/
default boolean timingMatches(GenerationTiming timing) {
GenerationTiming generationTiming = getGenerationTiming();
return timing == GenerationTiming.INSERT && generationTiming.includesInsert()
|| timing == GenerationTiming.ALWAYS && generationTiming.includesUpdate();
}
}

View File

@ -395,9 +395,7 @@ public class EntityMetamodel implements Serializable {
hasSubclasses = persistentClass.hasSubclasses();
optimisticLockStyle = persistentClass.getOptimisticLockStyle();
final boolean isAllOrDirty =
optimisticLockStyle == OptimisticLockStyle.ALL
|| optimisticLockStyle == OptimisticLockStyle.DIRTY;
final boolean isAllOrDirty = optimisticLockStyle.isAllOrDirty();
if ( isAllOrDirty && !dynamicUpdate ) {
throw new MappingException( "optimistic-lock=all|dirty requires dynamic-update=\"true\": " + name );
}