Skip to content

Commit 9ac0a34

Browse files
committed
HHH-9840 Refactor org.hibernate.cache.spi.CacheKey into an interface
1 parent ea82d09 commit 9ac0a34

File tree

4 files changed

+114
-81
lines changed

4 files changed

+114
-81
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.cache.internal;
8+
9+
import java.io.Serializable;
10+
11+
import org.hibernate.cache.spi.CacheKey;
12+
import org.hibernate.engine.spi.SessionFactoryImplementor;
13+
import org.hibernate.internal.util.compare.EqualsHelper;
14+
import org.hibernate.type.Type;
15+
16+
/**
17+
* Allows multiple entity classes / collection roles to be stored in the same cache region. Also allows for composite
18+
* keys which do not properly implement equals()/hashCode().
19+
*
20+
* This was named org.hibernate.cache.spi.CacheKey in Hibernate until version 5.
21+
* Temporarily maintained as a reference while all components catch up with the refactoring to interface.
22+
*
23+
* @author Gavin King
24+
* @author Steve Ebersole
25+
*/
26+
@Deprecated
27+
public class OldCacheKeyImplementation implements CacheKey, Serializable {
28+
private final Serializable key;
29+
private final Type type;
30+
private final String entityOrRoleName;
31+
private final String tenantId;
32+
private final int hashCode;
33+
34+
/**
35+
* Construct a new key for a collection or entity instance.
36+
* Note that an entity name should always be the root entity
37+
* name, not a subclass entity name.
38+
*
39+
* @param id The identifier associated with the cached data
40+
* @param type The Hibernate type mapping
41+
* @param entityOrRoleName The entity or collection-role name.
42+
* @param tenantId The tenant identifier associated this data.
43+
* @param factory The session factory for which we are caching
44+
*/
45+
public OldCacheKeyImplementation(
46+
final Serializable id,
47+
final Type type,
48+
final String entityOrRoleName,
49+
final String tenantId,
50+
final SessionFactoryImplementor factory) {
51+
this.key = id;
52+
this.type = type;
53+
this.entityOrRoleName = entityOrRoleName;
54+
this.tenantId = tenantId;
55+
this.hashCode = calculateHashCode( type, factory );
56+
}
57+
58+
private int calculateHashCode(Type type, SessionFactoryImplementor factory) {
59+
int result = type.getHashCode( key, factory );
60+
result = 31 * result + (tenantId != null ? tenantId.hashCode() : 0);
61+
return result;
62+
}
63+
64+
public Serializable getKey() {
65+
return key;
66+
}
67+
68+
public String getEntityOrRoleName() {
69+
return entityOrRoleName;
70+
}
71+
72+
public String getTenantId() {
73+
return tenantId;
74+
}
75+
76+
@Override
77+
public boolean equals(Object other) {
78+
if ( other == null ) {
79+
return false;
80+
}
81+
if ( this == other ) {
82+
return true;
83+
}
84+
if ( hashCode != other.hashCode() || !( other instanceof OldCacheKeyImplementation ) ) {
85+
//hashCode is part of this check since it is pre-calculated and hash must match for equals to be true
86+
return false;
87+
}
88+
final OldCacheKeyImplementation that = (OldCacheKeyImplementation) other;
89+
return EqualsHelper.equals( entityOrRoleName, that.entityOrRoleName )
90+
&& type.isEqual( key, that.key )
91+
&& EqualsHelper.equals( tenantId, that.tenantId );
92+
}
93+
94+
@Override
95+
public int hashCode() {
96+
return hashCode;
97+
}
98+
99+
@Override
100+
public String toString() {
101+
// Used to be required for OSCache
102+
return entityOrRoleName + '#' + key.toString();
103+
}
104+
}

hibernate-core/src/main/java/org/hibernate/cache/spi/CacheKey.java

Lines changed: 5 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -8,92 +8,19 @@
88

99
import java.io.Serializable;
1010

11-
import org.hibernate.engine.spi.SessionFactoryImplementor;
12-
import org.hibernate.internal.util.compare.EqualsHelper;
13-
import org.hibernate.type.Type;
14-
1511
/**
1612
* Allows multiple entity classes / collection roles to be stored in the same cache region. Also allows for composite
1713
* keys which do not properly implement equals()/hashCode().
1814
*
1915
* @author Gavin King
2016
* @author Steve Ebersole
2117
*/
22-
public class CacheKey implements Serializable {
23-
private final Serializable key;
24-
private final Type type;
25-
private final String entityOrRoleName;
26-
private final String tenantId;
27-
private final int hashCode;
28-
29-
/**
30-
* Construct a new key for a collection or entity instance.
31-
* Note that an entity name should always be the root entity
32-
* name, not a subclass entity name.
33-
*
34-
* @param id The identifier associated with the cached data
35-
* @param type The Hibernate type mapping
36-
* @param entityOrRoleName The entity or collection-role name.
37-
* @param tenantId The tenant identifier associated this data.
38-
* @param factory The session factory for which we are caching
39-
*/
40-
public CacheKey(
41-
final Serializable id,
42-
final Type type,
43-
final String entityOrRoleName,
44-
final String tenantId,
45-
final SessionFactoryImplementor factory) {
46-
this.key = id;
47-
this.type = type;
48-
this.entityOrRoleName = entityOrRoleName;
49-
this.tenantId = tenantId;
50-
this.hashCode = calculateHashCode( type, factory );
51-
}
52-
53-
private int calculateHashCode(Type type, SessionFactoryImplementor factory) {
54-
int result = type.getHashCode( key, factory );
55-
result = 31 * result + (tenantId != null ? tenantId.hashCode() : 0);
56-
return result;
57-
}
58-
59-
public Serializable getKey() {
60-
return key;
61-
}
62-
63-
public String getEntityOrRoleName() {
64-
return entityOrRoleName;
65-
}
66-
67-
public String getTenantId() {
68-
return tenantId;
69-
}
18+
public interface CacheKey {
19+
20+
public Serializable getKey();
7021

71-
@Override
72-
public boolean equals(Object other) {
73-
if ( other == null ) {
74-
return false;
75-
}
76-
if ( this == other ) {
77-
return true;
78-
}
79-
if ( hashCode != other.hashCode() || !( other instanceof CacheKey ) ) {
80-
//hashCode is part of this check since it is pre-calculated and hash must match for equals to be true
81-
return false;
82-
}
83-
final CacheKey that = (CacheKey) other;
84-
return EqualsHelper.equals( entityOrRoleName, that.entityOrRoleName )
85-
&& type.isEqual( key, that.key )
86-
&& EqualsHelper.equals( tenantId, that.tenantId );
87-
}
22+
public String getEntityOrRoleName();
8823

89-
@Override
90-
public int hashCode() {
91-
return hashCode;
92-
}
24+
public String getTenantId();
9325

94-
@Override
95-
public String toString() {
96-
// Used to be required for OSCache
97-
return entityOrRoleName + '#' + key.toString();
98-
}
9926
}

hibernate-core/src/main/java/org/hibernate/internal/AbstractSessionImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.hibernate.SharedSessionContract;
2727
import org.hibernate.Transaction;
2828
import org.hibernate.boot.spi.SessionFactoryOptions;
29+
import org.hibernate.cache.internal.OldCacheKeyImplementation;
2930
import org.hibernate.cache.spi.CacheKey;
3031
import org.hibernate.engine.jdbc.LobCreationContext;
3132
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
@@ -338,7 +339,7 @@ public EntityKey generateEntityKey(Serializable id, EntityPersister persister) {
338339

339340
@Override
340341
public CacheKey generateCacheKey(Serializable id, Type type, String entityOrRoleName) {
341-
return new CacheKey( id, type, entityOrRoleName, getTenantIdentifier(), getFactory() );
342+
return new OldCacheKeyImplementation( id, type, entityOrRoleName, getTenantIdentifier(), getFactory() );
342343
}
343344

344345
private transient JdbcConnectionAccess jdbcConnectionAccess;

hibernate-core/src/main/java/org/hibernate/internal/CacheImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import org.hibernate.HibernateException;
1616
import org.hibernate.boot.spi.SessionFactoryOptions;
17+
import org.hibernate.cache.internal.OldCacheKeyImplementation;
1718
import org.hibernate.cache.spi.CacheKey;
1819
import org.hibernate.cache.spi.QueryCache;
1920
import org.hibernate.cache.spi.Region;
@@ -97,7 +98,7 @@ public void evictEntity(String entityName, Serializable identifier) {
9798
}
9899

99100
private CacheKey buildCacheKey(Serializable identifier, EntityPersister p) {
100-
return new CacheKey(
101+
return new OldCacheKeyImplementation(
101102
identifier,
102103
p.getIdentifierType(),
103104
p.getRootEntityName(),
@@ -175,7 +176,7 @@ public void evictCollection(String role, Serializable ownerIdentifier) {
175176
}
176177

177178
private CacheKey buildCacheKey(Serializable ownerIdentifier, CollectionPersister p) {
178-
return new CacheKey(
179+
return new OldCacheKeyImplementation(
179180
ownerIdentifier,
180181
p.getKeyType(),
181182
p.getRole(),

0 commit comments

Comments
 (0)