Skip to content

Commit 9c9ff76

Browse files
rvansagbadner
authored andcommitted
HHH-11083 WrongClassException using Infinispan and sharing cache regions
* DefaultCacheKeysFactory implements CacheKeysFactory, therefore it can be used in hibernate.cache.keys_factory * Use DefaultCacheKeysFactory by default * Add "default" and "simple" as short names for those factories (cherry picked from commit f744f89)
1 parent 4f94b46 commit 9c9ff76

24 files changed

+177
-73
lines changed

hibernate-core/src/main/java/org/hibernate/boot/registry/selector/internal/StrategySelectorBuilder.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import org.hibernate.boot.registry.selector.StrategyRegistrationProvider;
2121
import org.hibernate.boot.registry.selector.spi.StrategySelectionException;
2222
import org.hibernate.boot.registry.selector.spi.StrategySelector;
23+
import org.hibernate.cache.internal.DefaultCacheKeysFactory;
24+
import org.hibernate.cache.internal.SimpleCacheKeysFactory;
25+
import org.hibernate.cache.spi.CacheKeysFactory;
2326
import org.hibernate.dialect.CUBRIDDialect;
2427
import org.hibernate.dialect.Cache71Dialect;
2528
import org.hibernate.dialect.DB2390Dialect;
@@ -157,6 +160,7 @@ public StrategySelector buildSelector(ClassLoaderService classLoaderService) {
157160
addMultiTableBulkIdStrategies( strategySelector );
158161
addEntityCopyObserverStrategies( strategySelector );
159162
addImplicitNamingStrategies( strategySelector );
163+
addCacheKeysFactories( strategySelector );
160164

161165
// apply auto-discovered registrations
162166
for ( StrategyRegistrationProvider provider : classLoaderService.loadJavaServices( StrategyRegistrationProvider.class ) ) {
@@ -436,4 +440,17 @@ private void addImplicitNamingStrategies(StrategySelectorImpl strategySelector)
436440
ImplicitNamingStrategyComponentPathImpl.class
437441
);
438442
}
443+
444+
private void addCacheKeysFactories(StrategySelectorImpl strategySelector) {
445+
strategySelector.registerStrategyImplementor(
446+
CacheKeysFactory.class,
447+
DefaultCacheKeysFactory.SHORT_NAME,
448+
DefaultCacheKeysFactory.class
449+
);
450+
strategySelector.registerStrategyImplementor(
451+
CacheKeysFactory.class,
452+
SimpleCacheKeysFactory.SHORT_NAME,
453+
SimpleCacheKeysFactory.class
454+
);
455+
}
439456
}

hibernate-core/src/main/java/org/hibernate/cache/internal/DefaultCacheKeysFactory.java

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,61 +38,61 @@
3838
* @author Sanne Grinovero
3939
* @since 5.0
4040
*/
41-
public class DefaultCacheKeysFactory {
41+
public class DefaultCacheKeysFactory implements CacheKeysFactory {
42+
public static final String SHORT_NAME = "default";
43+
public static final DefaultCacheKeysFactory INSTANCE = new DefaultCacheKeysFactory();
4244

43-
public static Object createCollectionKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
45+
public static Object staticCreateCollectionKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
4446
return new OldCacheKeyImplementation( id, persister.getKeyType(), persister.getRole(), tenantIdentifier, factory );
4547
}
4648

47-
public static Object createEntityKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
49+
public static Object staticCreateEntityKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
4850
return new OldCacheKeyImplementation( id, persister.getIdentifierType(), persister.getRootEntityName(), tenantIdentifier, factory );
4951
}
5052

51-
public static Object createNaturalIdKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session) {
53+
public static Object staticCreateNaturalIdKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session) {
5254
return new OldNaturalIdCacheKey( naturalIdValues, persister.getPropertyTypes(), persister.getNaturalIdentifierProperties(), persister.getRootEntityName(), session );
5355
}
5456

