Skip to content

DATAJPA-1446 - Clear JpaMetamodel CACHE when application context is c… #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.jpa.mapping.JpaMetamodelMappingContext;
import org.springframework.data.jpa.util.JpaMetamodel;
import org.springframework.data.util.StreamUtils;
import org.springframework.lang.Nullable;

Expand All @@ -39,6 +40,7 @@
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Sylvère Richard
* @since 1.6
*/
class JpaMetamodelMappingContextFactoryBean extends AbstractFactoryBean<JpaMetamodelMappingContext>
Expand Down Expand Up @@ -66,6 +68,11 @@ public Class<?> getObjectType() {
return JpaMetamodelMappingContext.class;
}

@Override
protected void destroyInstance(JpaMetamodelMappingContext instance) throws Exception {
JpaMetamodel.clearCache();
}

/*
* (non-Javadoc)
* @see org.springframework.beans.factory.config.AbstractFactoryBean#createInstance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
package org.springframework.data.jpa.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.ManagedType;
Expand All @@ -34,10 +34,11 @@
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Sylvère Richard
*/
public class JpaMetamodel {

private static final Map<Metamodel, JpaMetamodel> CACHE = new HashMap<>(4);
private static final Map<Metamodel, JpaMetamodel> CACHE = new ConcurrentHashMap<>(4);

private final Metamodel metamodel;

Expand All @@ -63,6 +64,10 @@ public static JpaMetamodel of(Metamodel metamodel) {
return CACHE.computeIfAbsent(metamodel, JpaMetamodel::new);
}

public static void clearCache() {
CACHE.clear();
}

/**
* Returns whether the given type is managed by the backing JPA {@link Metamodel}.
*
Expand All @@ -78,7 +83,7 @@ public boolean isJpaManaged(Class<?> type) {

/**
* Returns whether the attribute of given name and type is the single identifier attribute of the given entity.
*
*
* @param entity must not be {@literal null}.
* @param name must not be {@literal null}.
* @param attributeType must not be {@literal null}.
Expand All @@ -98,7 +103,7 @@ public boolean isSingleIdAttribute(Class<?> entity, String name, Class<?> attrib
/**
* Returns the {@link SingularAttribute} representing the identifier of the given {@link EntityType} if it contains a
* singular one.
*
*
* @param entityType must not be {@literal null}.
* @return
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@

/**
* Unit tests for {@link JpaMetamodel}.
*
*
* @author Oliver Gierke
* @author Sylvère Richard
*/
@RunWith(MockitoJUnitRunner.class)
public class JpaMetamodelUnitTests {
Expand All @@ -47,4 +48,13 @@ public void skipsEntityTypesWithoutJavaTypeForIdentifierLookup() {

assertThat(JpaMetamodel.of(metamodel).isSingleIdAttribute(Object.class, "id", Object.class)).isFalse();
}

@Test //DATAJPA-1446
public void cacheIsEffectiveUnlessCleared() {
JpaMetamodel model1 = JpaMetamodel.of(metamodel);
assertThat(model1).isEqualTo(JpaMetamodel.of(metamodel));

JpaMetamodel.clearCache();
assertThat(model1).isNotEqualTo(JpaMetamodel.of(metamodel));
}
}