Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions hibernate-core/src/main/java/org/hibernate/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,28 +329,20 @@ public interface Cache extends jakarta.persistence.Cache {
void evictRegion(String regionName);

/**
* {@inheritDoc}
* Evict all cached entity data.
*
* @apiNote This operation only affects cached data for entities, in keeping
* with the intent of the JPA specification, which only defines caching for
* entity data. To evict all data from every cache region, including cached
* collections, natural-id mappings, and cached query results, use
* collections, natural id mappings, and cached query results, use
* {@link #evictAllRegions()} instead.
*/
@Override
default void evictAll() {
// Evict only the "JPA cache", which is purely defined as the entity regions.
evictEntityData();
}
void evictAll();

/**
* Evict all cached data from every cache region.
* Evict all cached data from every cache region, including cached
* collections, natural id mappings, and cached query results.
*/
default void evictAllRegions() {
evictEntityData();
evictNaturalIdData();
evictCollectionData();
evictDefaultQueryRegion();
evictQueryRegions();
}
void evictAllRegions();
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public enum ConnectionAcquisitionMode {
AS_NEEDED;

public static ConnectionAcquisitionMode interpret(String value) {
return "immediate".equalsIgnoreCase( value ) || "immediately".equalsIgnoreCase( value )
return "immediate".equalsIgnoreCase( value )
|| "immediately".equalsIgnoreCase( value )
? IMMEDIATELY
: AS_NEEDED;
}
Expand All @@ -47,6 +48,5 @@ else if ( setting instanceof ConnectionAcquisitionMode mode ) {
final String value = setting.toString();
return isEmpty( value ) ? null : interpret( value );
}

}
}
18 changes: 8 additions & 10 deletions hibernate-core/src/main/java/org/hibernate/Hibernate.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
import org.hibernate.engine.jdbc.env.internal.NonContextualLobCreator;
import org.hibernate.engine.spi.PersistentAttributeInterceptor;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;
import org.hibernate.collection.spi.LazyInitializable;

