Skip to content

Commit

Permalink
HHH-11867 - @UpdateTimestamp not working with @Inheritance( strategy …
Browse files Browse the repository at this point in the history
…= JOINED )
  • Loading branch information
dreab8 committed Mar 21, 2018
1 parent 3f666fe commit f030e7f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 108 deletions.
Expand Up @@ -95,7 +95,6 @@
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.Formula;
import org.hibernate.mapping.Join;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.Selectable;
Expand Down Expand Up @@ -193,7 +192,7 @@ public abstract class AbstractEntityPersister
private final boolean[] propertyUniqueness;
private final boolean[] propertySelectable;

private final List<Integer> lobProperties = new ArrayList<Integer>();
private final List<Integer> lobProperties = new ArrayList<>();

//information about lazy properties of this class
private final String[] lazyPropertyNames;
Expand Down Expand Up @@ -230,7 +229,7 @@ public abstract class AbstractEntityPersister
// dynamic filters attached to the class-level
private final FilterHelper filterHelper;

private final Set<String> affectingFetchProfileNames = new HashSet<String>();
private final Set<String> affectingFetchProfileNames = new HashSet<>();

private final Map uniqueKeyLoaders = new HashMap();
private final Map lockers = new HashMap();
Expand Down Expand Up @@ -1654,8 +1653,8 @@ public boolean includeProperty(int propertyNumber) {
.toStatementString();
}

protected static interface InclusionChecker {
public boolean includeProperty(int propertyNumber);
protected interface InclusionChecker {
boolean includeProperty(int propertyNumber);
}

protected String concretePropertySelectFragment(String alias, final boolean[] includeProperty) {
Expand Down Expand Up @@ -1753,7 +1752,7 @@ public Object forceVersionIncrement(Serializable id, Object currentVersion, Shar

// todo : cache this sql...
String versionIncrementString = generateVersionIncrementUpdateString();
PreparedStatement st = null;
PreparedStatement st;
try {
st = session
.getJdbcCoordinator()
Expand Down Expand Up @@ -3454,7 +3453,7 @@ private String[] getUpdateStrings(boolean byRowId, boolean lazy) {
public void update(
final Serializable id,
final Object[] fields,
final int[] dirtyFields,
int[] dirtyFields,
final boolean hasDirtyCollection,
final Object[] oldFields,
final Object oldVersion,
Expand All @@ -3464,12 +3463,32 @@ public void update(

// apply any pre-update in-memory value generation
if ( getEntityMetamodel().hasPreUpdateGeneratedValues() ) {
final InMemoryValueGenerationStrategy[] strategies = getEntityMetamodel().getInMemoryValueGenerationStrategies();
for ( int i = 0; i < strategies.length; i++ ) {
if ( strategies[i] != null && strategies[i].getGenerationTiming().includesUpdate() ) {
fields[i] = strategies[i].getValueGenerator().generateValue( (Session) session, object );
setPropertyValue( object, i, fields[i] );
// todo : probably best to add to dirtyFields if not-null
final InMemoryValueGenerationStrategy[] valueGenerationStrategies = getEntityMetamodel().getInMemoryValueGenerationStrategies();
int valueGenerationStrategiesSize = valueGenerationStrategies.length;
if ( valueGenerationStrategiesSize != 0 ) {
int[] fieldsPreUpdateNeeded = new int[valueGenerationStrategiesSize];
for ( int i = 0; i < valueGenerationStrategiesSize; i++ ) {
if ( valueGenerationStrategies[i] != null && valueGenerationStrategies[i].getGenerationTiming()
.includesUpdate() ) {
fields[i] = valueGenerationStrategies[i].getValueGenerator().generateValue(
(Session) session,
object
);
setPropertyValue( object, i, fields[i] );
fieldsPreUpdateNeeded[i] = i;
}
}
// if ( fieldsPreUpdateNeeded.length != 0 ) {
// if ( dirtyFields != null ) {
// dirtyFields = ArrayHelper.join( fieldsPreUpdateNeeded, dirtyFields );
// }
// else if ( hasDirtyCollection ) {
// dirtyFields = fieldsPreUpdateNeeded;
// }
// // no dirty fields and no dirty collections so no update needed ???
// }
if ( fieldsPreUpdateNeeded.length != 0 && dirtyFields != null ) {
dirtyFields = ArrayHelper.join( fieldsPreUpdateNeeded, dirtyFields );
}
}
}
Expand Down Expand Up @@ -3752,7 +3771,7 @@ public String fromJoinFragment(String alias, boolean innerJoin, boolean includeS
alias,
innerJoin,
includeSubclasses,
Collections.<String>emptySet()
Collections.emptySet()
).toFromFragmentString();
}

Expand All @@ -3777,7 +3796,7 @@ public String whereJoinFragment(String alias, boolean innerJoin, boolean include
alias,
innerJoin,
includeSubclasses,
Collections.<String>emptySet()
Collections.emptySet()
).toWhereFragmentString();
}

Expand Down Expand Up @@ -5468,7 +5487,6 @@ public CacheEntry buildCacheEntry(Object entity, Object[] state, Object version,
// EntityDefinition impl ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

private EntityIdentifierDefinition entityIdentifierDefinition;
private Iterable<AttributeDefinition> embeddedCompositeIdentifierAttributes;
private Iterable<AttributeDefinition> attributeDefinitions;

@Override
Expand All @@ -5493,12 +5511,11 @@ public Iterable<AttributeDefinition> getAttributes() {
}

public String[][] getPolymorphicJoinColumns(String lhsTableAlias, String propertyPath) {
Set<String> subclassEntityNames = (Set<String>) getEntityMetamodel()
.getSubclassEntityNames();
Set<String> subclassEntityNames = getEntityMetamodel().getSubclassEntityNames();
// We will collect all the join columns from the LHS subtypes here
List<String[]> polymorphicJoinColumns = new ArrayList<>( subclassEntityNames.size() );

String[] joinColumns = null;
String[] joinColumns;

OUTER:
for ( String subclassEntityName : subclassEntityNames ) {
Expand Down Expand Up @@ -5610,7 +5627,7 @@ private void collectAttributeDefinitions() {
// to try and drive SQL generation on these (which we do ultimately). A possible solution there
// would be to delay all SQL generation until postInstantiate

Map<String, AttributeDefinition> attributeDefinitionsByName = new LinkedHashMap<String, AttributeDefinition>();
Map<String, AttributeDefinition> attributeDefinitionsByName = new LinkedHashMap<>();
collectAttributeDefinitions( attributeDefinitionsByName, getEntityMetamodel() );


Expand All @@ -5630,7 +5647,7 @@ private void collectAttributeDefinitions() {
// }

this.attributeDefinitions = Collections.unmodifiableList(
new ArrayList<AttributeDefinition>( attributeDefinitionsByName.values() )
new ArrayList<>( attributeDefinitionsByName.values() )
);
// // todo : leverage the attribute definitions housed on EntityMetamodel
// // for that to work, we'd have to be able to walk our super entity persister(s)
Expand Down
Expand Up @@ -24,7 +24,6 @@
import org.hibernate.engine.spi.CascadeStyle;
import org.hibernate.engine.spi.CascadeStyles;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.ValueInclusion;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.collections.ArrayHelper;
Expand Down Expand Up @@ -92,7 +91,7 @@ public class EntityMetamodel implements Serializable {
private final InDatabaseValueGenerationStrategy[] inDatabaseValueGenerationStrategies;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

private final Map<String, Integer> propertyIndexes = new HashMap<String, Integer>();
private final Map<String, Integer> propertyIndexes = new HashMap<>();
private final boolean hasCollections;
private final boolean hasMutableProperties;
private final boolean hasLazyProperties;
Expand Down Expand Up @@ -150,7 +149,7 @@ public EntityMetamodel(

propertySpan = persistentClass.getPropertyClosureSpan();
properties = new NonIdentifierAttribute[propertySpan];
List<Integer> naturalIdNumbers = new ArrayList<Integer>();
List<Integer> naturalIdNumbers = new ArrayList<>();
// temporary ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
propertyNames = new String[propertySpan];
propertyTypes = new Type[propertySpan];
Expand Down Expand Up @@ -181,8 +180,6 @@ public EntityMetamodel(
boolean foundCollection = false;
boolean foundMutable = false;
boolean foundNonIdentifierPropertyNamedId = false;
boolean foundInsertGeneratedValue = false;
boolean foundUpdateGeneratedValue = false;
boolean foundUpdateableNaturalIdProperty = false;

while ( iter.hasNext() ) {
Expand Down Expand Up @@ -379,7 +376,7 @@ else if ( timing == GenerationTiming.ALWAYS ) {
}

entityMode = persistentClass.hasPojoRepresentation() ? EntityMode.POJO : EntityMode.MAP;
final EntityTuplizerFactory entityTuplizerFactory = sessionFactory.getSettings().getEntityTuplizerFactory();
final EntityTuplizerFactory entityTuplizerFactory = sessionFactory.getSessionFactoryOptions().getEntityTuplizerFactory();
final String tuplizerClassName = persistentClass.getTuplizerImplClassName( entityMode );
if ( tuplizerClassName == null ) {
entityTuplizer = entityTuplizerFactory.constructDefaultTuplizer( entityMode, this, persistentClass );
Expand Down Expand Up @@ -514,10 +511,6 @@ public static class ValueGenerationStrategyException extends HibernateException
public ValueGenerationStrategyException(String message) {
super( message );
}

public ValueGenerationStrategyException(String message, Throwable cause) {
super( message, cause );
}
}

private static class CompositeGenerationStrategyPairBuilder {
Expand All @@ -540,7 +533,7 @@ public void addPair(GenerationStrategyPair generationStrategyPair) {

private void add(InMemoryValueGenerationStrategy inMemoryStrategy) {
if ( inMemoryStrategies == null ) {
inMemoryStrategies = new ArrayList<InMemoryValueGenerationStrategy>();
inMemoryStrategies = new ArrayList<>();
}
inMemoryStrategies.add( inMemoryStrategy );

Expand All @@ -551,7 +544,7 @@ private void add(InMemoryValueGenerationStrategy inMemoryStrategy) {

private void add(InDatabaseValueGenerationStrategy inDatabaseStrategy) {
if ( inDatabaseStrategies == null ) {
inDatabaseStrategies = new ArrayList<InDatabaseValueGenerationStrategy>();
inDatabaseStrategies = new ArrayList<>();
}
inDatabaseStrategies.add( inDatabaseStrategy );

Expand Down Expand Up @@ -730,81 +723,6 @@ public String[] getReferencedColumnValues() {
}
}

private ValueInclusion determineInsertValueGenerationType(Property mappingProperty, NonIdentifierAttribute runtimeProperty) {
if ( isInsertGenerated( runtimeProperty ) ) {
return ValueInclusion.FULL;
}
else if ( mappingProperty.getValue() instanceof Component ) {
if ( hasPartialInsertComponentGeneration( ( Component ) mappingProperty.getValue() ) ) {
return ValueInclusion.PARTIAL;
}
}
return ValueInclusion.NONE;
}

private boolean isInsertGenerated(NonIdentifierAttribute property) {
return property.getValueGenerationStrategy() != null
&& property.getValueGenerationStrategy().getGenerationTiming() != GenerationTiming.NEVER;
}

private boolean isInsertGenerated(Property property) {
return property.getValueGenerationStrategy() != null
&& property.getValueGenerationStrategy().getGenerationTiming() != GenerationTiming.NEVER;
}

private boolean hasPartialInsertComponentGeneration(Component component) {
Iterator subProperties = component.getPropertyIterator();
while ( subProperties.hasNext() ) {
final Property prop = ( Property ) subProperties.next();
if ( isInsertGenerated( prop ) ) {
return true;
}
else if ( prop.getValue() instanceof Component ) {
if ( hasPartialInsertComponentGeneration( (Component) prop.getValue() ) ) {
return true;
}
}
}
return false;
}

private ValueInclusion determineUpdateValueGenerationType(Property mappingProperty, NonIdentifierAttribute runtimeProperty) {
if ( isUpdateGenerated( runtimeProperty ) ) {
return ValueInclusion.FULL;
}
else if ( mappingProperty.getValue() instanceof Component ) {
if ( hasPartialUpdateComponentGeneration( ( Component ) mappingProperty.getValue() ) ) {
return ValueInclusion.PARTIAL;
}
}
return ValueInclusion.NONE;
}

private static boolean isUpdateGenerated(Property property) {
return property.getValueGenerationStrategy() != null
&& property.getValueGenerationStrategy().getGenerationTiming() == GenerationTiming.ALWAYS;
}

private static boolean isUpdateGenerated(NonIdentifierAttribute property) {
return property.getValueGenerationStrategy() != null
&& property.getValueGenerationStrategy().getGenerationTiming() == GenerationTiming.ALWAYS;
}

private boolean hasPartialUpdateComponentGeneration(Component component) {
Iterator subProperties = component.getPropertyIterator();
while ( subProperties.hasNext() ) {
Property prop = (Property) subProperties.next();
if ( isUpdateGenerated( prop ) ) {
return true;
}
else if ( prop.getValue() instanceof Component ) {
if ( hasPartialUpdateComponentGeneration( ( Component ) prop.getValue() ) ) {
return true;
}
}
}
return false;
}

private void mapPropertyToIndex(Property prop, int i) {
propertyIndexes.put( prop.getName(), i );
Expand Down

0 comments on commit f030e7f

Please sign in to comment.