Skip to content

Commit

Permalink
Clean up various legacy "read path" contracts
Browse files Browse the repository at this point in the history
* Type#nullSafeGet
* Type#hydrate
* Type#resolve
* Type#getSemiResolvedType
* Type#semiResolve
* related
  • Loading branch information
sebersole committed Oct 21, 2021
1 parent 3d27f61 commit cf36d17
Show file tree
Hide file tree
Showing 23 changed files with 110 additions and 1,125 deletions.

Large diffs are not rendered by default.

Expand Up @@ -36,14 +36,14 @@ public class EntityUniqueKey implements Serializable {
public EntityUniqueKey(
final String entityName,
final String uniqueKeyName,
final Object semiResolvedKey,
final Object key,
final Type keyType,
final EntityMode entityMode,
final SessionFactoryImplementor factory) {
this.uniqueKeyName = uniqueKeyName;
this.entityName = entityName;
this.key = semiResolvedKey;
this.keyType = keyType.getSemiResolvedType( factory );
this.key = key;
this.keyType = keyType;
this.entityMode = entityMode;
this.hashCode = generateHashCode( factory );
}
Expand Down
Expand Up @@ -54,7 +54,8 @@ else if ( value instanceof PersistentCollection ) {
collection = (PersistentCollection) value;
}
else if ( value == LazyPropertyInitializer.UNFETCHED_PROPERTY ) {
collection = (PersistentCollection) type.resolve( value, session, this.owner );
final Object keyOfOwner = type.getKeyOfOwner( owner, session );
collection = (PersistentCollection) type.getCollection( keyOfOwner, session, owner, Boolean.FALSE );
}
else {
return; //EARLY EXIT!
Expand Down
Expand Up @@ -11,6 +11,7 @@
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.internal.Collections;
import org.hibernate.event.spi.EventSource;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.type.CollectionType;

/**
Expand Down Expand Up @@ -41,7 +42,8 @@ Object processCollection(Object collection, CollectionType type) throws Hibernat
coll = session.getPersistenceContextInternal().getCollectionHolder(collection);
}
else if ( collection == LazyPropertyInitializer.UNFETCHED_PROPERTY ) {
coll = (PersistentCollection) type.resolve( collection, session, owner );
final Object keyOfOwner = type.getKeyOfOwner( owner, session );
coll = (PersistentCollection) type.getCollection( keyOfOwner, session, owner, Boolean.FALSE );
}
else if ( collection instanceof PersistentCollection ) {
coll = (PersistentCollection) collection;
Expand Down
30 changes: 18 additions & 12 deletions hibernate-core/src/main/java/org/hibernate/id/SelectGenerator.java
Expand Up @@ -13,6 +13,7 @@

import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.insert.AbstractSelectingDelegate;
Expand Down Expand Up @@ -92,13 +93,16 @@ private SelectGeneratorDelegate(
Dialect dialect,
String suppliedUniqueKeyPropertyName) {
super( persister );
this.persister = persister;
this.dialect = dialect;
this.uniqueKeyPropertyName = determineNameOfPropertyToUse( persister, suppliedUniqueKeyPropertyName );

idSelectString = persister.getSelectByUniqueKeyString( uniqueKeyPropertyName );
uniqueKeyType = persister.getPropertyType( uniqueKeyPropertyName );
idType = persister.getIdentifierType();
throw new NotYetImplementedFor6Exception( getClass() );

// this.persister = persister;
// this.dialect = dialect;
// this.uniqueKeyPropertyName = determineNameOfPropertyToUse( persister, suppliedUniqueKeyPropertyName );
//
// idSelectString = persister.getSelectByUniqueKeyString( uniqueKeyPropertyName );
// uniqueKeyType = persister.getPropertyType( uniqueKeyPropertyName );
// idType = persister.getIdentifierType();
}

public IdentifierGeneratingInsert prepareIdentifierGeneratingInsert() {
Expand Down Expand Up @@ -130,12 +134,14 @@ protected Object getResult(
uniqueKeyPropertyName
);
}
return idType.nullSafeGet(
rs,
persister.getRootTableKeyColumnNames(),
session,
entity
);

throw new NotYetImplementedFor6Exception( getClass() );
// return idType.nullSafeGet(
// rs,
// persister.getRootTableKeyColumnNames(),
// session,
// entity
// );
}
}
}
Expand Up @@ -986,55 +986,13 @@ public Class getElementClass() {
return elementClass;
}

@Override
public Object readElement(ResultSet rs, Object owner, String[] aliases, SharedSessionContractImplementor session)
throws HibernateException, SQLException {
return getElementType().nullSafeGet( rs, aliases, session, owner );
}

@Override
public Object readIndex(ResultSet rs, String[] aliases, SharedSessionContractImplementor session)
throws HibernateException, SQLException {
Object index = getIndexType().nullSafeGet( rs, aliases, session, null );
if ( index == null ) {
throw new HibernateException( "null index column for collection: " + navigableRole.getFullPath() );
}
index = decrementIndexByBase( index );
return index;
}

protected Object decrementIndexByBase(Object index) {
if ( baseIndex != 0 ) {
index = (Integer)index - baseIndex;
}
return index;
}

@Override
public Object readIdentifier(ResultSet rs, String alias, SharedSessionContractImplementor session)
throws HibernateException, SQLException {
Object id = getIdentifierType().nullSafeGet( rs, alias, session, null );
if ( id == null ) {
throw new HibernateException( "null identifier column for collection: " + navigableRole.getFullPath() );
}
return id;
}

@Override
public Object readKey(ResultSet rs, String[] aliases, SharedSessionContractImplementor session)
throws HibernateException, SQLException {
// First hydrate the collection key to check if it is null.
// Don't bother resolving the collection key if the hydrated value is null.

// Implementation note: if collection key is a composite value, then resolving a null value will
// result in instantiating an empty composite if AvailableSettings#CREATE_EMPTY_COMPOSITES_ENABLED
// is true. By not resolving a null value for a composite key, we avoid the overhead of instantiating
// an empty composite, checking if it is equivalent to null (it should be), then ultimately throwing
// out the empty value.
final Object hydratedKey = getKeyType().hydrate( rs, aliases, session, null );
return hydratedKey == null ? null : getKeyType().resolve( hydratedKey, session, null );
}

/**
* Write the key to a JDBC <tt>PreparedStatement</tt>
*/
Expand Down
Expand Up @@ -126,31 +126,6 @@ default BasicValueConverter getIndexConverter() {
return null;
}

/**
* Read the key from a row of the JDBC <tt>ResultSet</tt>
*/
Object readKey(ResultSet rs, String[] keyAliases, SharedSessionContractImplementor session)
throws HibernateException, SQLException;
/**
* Read the element from a row of the JDBC <tt>ResultSet</tt>
*/
Object readElement(
ResultSet rs,
Object owner,
String[] columnAliases,
SharedSessionContractImplementor session) throws SQLException;
/**
* Read the index from a row of the JDBC <tt>ResultSet</tt>
*/
Object readIndex(ResultSet rs, String[] columnAliases, SharedSessionContractImplementor session) throws SQLException;

/**
* Read the identifier from a row of the JDBC <tt>ResultSet</tt>
*/
Object readIdentifier(
ResultSet rs,
String columnAlias,
SharedSessionContractImplementor session) throws SQLException;
/**
* Is this an array or primitive values?
*/
Expand Down
Expand Up @@ -107,25 +107,6 @@ public T extract(CallableStatement statement, String paramName, WrapperOptions o
return (T) get( discriminatorValue, options.getSession() );
}

@Override
public Object nullSafeGet(
ResultSet rs,
String[] names,
SharedSessionContractImplementor session,
Object owner) throws HibernateException, SQLException {
return nullSafeGet( rs, names[0], session, owner );
}

@Override
public Object nullSafeGet(
ResultSet rs,
String name,
SharedSessionContractImplementor session,
Object owner) throws HibernateException, SQLException {
final Object discriminatorValue = underlyingType.nullSafeGet( rs, name, session, owner );
return get( discriminatorValue, session );
}

private Object get(Object discriminatorValue, SharedSessionContractImplementor session) {
final String entityName = persister.getSubclassForDiscriminatorValue( discriminatorValue );
if ( entityName == null ) {
Expand Down
Expand Up @@ -251,17 +251,15 @@ public final boolean isModified(
return isDirty( oldHydratedState, currentState );
}

@Override
public final Object nullSafeGet(
private final Object nullSafeGet(
ResultSet rs,
String[] names,
SharedSessionContractImplementor session,
Object owner) throws SQLException {
return nullSafeGet( rs, names[0], session );
}

@Override
public final Object nullSafeGet(ResultSet rs, String name, SharedSessionContractImplementor session, Object owner)
private final Object nullSafeGet(ResultSet rs, String name, SharedSessionContractImplementor session, Object owner)
throws SQLException {
return nullSafeGet( rs, name, session );
}
Expand Down Expand Up @@ -336,27 +334,6 @@ public final Object assemble(Serializable cached, SharedSessionContractImplement
public final void beforeAssemble(Serializable cached, SharedSessionContractImplementor session) {
}

@Override
public final Object hydrate(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
throws HibernateException, SQLException {
return nullSafeGet(rs, names, session, owner);
}

@Override
public final Object resolve(Object value, SharedSessionContractImplementor session, Object owner) throws HibernateException {
return value;
}

@Override
public final Object semiResolve(Object value, SharedSessionContractImplementor session, Object owner) throws HibernateException {
return value;
}

@Override
public final Type getSemiResolvedType(SessionFactoryImplementor factory) {
return this;
}

@Override
@SuppressWarnings({ "unchecked" })
public final Object replace(Object original, Object target, SharedSessionContractImplementor session, Object owner, Map copyCache) {
Expand Down
31 changes: 0 additions & 31 deletions hibernate-core/src/main/java/org/hibernate/type/AbstractType.java
Expand Up @@ -7,8 +7,6 @@
package org.hibernate.type;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.Objects;

Expand Down Expand Up @@ -79,30 +77,6 @@ public boolean isDirty(Object old, Object current, SharedSessionContractImplemen
return !isSame( old, current );
}

@Override
public Object hydrate(
ResultSet rs,
String[] names,
SharedSessionContractImplementor session,
Object owner)
throws HibernateException, SQLException {
// TODO: this is very suboptimal for some subclasses (namely components),
// since it does not take advantage of two-phase-load
return nullSafeGet(rs, names, session, owner);
}

@Override
public Object resolve(Object value, SharedSessionContractImplementor session, Object owner)
throws HibernateException {
return value;
}

@Override
public Object semiResolve(Object value, SharedSessionContractImplementor session, Object owner)
throws HibernateException {
return value;
}

@Override
public boolean isAnyType() {
return false;
Expand Down Expand Up @@ -139,11 +113,6 @@ public int getHashCode(Object x, SessionFactoryImplementor factory) {
return getHashCode(x );
}

@Override
public Type getSemiResolvedType(SessionFactoryImplementor factory) {
return this;
}

@Override
public Object replace(
Object original,
Expand Down
39 changes: 1 addition & 38 deletions hibernate-core/src/main/java/org/hibernate/type/AnyType.java
Expand Up @@ -236,37 +236,6 @@ public int getColumnSpan(Mapping session) {
return 2;
}

@Override
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
throws HibernateException, SQLException {
return resolveAny(
(String) discriminatorType.nullSafeGet( rs, names[0], session, owner ),
identifierType.nullSafeGet( rs, names[1], session, owner ),
session
);
}

@Override
public Object hydrate(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
throws HibernateException, SQLException {
final String entityName = (String) discriminatorType.nullSafeGet( rs, names[0], session, owner );
final Object id = identifierType.nullSafeGet( rs, names[1], session, owner );
return new ObjectTypeCacheEntry( entityName, id );
}

@Override
public Object resolve(Object value, SharedSessionContractImplementor session, Object owner) throws HibernateException {
final ObjectTypeCacheEntry holder = (ObjectTypeCacheEntry) value;
return resolveAny( holder.entityName, holder.id, session );
}

private Object resolveAny(String entityName, Object id, SharedSessionContractImplementor session)
throws HibernateException {
return entityName==null || id==null
? null
: session.internalLoad( entityName, id, eager, false );
}

@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session)
throws HibernateException, SQLException {
Expand Down Expand Up @@ -353,16 +322,10 @@ public Object replace(Object original, Object target, SharedSessionContractImple
}
}

@Override
public Object nullSafeGet(ResultSet rs, String name, SharedSessionContractImplementor session, Object owner) {
private Object nullSafeGet(ResultSet rs, String name, SharedSessionContractImplementor session, Object owner) {
throw new UnsupportedOperationException( "object is a multicolumn type" );
}

@Override
public Object semiResolve(Object value, SharedSessionContractImplementor session, Object owner) {
throw new UnsupportedOperationException( "any mappings may not form part of a property-ref" );
}

// CompositeType implementation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@Override
Expand Down

0 comments on commit cf36d17

Please sign in to comment.