import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable;
Expand Down Expand Up @@ -132,7 +130,7 @@ private Hibernate() {
*/
public static void initialize(Object proxy) throws HibernateException {
if ( proxy != null ) {
final LazyInitializer lazyInitializer = extractLazyInitializer( proxy );
final var lazyInitializer = extractLazyInitializer( proxy );
if ( lazyInitializer != null ) {
lazyInitializer.initialize();
}
Expand All @@ -157,7 +155,7 @@ else if ( isPersistentAttributeInterceptable( proxy ) ) {
* @return true if the argument is already initialized, or is not a proxy or collection
*/
public static boolean isInitialized(Object proxy) {
final LazyInitializer lazyInitializer = extractLazyInitializer( proxy );
final var lazyInitializer = extractLazyInitializer( proxy );
if ( lazyInitializer != null ) {
return !lazyInitializer.isUninitialized();
}
Expand Down Expand Up @@ -264,7 +262,7 @@ public static <T> T get(List<T> list, int key) {
*/
@SuppressWarnings("unchecked")
public static <T> Class<? extends T> getClass(T proxy) {
final LazyInitializer lazyInitializer = extractLazyInitializer( proxy );
final var lazyInitializer = extractLazyInitializer( proxy );
final Class<?> result =
lazyInitializer != null
? lazyInitializer.getImplementation().getClass()
Expand All @@ -287,7 +285,7 @@ public static <T> Class<? extends T> getClass(T proxy) {
*/
@SuppressWarnings("unchecked")
public static <T> Class<? extends T> getClassLazy(T proxy) {
final LazyInitializer lazyInitializer = extractLazyInitializer( proxy );
final var lazyInitializer = extractLazyInitializer( proxy );
final Class<?> result =
lazyInitializer != null
? lazyInitializer.getImplementationClass()
Expand Down Expand Up @@ -339,7 +337,7 @@ public static <E> boolean isPropertyInitialized(E entity, Attribute<? super E, ?
*/
public static boolean isPropertyInitialized(Object proxy, String attributeName) {
final Object entity;
final LazyInitializer lazyInitializer = extractLazyInitializer( proxy );
final var lazyInitializer = extractLazyInitializer( proxy );
if ( lazyInitializer != null ) {
if ( lazyInitializer.isUninitialized() ) {
return false;
Expand Down Expand Up @@ -382,7 +380,7 @@ public static <E> void initializeProperty(E entity, Attribute<? super E, ?> attr
* @see jakarta.persistence.PersistenceUnitUtil#load(Object, String)
*/
public static void initializeProperty(Object proxy, String attributeName) {
final LazyInitializer lazyInitializer = extractLazyInitializer( proxy );
final var lazyInitializer = extractLazyInitializer( proxy );
final Object entity = lazyInitializer != null ? lazyInitializer.getImplementation() : proxy;
if ( isPersistentAttributeInterceptable( entity ) ) {
getAttributeInterceptor( entity ).readObject( entity, attributeName, null );
Expand All @@ -401,7 +399,7 @@ public static void initializeProperty(Object proxy, String attributeName) {
* uninitialized proxy that is not associated with an open session.
*/
public static Object unproxy(Object proxy) {
final LazyInitializer lazyInitializer = extractLazyInitializer( proxy );
final var lazyInitializer = extractLazyInitializer( proxy );
return lazyInitializer != null ? lazyInitializer.getImplementation() : proxy;
}

Expand Down Expand Up @@ -439,7 +437,7 @@ public static <T> T unproxy(T proxy, Class<T> entityClass) {
*/
@SuppressWarnings("unchecked")
public static <E> E createDetachedProxy(SessionFactory sessionFactory, Class<E> entityClass, Object id) {
final EntityPersister persister =
final var persister =
sessionFactory.unwrap( SessionFactoryImplementor.class )
.getMappingMetamodel()
.findEntityDescriptor( entityClass );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ public boolean isVersioned() {
}

@Override
public Comparator getOwnerVersionComparator() {
if ( !isVersioned() ) {
public Comparator<?> getOwnerVersionComparator() {
if ( isVersioned() ) {
final var type = (BasicType<?>) collectionDescriptor.getOwner().getVersion().getType();
return type.getJavaTypeDescriptor().getComparator();
}
else {
return null;
}
return ( (BasicType<?>) collectionDescriptor.getOwner().getVersion().getType() ).getJavaTypeDescriptor().getComparator();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package org.hibernate.cache.cfg.internal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -22,6 +21,9 @@
import org.hibernate.metamodel.model.domain.NavigableRole;
import org.hibernate.type.BasicType;

import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableList;

/**
* DomainDataRegionConfig implementation
*
Expand Down Expand Up @@ -89,25 +91,27 @@ public Builder addEntityConfig(PersistentClass bootEntityDescriptor, AccessType

// todo (5.3) : this is another place where having `BootstrapContext` / `TypeConfiguration` helps
// would allow us to delay the attempt to resolve the comparator (usual timing issues wrt Type resolution)
final NavigableRole rootEntityName = new NavigableRole( bootEntityDescriptor.getRootClass().getEntityName() );
final EntityDataCachingConfigImpl entityDataCachingConfig = entityConfigsByRootName.computeIfAbsent(
final var rootEntityName = new NavigableRole( bootEntityDescriptor.getRootClass().getEntityName() );
final var entityDataCachingConfig = entityConfigsByRootName.computeIfAbsent(
rootEntityName,
x -> new EntityDataCachingConfigImpl(
rootEntityName,
bootEntityDescriptor.isVersioned()
? () -> ( (BasicType<?>) bootEntityDescriptor.getVersion().getType() ).getJavaTypeDescriptor().getComparator()
? () -> {
final var type = (BasicType<?>) bootEntityDescriptor.getVersion().getType();
return type.getJavaTypeDescriptor().getComparator();
}
: null,
bootEntityDescriptor.isMutable(),
accessType
)
);

if ( bootEntityDescriptor == bootEntityDescriptor.getRootClass() ) {
entityDataCachingConfig.addCachedType( rootEntityName );
}
else {
entityDataCachingConfig.addCachedType( new NavigableRole( bootEntityDescriptor.getEntityName() ) );
}
final var cachedRole =
bootEntityDescriptor == bootEntityDescriptor.getRootClass()
? rootEntityName
: new NavigableRole( bootEntityDescriptor.getEntityName() );
entityDataCachingConfig.addCachedType( cachedRole );

return this;
}
Expand All @@ -123,7 +127,6 @@ public Builder addNaturalIdConfig(RootClass rootEntityDescriptor, AccessType acc
if ( naturalIdConfigs == null ) {
naturalIdConfigs = new ArrayList<>();
}

naturalIdConfigs.add( new NaturalIdDataCachingConfigImpl( rootEntityDescriptor, accessType ) );
return this;
}
Expand All @@ -133,7 +136,6 @@ public Builder addCollectionConfig(Collection collectionDescriptor, AccessType a
if ( collectionConfigs == null ) {
collectionConfigs = new ArrayList<>();
}

collectionConfigs.add( new CollectionDataCachingConfigImpl( collectionDescriptor, accessType ) );
return this;
}
Expand All @@ -147,17 +149,16 @@ public DomainDataRegionConfigImpl build() {
);
}

@SuppressWarnings("unchecked")
private <T extends DomainDataCachingConfig> List<T> finalize(Map configs) {
private <T extends DomainDataCachingConfig> List<T> finalize(Map<?,? extends T> configs) {
return configs == null
? Collections.emptyList()
: Collections.unmodifiableList( new ArrayList( configs.values() ) );
? emptyList()
: unmodifiableList( new ArrayList<>( configs.values() ) );
}

private <T extends DomainDataCachingConfig> List<T> finalize(List<T> configs) {
return configs == null
? Collections.emptyList()
: Collections.unmodifiableList( configs );
? emptyList()
: unmodifiableList( configs );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public class EntityDataCachingConfigImpl
extends AbstractDomainDataCachingConfig
implements EntityDataCachingConfig {
private final NavigableRole navigableRole;
private final Supplier<Comparator> versionComparatorAccess;
private final Supplier<Comparator<?>> versionComparatorAccess;
private final boolean isEntityMutable;

private final Set<NavigableRole> cachedTypes = new HashSet<>();

public EntityDataCachingConfigImpl(
NavigableRole rootEntityName,
Supplier<Comparator> versionComparatorAccess,
Supplier<Comparator<?>> versionComparatorAccess,
boolean isEntityMutable,
AccessType accessType) {
super( accessType );
Expand All @@ -37,7 +37,7 @@ public EntityDataCachingConfigImpl(
}

@Override
public Supplier<Comparator> getVersionComparatorAccess() {
public Supplier<Comparator<?>> getVersionComparatorAccess() {
return versionComparatorAccess;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public NaturalIdDataCachingConfigImpl(
super( accessType );
this.rootEntityDescriptor = rootEntityDescriptor;
this.navigableRole = new NavigableRole( rootEntityDescriptor.getEntityName() );

// sucks that we need to do this here. persister does the same "calculation"
// Sucks that we need to do this here. Persister does the same "calculation"
this.mutable = hasAnyMutableNaturalIdProps();
}

Expand All @@ -37,7 +36,6 @@ private boolean hasAnyMutableNaturalIdProps() {
return true;
}
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ public interface CollectionDataCachingConfig extends DomainDataCachingConfig {
/**
* The comparator to be used with the owning entity's version (if it has one).
*/
Comparator getOwnerVersionComparator();
Comparator<?> getOwnerVersionComparator();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface EntityDataCachingConfig extends DomainDataCachingConfig {
* version. If the entity is not versioned, then this method
* returns {@code null}.
*/
Supplier<Comparator> getVersionComparatorAccess();
Supplier<Comparator<?>> getVersionComparatorAccess();

/**
* The list of specific subclasses of the root that are actually
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public CacheKeyImplementation(
}

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

public Object getId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@
*/
package org.hibernate.cache.internal;

import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.action.internal.CollectionAction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.boot.spi.SessionFactoryOptions;
import org.hibernate.cache.spi.access.CollectionDataAccess;
import org.hibernate.cache.spi.access.SoftLock;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventSource;
import org.hibernate.event.spi.EventType;
import org.hibernate.event.spi.PostDeleteEvent;
Expand All @@ -26,7 +21,6 @@
import org.hibernate.event.spi.PostUpdateEvent;
import org.hibernate.event.spi.PostUpdateEventListener;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.metamodel.spi.MappingMetamodelImplementor;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.entity.EntityPersister;

Expand Down Expand Up @@ -84,25 +78,20 @@ public void onPostUpdate(PostUpdateEvent event) {
}

private void integrate(SessionFactoryImplementor sessionFactory) {
final SessionFactoryOptions sessionFactoryOptions = sessionFactory.getSessionFactoryOptions();
if ( !sessionFactoryOptions.isAutoEvictCollectionCache() ) {
// feature is disabled
return;
final var options = sessionFactory.getSessionFactoryOptions();
if ( options.isSecondLevelCacheEnabled()
&& options.isAutoEvictCollectionCache() ) {
final var eventListenerRegistry = sessionFactory.getEventListenerRegistry();
eventListenerRegistry.appendListeners( EventType.POST_INSERT, this );
eventListenerRegistry.appendListeners( EventType.POST_DELETE, this );
eventListenerRegistry.appendListeners( EventType.POST_UPDATE, this );
}
if ( !sessionFactoryOptions.isSecondLevelCacheEnabled() ) {
// Nothing to do, if caching is disabled
return;
}
final EventListenerRegistry eventListenerRegistry = sessionFactory.getEventListenerRegistry();
eventListenerRegistry.appendListeners( EventType.POST_INSERT, this );
eventListenerRegistry.appendListeners( EventType.POST_DELETE, this );
eventListenerRegistry.appendListeners( EventType.POST_UPDATE, this );
}

private void evictCache(Object entity, EntityPersister persister, EventSource session, Object[] oldState) {
try {
final MappingMetamodelImplementor metamodel = persister.getFactory().getMappingMetamodel();
final Set<String> roles = metamodel.getCollectionRolesByEntityParticipant( persister.getEntityName() );
final var metamodel = persister.getFactory().getMappingMetamodel();
final var roles = metamodel.getCollectionRolesByEntityParticipant( persister.getEntityName() );
if ( !isEmpty( roles ) ) {
for ( String role : roles ) {
evictCollection( entity, persister, oldState, metamodel.getCollectionDescriptor( role ), session );
Expand Down Expand Up @@ -132,8 +121,8 @@ private void evictCollection(
if ( L2CACHE_LOGGER.isTraceEnabled() ) {
L2CACHE_LOGGER.autoEvictingCollectionCacheByRole( collectionPersister.getRole() );
}
final CollectionDataAccess cacheAccessStrategy = collectionPersister.getCacheAccessStrategy();
final SoftLock softLock = cacheAccessStrategy.lockRegion();
final var cacheAccessStrategy = collectionPersister.getCacheAccessStrategy();
final var softLock = cacheAccessStrategy.lockRegion();
session.getActionQueue()
.registerProcess( (success, s) -> cacheAccessStrategy.unlockRegion( softLock ) );
}
Expand Down Expand Up @@ -190,7 +179,7 @@ private void evict(Object id, CollectionPersister collectionPersister, EventSour
L2CACHE_LOGGER.autoEvictingCollectionCache(
collectionInfoString( collectionPersister, id, collectionPersister.getFactory() ) );
}
final CollectionEvictCacheAction evictCacheAction =
final var evictCacheAction =
new CollectionEvictCacheAction( collectionPersister, null, id, session );
evictCacheAction.execute();
session.getActionQueue().registerProcess( evictCacheAction.getAfterTransactionCompletionProcess() );
Expand Down
Loading
Loading