55-
public static Object getEntityId(Object cacheKey) {
57+
public static Object staticGetEntityId(Object cacheKey) {
5658
return ((OldCacheKeyImplementation) cacheKey).getId();
5759
}
5860

59-
public static Object getCollectionId(Object cacheKey) {
61+
public static Object staticGetCollectionId(Object cacheKey) {
6062
return ((OldCacheKeyImplementation) cacheKey).getId();
6163
}
6264

63-
public static Object[] getNaturalIdValues(Object cacheKey) {
65+
public static Object[] staticGetNaturalIdValues(Object cacheKey) {
6466
return ((OldNaturalIdCacheKey) cacheKey).getNaturalIdValues();
6567
}
6668

67-
public static CacheKeysFactory INSTANCE = new CacheKeysFactory() {
68-
@Override
69-
public Object createCollectionKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
70-
return DefaultCacheKeysFactory.createCollectionKey(id, persister, factory, tenantIdentifier);
71-
}
69+
@Override
70+
public Object createCollectionKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
71+
return staticCreateCollectionKey(id, persister, factory, tenantIdentifier);
72+
}
7273

73-
@Override
74-
public Object createEntityKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
75-
return DefaultCacheKeysFactory.createEntityKey(id, persister, factory, tenantIdentifier);
76-
}
74+
@Override
75+
public Object createEntityKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
76+
return staticCreateEntityKey(id, persister, factory, tenantIdentifier);
77+
}
7778

78-
@Override
79-
public Object createNaturalIdKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session) {
80-
return DefaultCacheKeysFactory.createNaturalIdKey(naturalIdValues, persister, session);
81-
}
79+
@Override
80+
public Object createNaturalIdKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session) {
81+
return staticCreateNaturalIdKey(naturalIdValues, persister, session);
82+
}
8283

83-
@Override
84-
public Object getEntityId(Object cacheKey) {
85-
return DefaultCacheKeysFactory.getEntityId(cacheKey);
86-
}
84+
@Override
85+
public Object getEntityId(Object cacheKey) {
86+
return staticGetEntityId(cacheKey);
87+
}
8788

88-
@Override
89-
public Object getCollectionId(Object cacheKey) {
90-
return DefaultCacheKeysFactory.getCollectionId(cacheKey);
91-
}
89+
@Override
90+
public Object getCollectionId(Object cacheKey) {
91+
return staticGetCollectionId(cacheKey);
92+
}
9293

93-
@Override
94-
public Object[] getNaturalIdValues(Object cacheKey) {
95-
return DefaultCacheKeysFactory.getNaturalIdValues(cacheKey);
96-
}
97-
};
94+
@Override
95+
public Object[] getNaturalIdValues(Object cacheKey) {
96+
return staticGetNaturalIdValues(cacheKey);
97+
}
9898
}

hibernate-core/src/main/java/org/hibernate/cache/internal/SimpleCacheKeysFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
* @author Radim Vansa <rvansa@redhat.com>
1919
*/
2020
public class SimpleCacheKeysFactory implements CacheKeysFactory {
21-
22-
public static CacheKeysFactory INSTANCE = new SimpleCacheKeysFactory();
21+
public static final String SHORT_NAME = "simple";
22+
public static CacheKeysFactory INSTANCE = new SimpleCacheKeysFactory();
2323

2424
@Override
2525
public Object createCollectionKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {

hibernate-core/src/test/java/org/hibernate/cache/spi/NaturalIdCacheKeyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
6161
}
6262
});
6363

64-
final OldNaturalIdCacheKey key = (OldNaturalIdCacheKey) DefaultCacheKeysFactory.createNaturalIdKey( new Object[] {"a", "b", "c"}, entityPersister, sessionImplementor );
64+
final OldNaturalIdCacheKey key = (OldNaturalIdCacheKey) DefaultCacheKeysFactory.staticCreateNaturalIdKey( new Object[] {"a", "b", "c"}, entityPersister, sessionImplementor );
6565

6666
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
6767
final ObjectOutputStream oos = new ObjectOutputStream(baos);

hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/nonstop/NonstopAwareCollectionRegionAccessStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ public void unlockRegion(SoftLock lock) throws CacheException {
163163

164164
@Override
165165
public Object generateCacheKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
166-
return DefaultCacheKeysFactory.createCollectionKey( id, persister, factory, tenantIdentifier );
166+
return DefaultCacheKeysFactory.staticCreateCollectionKey( id, persister, factory, tenantIdentifier );
167167
}
168168

169169
@Override
170170
public Object getCacheKeyId(Object cacheKey) {
171-
return DefaultCacheKeysFactory.getCollectionId(cacheKey);
171+
return DefaultCacheKeysFactory.staticGetCollectionId(cacheKey);
172172
}
173173
}

hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/nonstop/NonstopAwareEntityRegionAccessStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,11 @@ public boolean update(SessionImplementor session, Object key, Object value, Obje
209209

210210
@Override
211211
public Object generateCacheKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
212-
return DefaultCacheKeysFactory.createEntityKey( id, persister, factory, tenantIdentifier );
212+
return DefaultCacheKeysFactory.staticCreateEntityKey( id, persister, factory, tenantIdentifier );
213213
}
214214

215215
@Override
216216
public Object getCacheKeyId(Object cacheKey) {
217-
return DefaultCacheKeysFactory.getEntityId(cacheKey);
217+
return DefaultCacheKeysFactory.staticGetEntityId(cacheKey);
218218
}
219219
}

hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/nonstop/NonstopAwareNaturalIdRegionAccessStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,11 @@ public void unlockRegion(SoftLock lock) throws CacheException {
206206

207207
@Override
208208
public Object generateCacheKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session) {
209-
return DefaultCacheKeysFactory.createNaturalIdKey( naturalIdValues, persister, session );
209+
return DefaultCacheKeysFactory.staticCreateNaturalIdKey( naturalIdValues, persister, session );
210210
}
211211

212212
@Override
213213
public Object[] getNaturalIdValues(Object cacheKey) {
214-
return DefaultCacheKeysFactory.getNaturalIdValues(cacheKey);
214+
return DefaultCacheKeysFactory.staticGetNaturalIdValues(cacheKey);
215215
}
216216
}

hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/NonStrictReadWriteEhcacheCollectionRegionAccessStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ public void remove(SessionImplementor session, Object key) throws CacheException
8686

8787
@Override
8888
public Object generateCacheKey(Object id, CollectionPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
89-
return DefaultCacheKeysFactory.createCollectionKey( id, persister, factory, tenantIdentifier );
89+
return DefaultCacheKeysFactory.staticCreateCollectionKey( id, persister, factory, tenantIdentifier );
9090
}
9191

9292
@Override
9393
public Object getCacheKeyId(Object cacheKey) {
94-
return DefaultCacheKeysFactory.getCollectionId( cacheKey );
94+
return DefaultCacheKeysFactory.staticGetCollectionId( cacheKey );
9595
}
9696
}

hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/NonStrictReadWriteEhcacheEntityRegionAccessStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,11 @@ public void remove(SessionImplementor session, Object key) throws CacheException
125125

126126
@Override
127127
public Object generateCacheKey(Object id, EntityPersister persister, SessionFactoryImplementor factory, String tenantIdentifier) {
128-
return DefaultCacheKeysFactory.createEntityKey( id, persister, factory, tenantIdentifier );
128+
return DefaultCacheKeysFactory.staticCreateEntityKey( id, persister, factory, tenantIdentifier );
129129
}
130130

131131
@Override
132132
public Object getCacheKeyId(Object cacheKey) {
133-
return DefaultCacheKeysFactory.getEntityId( cacheKey );
133+
return DefaultCacheKeysFactory.staticGetEntityId( cacheKey );
134134
}
135135
}

hibernate-ehcache/src/main/java/org/hibernate/cache/ehcache/internal/strategy/NonStrictReadWriteEhcacheNaturalIdRegionAccessStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ public void remove(SessionImplementor session, Object key) throws CacheException
122122

123123
@Override
124124
public Object generateCacheKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor session) {
125-
return DefaultCacheKeysFactory.createNaturalIdKey(naturalIdValues, persister, session);
125+
return DefaultCacheKeysFactory.staticCreateNaturalIdKey(naturalIdValues, persister, session);
126126
}
127127

128128
@Override
129129
public Object[] getNaturalIdValues(Object cacheKey) {
130-
return DefaultCacheKeysFactory.getNaturalIdValues( cacheKey );
130+
return DefaultCacheKeysFactory.staticGetNaturalIdValues( cacheKey );
131131
}
132132
}

0 commit comments

Comments
 (0)