HHH-9840 Refactor org.hibernate.cache.spi.CacheKey into an interface
This commit is contained in:
parent
ea82d09101
commit
9ac0a343ac
104
hibernate-core/src/main/java/org/hibernate/cache/internal/OldCacheKeyImplementation.java
vendored
Normal file
104
hibernate-core/src/main/java/org/hibernate/cache/internal/OldCacheKeyImplementation.java
vendored
Normal file
|
@ -0,0 +1,104 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.cache.internal;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.hibernate.cache.spi.CacheKey;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.internal.util.compare.EqualsHelper;
|
||||
import org.hibernate.type.Type;
|
||||
|
||||
/**
|
||||
* Allows multiple entity classes / collection roles to be stored in the same cache region. Also allows for composite
|
||||
* keys which do not properly implement equals()/hashCode().
|
||||
*
|
||||
* This was named org.hibernate.cache.spi.CacheKey in Hibernate until version 5.
|
||||
* Temporarily maintained as a reference while all components catch up with the refactoring to interface.
|
||||
*
|
||||
* @author Gavin King
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
@Deprecated
|
||||
public class OldCacheKeyImplementation implements CacheKey, Serializable {
|
||||
private final Serializable key;
|
||||
private final Type type;
|
||||
private final String entityOrRoleName;
|
||||
private final String tenantId;
|
||||
private final int hashCode;
|
||||
|
||||
/**
|
||||
* Construct a new key for a collection or entity instance.
|
||||
* Note that an entity name should always be the root entity
|
||||
* name, not a subclass entity name.
|
||||
*
|
||||
* @param id The identifier associated with the cached data
|
||||
* @param type The Hibernate type mapping
|
||||
* @param entityOrRoleName The entity or collection-role name.
|
||||
* @param tenantId The tenant identifier associated this data.
|
||||
* @param factory The session factory for which we are caching
|
||||
*/
|
||||
public OldCacheKeyImplementation(
|
||||
final Serializable id,
|
||||
final Type type,
|
||||
final String entityOrRoleName,
|
||||
final String tenantId,
|
||||
final SessionFactoryImplementor factory) {
|
||||
this.key = id;
|
||||
this.type = type;
|
||||
this.entityOrRoleName = entityOrRoleName;
|
||||
this.tenantId = tenantId;
|
||||
this.hashCode = calculateHashCode( type, factory );
|
||||
}
|
||||
|
||||
private int calculateHashCode(Type type, SessionFactoryImplementor factory) {
|
||||
int result = type.getHashCode( key, factory );
|
||||
result = 31 * result + (tenantId != null ? tenantId.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Serializable getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public String getEntityOrRoleName() {
|
||||
return entityOrRoleName;
|
||||
}
|
||||
|
||||
public String getTenantId() {
|
||||
return tenantId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if ( other == null ) {
|
||||
return false;
|
||||
}
|
||||
if ( this == other ) {
|
||||
return true;
|
||||
}
|
||||
if ( hashCode != other.hashCode() || !( other instanceof OldCacheKeyImplementation ) ) {
|
||||
//hashCode is part of this check since it is pre-calculated and hash must match for equals to be true
|
||||
return false;
|
||||
}
|
||||
final OldCacheKeyImplementation that = (OldCacheKeyImplementation) other;
|
||||
return EqualsHelper.equals( entityOrRoleName, that.entityOrRoleName )
|
||||
&& type.isEqual( key, that.key )
|
||||
&& EqualsHelper.equals( tenantId, that.tenantId );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return hashCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
// Used to be required for OSCache
|
||||
return entityOrRoleName + '#' + key.toString();
|
||||
}
|
||||
}
|
|
@ -8,10 +8,6 @@ package org.hibernate.cache.spi;
|
|||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.internal.util.compare.EqualsHelper;
|
||||
import org.hibernate.type.Type;
|
||||
|
||||
/**
|
||||
* Allows multiple entity classes / collection roles to be stored in the same cache region. Also allows for composite
|
||||
* keys which do not properly implement equals()/hashCode().
|
||||
|
@ -19,81 +15,12 @@ import org.hibernate.type.Type;
|
|||
* @author Gavin King
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class CacheKey implements Serializable {
|
||||
private final Serializable key;
|
||||
private final Type type;
|
||||
private final String entityOrRoleName;
|
||||
private final String tenantId;
|
||||
private final int hashCode;
|
||||
public interface CacheKey {
|
||||
|
||||
public Serializable getKey();
|
||||
|
||||
/**
|
||||
* Construct a new key for a collection or entity instance.
|
||||
* Note that an entity name should always be the root entity
|
||||
* name, not a subclass entity name.
|
||||
*
|
||||
* @param id The identifier associated with the cached data
|
||||
* @param type The Hibernate type mapping
|
||||
* @param entityOrRoleName The entity or collection-role name.
|
||||
* @param tenantId The tenant identifier associated this data.
|
||||
* @param factory The session factory for which we are caching
|
||||
*/
|
||||
public CacheKey(
|
||||
final Serializable id,
|
||||
final Type type,
|
||||
final String entityOrRoleName,
|
||||
final String tenantId,
|
||||
final SessionFactoryImplementor factory) {
|
||||
this.key = id;
|
||||
this.type = type;
|
||||
this.entityOrRoleName = entityOrRoleName;
|
||||
this.tenantId = tenantId;
|
||||
this.hashCode = calculateHashCode( type, factory );
|
||||
}
|
||||
public String getEntityOrRoleName();
|
||||
|
||||
private int calculateHashCode(Type type, SessionFactoryImplementor factory) {
|
||||
int result = type.getHashCode( key, factory );
|
||||
result = 31 * result + (tenantId != null ? tenantId.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
public String getTenantId();
|
||||
|
||||
public Serializable getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public String getEntityOrRoleName() {
|
||||
return entityOrRoleName;
|
||||
}
|
||||
|
||||
public String getTenantId() {
|
||||
return tenantId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if ( other == null ) {
|
||||
return false;
|
||||
}
|
||||
if ( this == other ) {
|
||||
return true;
|
||||
}
|
||||
if ( hashCode != other.hashCode() || !( other instanceof CacheKey ) ) {
|
||||
//hashCode is part of this check since it is pre-calculated and hash must match for equals to be true
|
||||
return false;
|
||||
}
|
||||
final CacheKey that = (CacheKey) other;
|
||||
return EqualsHelper.equals( entityOrRoleName, that.entityOrRoleName )
|
||||
&& type.isEqual( key, that.key )
|
||||
&& EqualsHelper.equals( tenantId, that.tenantId );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return hashCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
// Used to be required for OSCache
|
||||
return entityOrRoleName + '#' + key.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@ import org.hibernate.SessionException;
|
|||
import org.hibernate.SharedSessionContract;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.boot.spi.SessionFactoryOptions;
|
||||
import org.hibernate.cache.internal.OldCacheKeyImplementation;
|
||||
import org.hibernate.cache.spi.CacheKey;
|
||||
import org.hibernate.engine.jdbc.LobCreationContext;
|
||||
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
|
||||
|
@ -338,7 +339,7 @@ public abstract class AbstractSessionImpl
|
|||
|
||||
@Override
|
||||
public CacheKey generateCacheKey(Serializable id, Type type, String entityOrRoleName) {
|
||||
return new CacheKey( id, type, entityOrRoleName, getTenantIdentifier(), getFactory() );
|
||||
return new OldCacheKeyImplementation( id, type, entityOrRoleName, getTenantIdentifier(), getFactory() );
|
||||
}
|
||||
|
||||
private transient JdbcConnectionAccess jdbcConnectionAccess;
|
||||
|
|
|
@ -14,6 +14,7 @@ import java.util.concurrent.ConcurrentMap;
|
|||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.boot.spi.SessionFactoryOptions;
|
||||
import org.hibernate.cache.internal.OldCacheKeyImplementation;
|
||||
import org.hibernate.cache.spi.CacheKey;
|
||||
import org.hibernate.cache.spi.QueryCache;
|
||||
import org.hibernate.cache.spi.Region;
|
||||
|
@ -97,7 +98,7 @@ public class CacheImpl implements CacheImplementor {
|
|||
}
|
||||
|
||||
private CacheKey buildCacheKey(Serializable identifier, EntityPersister p) {
|
||||
return new CacheKey(
|
||||
return new OldCacheKeyImplementation(
|
||||
identifier,
|
||||
p.getIdentifierType(),
|
||||
p.getRootEntityName(),
|
||||
|
@ -175,7 +176,7 @@ public class CacheImpl implements CacheImplementor {
|
|||
}
|
||||
|
||||
private CacheKey buildCacheKey(Serializable ownerIdentifier, CollectionPersister p) {
|
||||
return new CacheKey(
|
||||
return new OldCacheKeyImplementation(
|
||||
ownerIdentifier,
|
||||
p.getKeyType(),
|
||||
p.getRole(),
|
||||
|
|
Loading…
Reference in New Issue