Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit efa9ba2

Browse files
cleanups to org.hibernate.cache and subpackages
1 parent ea64d5d commit efa9ba2

30 files changed

+338
-425
lines changed

‎hibernate-core/src/main/java/org/hibernate/Cache.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -329,28 +329,20 @@ public interface Cache extends jakarta.persistence.Cache {
329329
void evictRegion(String regionName);
330330

331331
/**
332-
* {@inheritDoc}
332+
* Evict all cached entity data.
333333
*
334334
* @apiNote This operation only affects cached data for entities, in keeping
335335
* with the intent of the JPA specification, which only defines caching for
336336
* entity data. To evict all data from every cache region, including cached
337-
* collections, natural-id mappings, and cached query results, use
337+
* collections, naturalid mappings, and cached query results, use
338338
* {@link #evictAllRegions()} instead.
339339
*/
340340
@Override
341-
default void evictAll() {
342-
// Evict only the "JPA cache", which is purely defined as the entity regions.
343-
evictEntityData();
344-
}
341+
void evictAll();
345342

346343
/**
347-
* Evict all cached data from every cache region.
344+
* Evict all cached data from every cache region, including cached
345+
* collections, natural id mappings, and cached query results.
348346
*/
349-
default void evictAllRegions() {
350-
evictEntityData();
351-
evictNaturalIdData();
352-
evictCollectionData();
353-
evictDefaultQueryRegion();
354-
evictQueryRegions();
355-
}
347+
void evictAllRegions();
356348
}

‎hibernate-core/src/main/java/org/hibernate/ConnectionAcquisitionMode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public enum ConnectionAcquisitionMode {
3131
AS_NEEDED;
3232

3333
public static ConnectionAcquisitionMode interpret(String value) {
34-
return "immediate".equalsIgnoreCase( value ) || "immediately".equalsIgnoreCase( value )
34+
return "immediate".equalsIgnoreCase( value )
35+
|| "immediately".equalsIgnoreCase( value )
3536
? IMMEDIATELY
3637
: AS_NEEDED;
3738
}
@@ -47,6 +48,5 @@ else if ( setting instanceof ConnectionAcquisitionMode mode ) {
4748
final String value = setting.toString();
4849
return isEmpty( value ) ? null : interpret( value );
4950
}
50-
5151
}
5252
}

‎hibernate-core/src/main/java/org/hibernate/Hibernate.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@
3636
import org.hibernate.engine.jdbc.env.internal.NonContextualLobCreator;
3737
import org.hibernate.engine.spi.PersistentAttributeInterceptor;
3838
import org.hibernate.engine.spi.SessionFactoryImplementor;
39-
import org.hibernate.persister.entity.EntityPersister;
4039
import org.hibernate.proxy.HibernateProxy;
41-
import org.hibernate.proxy.LazyInitializer;
4240
import org.hibernate.collection.spi.LazyInitializable;
4341

4442
import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable;
@@ -132,7 +130,7 @@ private Hibernate() {
132130
*/
133131
public static void initialize(Object proxy) throws HibernateException {
134132
if ( proxy != null ) {
135-
final LazyInitializer lazyInitializer = extractLazyInitializer( proxy );
133+
final var lazyInitializer = extractLazyInitializer( proxy );
136134
if ( lazyInitializer != null ) {
137135
lazyInitializer.initialize();
138136
}
@@ -157,7 +155,7 @@ else if ( isPersistentAttributeInterceptable( proxy ) ) {
157155
* @return true if the argument is already initialized, or is not a proxy or collection
158156
*/
159157
public static boolean isInitialized(Object proxy) {
160-
final LazyInitializer lazyInitializer = extractLazyInitializer( proxy );
158+
final var lazyInitializer = extractLazyInitializer( proxy );
161159
if ( lazyInitializer != null ) {
162160
return !lazyInitializer.isUninitialized();
163161
}
@@ -264,7 +262,7 @@ public static <T> T get(List<T> list, int key) {
264262
*/
265263
@SuppressWarnings("unchecked")
266264
public static <T> Class<? extends T> getClass(T proxy) {
267-
final LazyInitializer lazyInitializer = extractLazyInitializer( proxy );
265+
final var lazyInitializer = extractLazyInitializer( proxy );
268266
final Class<?> result =
269267
lazyInitializer != null
270268
? lazyInitializer.getImplementation().getClass()
@@ -287,7 +285,7 @@ public static <T> Class<? extends T> getClass(T proxy) {
287285
*/
288286
@SuppressWarnings("unchecked")
289287
public static <T> Class<? extends T> getClassLazy(T proxy) {
290-
final LazyInitializer lazyInitializer = extractLazyInitializer( proxy );
288+
final var lazyInitializer = extractLazyInitializer( proxy );
291289
final Class<?> result =
292290
lazyInitializer != null
293291
? lazyInitializer.getImplementationClass()
@@ -339,7 +337,7 @@ public static <E> boolean isPropertyInitialized(E entity, Attribute<? super E, ?
339337
*/
340338
public static boolean isPropertyInitialized(Object proxy, String attributeName) {
341339
final Object entity;
342-
final LazyInitializer lazyInitializer = extractLazyInitializer( proxy );
340+
final var lazyInitializer = extractLazyInitializer( proxy );
343341
if ( lazyInitializer != null ) {
344342
if ( lazyInitializer.isUninitialized() ) {
345343
return false;
@@ -382,7 +380,7 @@ public static <E> void initializeProperty(E entity, Attribute<? super E, ?> attr
382380
* @see jakarta.persistence.PersistenceUnitUtil#load(Object, String)
383381
*/
384382
public static void initializeProperty(Object proxy, String attributeName) {
385-
final LazyInitializer lazyInitializer = extractLazyInitializer( proxy );
383+
final var lazyInitializer = extractLazyInitializer( proxy );
386384
final Object entity = lazyInitializer != null ? lazyInitializer.getImplementation() : proxy;
387385
if ( isPersistentAttributeInterceptable( entity ) ) {
388386
getAttributeInterceptor( entity ).readObject( entity, attributeName, null );
@@ -401,7 +399,7 @@ public static void initializeProperty(Object proxy, String attributeName) {
401399
* uninitialized proxy that is not associated with an open session.
402400
*/
403401
public static Object unproxy(Object proxy) {
404-
final LazyInitializer lazyInitializer = extractLazyInitializer( proxy );
402+
final var lazyInitializer = extractLazyInitializer( proxy );
405403
return lazyInitializer != null ? lazyInitializer.getImplementation() : proxy;
406404
}
407405

@@ -439,7 +437,7 @@ public static <T> T unproxy(T proxy, Class<T> entityClass) {
439437
*/
440438
@SuppressWarnings("unchecked")
441439
public static <E> E createDetachedProxy(SessionFactory sessionFactory, Class<E> entityClass, Object id) {
442-
final EntityPersister persister =
440+
final var persister =
443441
sessionFactory.unwrap( SessionFactoryImplementor.class )
444442
.getMappingMetamodel()
445443
.findEntityDescriptor( entityClass );

‎hibernate-core/src/main/java/org/hibernate/cache/cfg/internal/CollectionDataCachingConfigImpl.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,14 @@ public boolean isVersioned() {
4040
}
4141

4242
@Override
43-
public Comparator getOwnerVersionComparator() {
44-
if ( !isVersioned() ) {
43+
public Comparator<?> getOwnerVersionComparator() {
44+
if ( isVersioned() ) {
45+
final var type = (BasicType<?>) collectionDescriptor.getOwner().getVersion().getType();
46+
return type.getJavaTypeDescriptor().getComparator();
47+
}
48+
else {
4549
return null;
4650
}
47-
return ( (BasicType<?>) collectionDescriptor.getOwner().getVersion().getType() ).getJavaTypeDescriptor().getComparator();
4851
}
4952

5053
@Override

‎hibernate-core/src/main/java/org/hibernate/cache/cfg/internal/DomainDataRegionConfigImpl.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package org.hibernate.cache.cfg.internal;
66

77
import java.util.ArrayList;
8-
import java.util.Collections;
98
import java.util.HashMap;
109
import java.util.List;
1110
import java.util.Map;
@@ -22,6 +21,9 @@
2221
import org.hibernate.metamodel.model.domain.NavigableRole;
2322
import org.hibernate.type.BasicType;
2423

24+
import static java.util.Collections.emptyList;
25+
import static java.util.Collections.unmodifiableList;
26+
2527
/**
2628
* DomainDataRegionConfig implementation
2729
*
@@ -89,25 +91,27 @@ public Builder addEntityConfig(PersistentClass bootEntityDescriptor, AccessType
8991

9092
// todo (5.3) : this is another place where having `BootstrapContext` / `TypeConfiguration` helps
9193
// would allow us to delay the attempt to resolve the comparator (usual timing issues wrt Type resolution)
92-
final NavigableRole rootEntityName = new NavigableRole( bootEntityDescriptor.getRootClass().getEntityName() );
93-
final EntityDataCachingConfigImpl entityDataCachingConfig = entityConfigsByRootName.computeIfAbsent(
94+
final var rootEntityName = new NavigableRole( bootEntityDescriptor.getRootClass().getEntityName() );
95+
final var entityDataCachingConfig = entityConfigsByRootName.computeIfAbsent(
9496
rootEntityName,
9597
x -> new EntityDataCachingConfigImpl(
9698
rootEntityName,
9799
bootEntityDescriptor.isVersioned()
98-
? () -> ( (BasicType<?>) bootEntityDescriptor.getVersion().getType() ).getJavaTypeDescriptor().getComparator()
100+
? () -> {
101+
final var type = (BasicType<?>) bootEntityDescriptor.getVersion().getType();
102+
return type.getJavaTypeDescriptor().getComparator();
103+
}
99104
: null,
100105
bootEntityDescriptor.isMutable(),
101106
accessType
102107
)
103108
);
104109

105-
if ( bootEntityDescriptor == bootEntityDescriptor.getRootClass() ) {
106-
entityDataCachingConfig.addCachedType( rootEntityName );
107-
}
108-
else {
109-
entityDataCachingConfig.addCachedType( new NavigableRole( bootEntityDescriptor.getEntityName() ) );
110-
}
110+
final var cachedRole =
111+
bootEntityDescriptor == bootEntityDescriptor.getRootClass()
112+
? rootEntityName
113+
: new NavigableRole( bootEntityDescriptor.getEntityName() );
114+
entityDataCachingConfig.addCachedType( cachedRole );
111115

112116
return this;
113117
}
@@ -123,7 +127,6 @@ public Builder addNaturalIdConfig(RootClass rootEntityDescriptor, AccessType acc
123127
if ( naturalIdConfigs == null ) {
124128
naturalIdConfigs = new ArrayList<>();
125129
}
126-
127130
naturalIdConfigs.add( new NaturalIdDataCachingConfigImpl( rootEntityDescriptor, accessType ) );
128131
return this;
129132
}
@@ -133,7 +136,6 @@ public Builder addCollectionConfig(Collection collectionDescriptor, AccessType a
133136
if ( collectionConfigs == null ) {
134137
collectionConfigs = new ArrayList<>();
135138
}
136-
137139
collectionConfigs.add( new CollectionDataCachingConfigImpl( collectionDescriptor, accessType ) );
138140
return this;
139141
}
@@ -147,17 +149,16 @@ public DomainDataRegionConfigImpl build() {
147149
);
148150
}
149151

150-
@SuppressWarnings("unchecked")
151-
private <T extends DomainDataCachingConfig> List<T> finalize(Map configs) {
152+
private <T extends DomainDataCachingConfig> List<T> finalize(Map<?,? extends T> configs) {
152153
return configs == null
153-
? Collections.emptyList()
154-
: Collections.unmodifiableList( new ArrayList( configs.values() ) );
154+
? emptyList()
155+
: unmodifiableList( new ArrayList<>( configs.values() ) );
155156
}
156157

157158
private <T extends DomainDataCachingConfig> List<T> finalize(List<T> configs) {
158159
return configs == null
159-
? Collections.emptyList()
160-
: Collections.unmodifiableList( configs );
160+
? emptyList()
161+
: unmodifiableList( configs );
161162
}
162163
}
163164

‎hibernate-core/src/main/java/org/hibernate/cache/cfg/internal/NaturalIdDataCachingConfigImpl.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public NaturalIdDataCachingConfigImpl(
2626
super( accessType );
2727
this.rootEntityDescriptor = rootEntityDescriptor;
2828
this.navigableRole = new NavigableRole( rootEntityDescriptor.getEntityName() );
29-
30-
// sucks that we need to do this here. persister does the same "calculation"
29+
// Sucks that we need to do this here. Persister does the same "calculation"
3130
this.mutable = hasAnyMutableNaturalIdProps();
3231
}
3332

@@ -37,7 +36,6 @@ private boolean hasAnyMutableNaturalIdProps() {
3736
return true;
3837
}
3938
}
40-
4139
return false;
4240
}
4341

‎hibernate-core/src/main/java/org/hibernate/cache/cfg/spi/CollectionDataCachingConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ public interface CollectionDataCachingConfig extends DomainDataCachingConfig {
1616
/**
1717
* The comparator to be used with the owning entity's version (if it has one).
1818
*/
19-
Comparator getOwnerVersionComparator();
19+
Comparator<?> getOwnerVersionComparator();
2020
}

‎hibernate-core/src/main/java/org/hibernate/cache/internal/CacheKeyImplementation.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ public CacheKeyImplementation(
7979
}
8080

8181
private static int calculateHashCode(Object id, Type type, String tenantId) {
82-
return 31 * type.getHashCode( id )
83-
+ ( tenantId != null ? tenantId.hashCode() : 0 );
82+
return 31 * type.getHashCode( id ) + Objects.hashCode( tenantId );
8483
}
8584

8685
public Object getId() {

‎hibernate-core/src/main/java/org/hibernate/cache/internal/CollectionCacheInvalidator.java

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,14 @@
44
*/
55
package org.hibernate.cache.internal;
66

7-
import java.util.Set;
87

98
import org.hibernate.HibernateException;
109
import org.hibernate.action.internal.CollectionAction;
1110
import org.hibernate.boot.Metadata;
1211
import org.hibernate.boot.spi.BootstrapContext;
13-
import org.hibernate.boot.spi.SessionFactoryOptions;
14-
import org.hibernate.cache.spi.access.CollectionDataAccess;
15-
import org.hibernate.cache.spi.access.SoftLock;
1612
import org.hibernate.collection.spi.PersistentCollection;
1713
import org.hibernate.engine.spi.SessionFactoryImplementor;
1814
import org.hibernate.engine.spi.SharedSessionContractImplementor;
19-
import org.hibernate.event.service.spi.EventListenerRegistry;
2015
import org.hibernate.event.spi.EventSource;
2116
import org.hibernate.event.spi.EventType;
2217
import org.hibernate.event.spi.PostDeleteEvent;
@@ -26,7 +21,6 @@
2621
import org.hibernate.event.spi.PostUpdateEvent;
2722
import org.hibernate.event.spi.PostUpdateEventListener;
2823
import org.hibernate.integrator.spi.Integrator;
29-
import org.hibernate.metamodel.spi.MappingMetamodelImplementor;
3024
import org.hibernate.persister.collection.CollectionPersister;
3125
import org.hibernate.persister.entity.EntityPersister;
3226

@@ -84,25 +78,20 @@ public void onPostUpdate(PostUpdateEvent event) {
8478
}
8579

8680
private void integrate(SessionFactoryImplementor sessionFactory) {
87-
final SessionFactoryOptions sessionFactoryOptions = sessionFactory.getSessionFactoryOptions();
88-
if ( !sessionFactoryOptions.isAutoEvictCollectionCache() ) {
89-
// feature is disabled
90-
return;
81+
final var options = sessionFactory.getSessionFactoryOptions();
82+
if ( options.isSecondLevelCacheEnabled()
83+
&& options.isAutoEvictCollectionCache() ) {
84+
final var eventListenerRegistry = sessionFactory.getEventListenerRegistry();
85+
eventListenerRegistry.appendListeners( EventType.POST_INSERT, this );
86+
eventListenerRegistry.appendListeners( EventType.POST_DELETE, this );
87+
eventListenerRegistry.appendListeners( EventType.POST_UPDATE, this );
9188
}
92-
if ( !sessionFactoryOptions.isSecondLevelCacheEnabled() ) {
93-
// Nothing to do, if caching is disabled
94-
return;
95-
}
96-
final EventListenerRegistry eventListenerRegistry = sessionFactory.getEventListenerRegistry();
97-
eventListenerRegistry.appendListeners( EventType.POST_INSERT, this );
98-
eventListenerRegistry.appendListeners( EventType.POST_DELETE, this );
99-
eventListenerRegistry.appendListeners( EventType.POST_UPDATE, this );
10089
}
10190

10291
private void evictCache(Object entity, EntityPersister persister, EventSource session, Object[] oldState) {
10392
try {
104-
final MappingMetamodelImplementor metamodel = persister.getFactory().getMappingMetamodel();
105-
final Set<String> roles = metamodel.getCollectionRolesByEntityParticipant( persister.getEntityName() );
93+
final var metamodel = persister.getFactory().getMappingMetamodel();
94+
final var roles = metamodel.getCollectionRolesByEntityParticipant( persister.getEntityName() );
10695
if ( !isEmpty( roles ) ) {
10796
for ( String role : roles ) {
10897
evictCollection( entity, persister, oldState, metamodel.getCollectionDescriptor( role ), session );
@@ -132,8 +121,8 @@ private void evictCollection(
132121
if ( L2CACHE_LOGGER.isTraceEnabled() ) {
133122
L2CACHE_LOGGER.autoEvictingCollectionCacheByRole( collectionPersister.getRole() );
134123
}
135-
final CollectionDataAccess cacheAccessStrategy = collectionPersister.getCacheAccessStrategy();
136-
final SoftLock softLock = cacheAccessStrategy.lockRegion();
124+
final var cacheAccessStrategy = collectionPersister.getCacheAccessStrategy();
125+
final var softLock = cacheAccessStrategy.lockRegion();
137126
session.getActionQueue()
138127
.registerProcess( (success, s) -> cacheAccessStrategy.unlockRegion( softLock ) );
139128
}
@@ -190,7 +179,7 @@ private void evict(Object id, CollectionPersister collectionPersister, EventSour
190179
L2CACHE_LOGGER.autoEvictingCollectionCache(
191180
collectionInfoString( collectionPersister, id, collectionPersister.getFactory() ) );
192181
}
193-
final CollectionEvictCacheAction evictCacheAction =
182+
final var evictCacheAction =
194183
new CollectionEvictCacheAction( collectionPersister, null, id, session );
195184
evictCacheAction.execute();
196185
session.getActionQueue().registerProcess( evictCacheAction.getAfterTransactionCompletionProcess() );

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /