Re-add AvailableSettings.JPA_METAMODEL_GENERATION and re-enable metamodel tests

This commit is contained in:
Christian Beikov 2021-12-22 10:03:37 +01:00
parent 195df69019
commit 1184a5963b
20 changed files with 242 additions and 219 deletions

View File

@ -1118,6 +1118,15 @@ XML configuration file to use to configure Hibernate.
If true, the persistence context will be discarded (think `clear()` when the method is called).
Otherwise, the persistence context will stay alive till the transaction completion: all objects will remain managed, and any change will be synchronized with the database (default to false, ie wait for transaction completion).
`*hibernate.ejb.metamodel.population*` (e.g. `enabled` or `disabled`, or `ignoreUnsupported` (default value))::
Setting that indicates whether to build the Jakarta Persistence types.
+
Accepts three values:
+
enabled::: Do the build.
disabled::: Do not do the build.
ignoreUnsupported::: Do the build, but ignore any non-Jakarta Persistence features that would otherwise result in a failure (e.g. `@Any` annotation).
`*hibernate.jpa.static_metamodel.population*` (e.g. `enabled` or `disabled`, or `skipUnsupported` (default value))::
Setting that controls whether we seek out Jakarta Persistence _static metamodel_ classes and populate them.
+

View File

@ -235,6 +235,23 @@ public interface AvailableSettings {
*/
String TC_CLASSLOADER = "hibernate.classLoader.tccl_lookup_precedence";
/**
* Setting that indicates whether to build the JPA types. Accepts
* 3 values:<ul>
* <li>
* <b>enabled</b> - Do the build
* </li>
* <li>
* <b>disabled</b> - Do not do the build
* </li>
* <li>
* <b>ignoreUnsupported</b> - Do the build, but ignore any non-JPA features that would otherwise
* result in a failure.
* </li>
* </ul>
*/
String JPA_METAMODEL_POPULATION = "hibernate.jpa.metamodel.population";
/**
* Setting that controls whether we seek out JPA "static metamodel" classes and populate them. Accepts
* 3 values:<ul>

View File

@ -239,7 +239,7 @@ public class AttributeFactory {
final EmbeddableTypeImpl<Y> embeddableType;
if ( component.isDynamic() ) {
final JavaType javaTypeDescriptor = context.getJavaTypeDescriptorRegistry().getDescriptor( Map.class );
final JavaType javaTypeDescriptor = context.getJavaTypeDescriptorRegistry().getDescriptor( java.util.Map.class );
embeddableType = new EmbeddableTypeImpl<>(
javaTypeDescriptor,

View File

@ -0,0 +1,41 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.metamodel.internal;
import java.util.Map;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.internal.util.config.ConfigurationHelper;
/**
* @author Steve Ebersole
*/
public enum JpaMetaModelPopulationSetting {
ENABLED,
DISABLED,
IGNORE_UNSUPPORTED;
public static JpaMetaModelPopulationSetting parse(String setting) {
if ( "enabled".equalsIgnoreCase( setting ) ) {
return ENABLED;
}
else if ( "disabled".equalsIgnoreCase( setting ) ) {
return DISABLED;
}
else {
return IGNORE_UNSUPPORTED;
}
}
public static JpaMetaModelPopulationSetting determineJpaMetaModelPopulationSetting(Map configurationValues) {
String setting = ConfigurationHelper.getString(
AvailableSettings.JPA_METAMODEL_POPULATION,
configurationValues
);
return JpaMetaModelPopulationSetting.parse( setting );
}
}

View File

@ -46,7 +46,7 @@ public enum JpaStaticMetaModelPopulationSetting {
}
}
public static JpaStaticMetaModelPopulationSetting determineJpaMetaModelPopulationSetting(Map configurationValues) {
public static JpaStaticMetaModelPopulationSetting determineJpaStaticMetaModelPopulationSetting(Map configurationValues) {
return parse( determineSetting( configurationValues ) );
}

View File

@ -75,30 +75,30 @@ public class MetadataContext {
private final JpaMetamodel jpaMetamodel;
private final RuntimeModelCreationContext runtimeModelCreationContext;
private Set<MappedSuperclass> knownMappedSuperclasses;
private TypeConfiguration typeConfiguration;
private final Set<MappedSuperclass> knownMappedSuperclasses;
private final TypeConfiguration typeConfiguration;
private final JpaStaticMetaModelPopulationSetting jpaStaticMetaModelPopulationSetting;
private final AttributeFactory attributeFactory = new AttributeFactory( this );
private Map<Class<?>, EntityDomainType<?>> entityTypes = new HashMap<>();
private Map<String, EntityDomainType<?>> entityTypesByEntityName = new HashMap<>();
private Map<PersistentClass, EntityDomainType<?>> entityTypesByPersistentClass = new HashMap<>();
private final Map<Class<?>, EntityDomainType<?>> entityTypes = new HashMap<>();
private final Map<String, EntityDomainType<?>> entityTypesByEntityName = new HashMap<>();
private final Map<PersistentClass, EntityDomainType<?>> entityTypesByPersistentClass = new HashMap<>();
private Map<Class, EmbeddableDomainType<?>> embeddables = new HashMap<>();
private Map<Class, List<EmbeddableDomainType<?>>> embeddablesToProcess = new HashMap<>();
private Map<EmbeddableDomainType<?>, Component> componentByEmbeddable = new HashMap<>();
private final Map<Class<?>, EmbeddableDomainType<?>> embeddables = new HashMap<>();
private final Map<Class<?>, List<EmbeddableDomainType<?>>> embeddablesToProcess = new HashMap<>();
private final Map<EmbeddableDomainType<?>, Component> componentByEmbeddable = new HashMap<>();
private Map<MappedSuperclass, MappedSuperclassDomainType<?>> mappedSuperclassByMappedSuperclassMapping = new HashMap<>();
private Map<MappedSuperclassDomainType<?>, PersistentClass> mappedSuperClassTypeToPersistentClass = new HashMap<>();
private final Map<MappedSuperclass, MappedSuperclassDomainType<?>> mappedSuperclassByMappedSuperclassMapping = new HashMap<>();
private final Map<MappedSuperclassDomainType<?>, PersistentClass> mappedSuperClassTypeToPersistentClass = new HashMap<>();
//this list contains MappedSuperclass and EntityTypes ordered by superclass first
private List<Object> orderedMappings = new ArrayList<>();
private final List<Object> orderedMappings = new ArrayList<>();
/**
* Stack of PersistentClass being processed. Last in the list is the highest in the stack.
*/
private List<PersistentClass> stackOfPersistentClassesBeingProcessed = new ArrayList<>();
private MappingMetamodel metamodel;
private final List<PersistentClass> stackOfPersistentClassesBeingProcessed = new ArrayList<>();
private final MappingMetamodel metamodel;
public MetadataContext(
JpaMetamodel jpaMetamodel,
@ -144,7 +144,7 @@ public class MetadataContext {
}
public Set<EmbeddableDomainType<?>> getEmbeddableTypeSet() {
return new HashSet<>( embeddables.values() );
return componentByEmbeddable.keySet();
}
public Map<Class<?>, MappedSuperclassDomainType<?>> getMappedSuperclassTypeMap() {
@ -153,7 +153,7 @@ public class MetadataContext {
mappedSuperclassByMappedSuperclassMapping.size()
);
for ( MappedSuperclassDomainType mappedSuperclassType : mappedSuperclassByMappedSuperclassMapping.values() ) {
for ( MappedSuperclassDomainType<?> mappedSuperclassType : mappedSuperclassByMappedSuperclassMapping.values() ) {
mappedSuperClassTypeMap.put(
mappedSuperclassType.getJavaType(),
mappedSuperclassType
@ -164,7 +164,7 @@ public class MetadataContext {
}
public void registerEntityType(PersistentClass persistentClass, EntityTypeImpl<?> entityType) {
if ( entityType.getBindableJavaType() != null ) {
if ( entityType.getBindableJavaType() != null && entityType.getBindableJavaType() != Map.class ) {
entityTypes.put( entityType.getBindableJavaType(), entityType );
}
@ -240,7 +240,7 @@ public class MetadataContext {
*/
@SuppressWarnings({"unchecked", "WeakerAccess"})
public <E> EntityDomainType<E> locateEntityType(String entityName) {
return (EntityDomainType) entityTypesByEntityName.get( entityName );
return (EntityDomainType<E>) entityTypesByEntityName.get( entityName );
}
@SuppressWarnings("WeakerAccess")
@ -264,7 +264,7 @@ public class MetadataContext {
LOG.trace( "Starting entity [" + safeMapping.getEntityName() + ']' );
}
try {
final EntityDomainType<?> jpaMapping = entityTypesByPersistentClass.get( safeMapping );
final EntityDomainType<Object> jpaMapping = (EntityDomainType<Object>) entityTypesByPersistentClass.get( safeMapping );
applyIdMetadata( safeMapping, jpaMapping );
applyVersionAttribute( safeMapping, jpaMapping );
@ -282,16 +282,16 @@ public class MetadataContext {
// skip the version property, it was already handled previously.
continue;
}
final PersistentAttribute attribute = attributeFactory.buildAttribute(
final PersistentAttribute<Object, ?> attribute = attributeFactory.buildAttribute(
jpaMapping,
property
);
if ( attribute != null ) {
( (AttributeContainer) jpaMapping ).getInFlightAccess().addAttribute( attribute );
( (AttributeContainer<Object>) jpaMapping ).getInFlightAccess().addAttribute( attribute );
}
}
( (AttributeContainer) jpaMapping ).getInFlightAccess().finishUp();
( (AttributeContainer<?>) jpaMapping ).getInFlightAccess().finishUp();
if ( staticMetamodelScanEnabled ) {
populateStaticMetamodel( jpaMapping );
@ -309,7 +309,7 @@ public class MetadataContext {
LOG.trace( "Starting mapped superclass [" + safeMapping.getMappedClass().getName() + ']' );
}
try {
final MappedSuperclassDomainType<?> jpaType = mappedSuperclassByMappedSuperclassMapping.get( safeMapping );
final MappedSuperclassDomainType<Object> jpaType = (MappedSuperclassDomainType<Object>) mappedSuperclassByMappedSuperclassMapping.get( safeMapping );
applyIdMetadata( safeMapping, jpaType );
applyVersionAttribute( safeMapping, jpaType );
@ -321,13 +321,13 @@ public class MetadataContext {
// skip the version property, it was already handled previously.
continue;
}
final PersistentAttribute attribute = attributeFactory.buildAttribute( jpaType, property );
final PersistentAttribute<Object, ?> attribute = attributeFactory.buildAttribute( jpaType, property );
if ( attribute != null ) {
( (AttributeContainer) jpaType ).getInFlightAccess().addAttribute( attribute );
( (AttributeContainer<Object>) jpaType ).getInFlightAccess().addAttribute( attribute );
}
}
( (AttributeContainer) jpaType ).getInFlightAccess().finishUp();
( (AttributeContainer<?>) jpaType ).getInFlightAccess().finishUp();
if ( staticMetamodelScanEnabled ) {
populateStaticMetamodel( jpaType );
@ -358,13 +358,13 @@ public class MetadataContext {
final Iterator<Property> propertyItr = component.getPropertyIterator();
while ( propertyItr.hasNext() ) {
final Property property = propertyItr.next();
final PersistentAttribute attribute = attributeFactory.buildAttribute( embeddable, property );
final PersistentAttribute<Object, ?> attribute = attributeFactory.buildAttribute( (ManagedDomainType<Object>) embeddable, property );
if ( attribute != null ) {
( ( AttributeContainer) embeddable ).getInFlightAccess().addAttribute( attribute );
( ( AttributeContainer<Object>) embeddable ).getInFlightAccess().addAttribute( attribute );
}
}
( ( AttributeContainer) embeddable ).getInFlightAccess().finishUp();
( ( AttributeContainer<?>) embeddable ).getInFlightAccess().finishUp();
embeddables.put( embeddable.getJavaType(), embeddable );
if ( staticMetamodelScanEnabled ) {
@ -516,7 +516,7 @@ public class MetadataContext {
}
final String metamodelClassName = managedTypeClass.getName() + '_';
try {
final Class metamodelClass = Class.forName( metamodelClassName, true, managedTypeClass.getClassLoader() );
final Class<?> metamodelClass = Class.forName( metamodelClassName, true, managedTypeClass.getClassLoader() );
// we found the class; so populate it...
registerAttributes( metamodelClass, managedType );
}
@ -532,9 +532,9 @@ public class MetadataContext {
}
}
private final Set<Class> processedMetamodelClasses = new HashSet<>();
private final Set<Class<?>> processedMetamodelClasses = new HashSet<>();
private <X> void registerAttributes(Class metamodelClass, ManagedDomainType<X> managedType) {
private <X> void registerAttributes(Class<?> metamodelClass, ManagedDomainType<X> managedType) {
if ( !processedMetamodelClasses.add( metamodelClass ) ) {
return;
}
@ -564,7 +564,7 @@ public class MetadataContext {
}
}
private <X> void registerAttribute(Class metamodelClass, Attribute<X, ?> attribute) {
private <X> void registerAttribute(Class<?> metamodelClass, Attribute<X, ?> attribute) {
final String name = attribute.getName();
try {
// there is a shortcoming in the existing Hibernate code in terms of the way MappedSuperclass
@ -667,7 +667,7 @@ public class MetadataContext {
public <J> BasicDomainType<J> resolveBasicType(Class<J> javaType) {
//noinspection unchecked
return (BasicDomainType) basicDomainTypeMap.computeIfAbsent(
return (BasicDomainType<J>) basicDomainTypeMap.computeIfAbsent(
javaType,
jt -> {
final JavaTypeRegistry registry =

View File

@ -92,7 +92,7 @@ public abstract class AbstractManagedType<J>
@Override
@SuppressWarnings("unchecked")
public void visitAttributes(Consumer<PersistentAttribute<J, ?>> action) {
public void visitAttributes(Consumer<? super PersistentAttribute<J, ?>> action) {
visitDeclaredAttributes( action );
if ( getSuperType() != null ) {
getSuperType().visitAttributes( (Consumer) action );
@ -100,7 +100,7 @@ public abstract class AbstractManagedType<J>
}
@Override
public void visitDeclaredAttributes(Consumer<PersistentAttribute<J, ?>> action) {
public void visitDeclaredAttributes(Consumer<? super PersistentAttribute<J, ?>> action) {
declaredSingularAttributes.values().forEach( action );
if ( declaredPluralAttributes != null ) {
declaredPluralAttributes.values().forEach( action );

View File

@ -63,32 +63,18 @@ public interface JpaMetamodel extends jakarta.persistence.metamodel.Metamodel {
*/
<X> EntityDomainType<X> resolveHqlEntityReference(String entityName);
/**
* Visitation over all managed types via Consumer
*/
void visitManagedTypes(Consumer<ManagedDomainType<?>> action);
/**
* Same as {@link #managedType} except {@code null} is returned rather
* than throwing an exception
*/
<X> ManagedDomainType<X> findManagedType(Class<X> cls);
/**
* Visitation over all entity types via Consumer
*/
void visitEntityTypes(Consumer<EntityDomainType<?>> action);
/**
* Same as {@link #entity} except {@code null} is returned rather
* than throwing an exception
*/
<X> EntityDomainType<X> findEntityType(Class<X> cls);
void visitRootEntityTypes(Consumer<EntityDomainType<?>> action);
void visitEmbeddables(Consumer<EmbeddableDomainType<?>> action);
String qualifyImportableName(String queryName);
/**

View File

@ -41,8 +41,8 @@ public interface ManagedDomainType<J> extends AllowableParameterType<J>, Managed
void addSubType(ManagedDomainType subType);
void visitAttributes(Consumer<PersistentAttribute<J,?>> action);
void visitDeclaredAttributes(Consumer<PersistentAttribute<J,?>> action);
void visitAttributes(Consumer<? super PersistentAttribute<J, ?>> action);
void visitDeclaredAttributes(Consumer<? super PersistentAttribute<J, ?>> action);
@Override
PersistentAttribute<? super J,?> getAttribute(String name);

View File

@ -17,7 +17,6 @@ import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.function.Consumer;
import java.util.stream.Stream;
import jakarta.persistence.EntityGraph;
@ -42,11 +41,11 @@ import org.hibernate.graph.spi.SubGraphImplementor;
import org.hibernate.internal.EntityManagerMessageLogger;
import org.hibernate.internal.HEMLogging;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.jpa.spi.JpaCompliance;
import org.hibernate.mapping.MappedSuperclass;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.metamodel.MappingMetamodel;
import org.hibernate.metamodel.internal.JpaMetaModelPopulationSetting;
import org.hibernate.metamodel.internal.JpaStaticMetaModelPopulationSetting;
import org.hibernate.metamodel.internal.MetadataContext;
import org.hibernate.metamodel.model.domain.EmbeddableDomainType;
@ -55,6 +54,7 @@ import org.hibernate.metamodel.model.domain.IdentifiableDomainType;
import org.hibernate.metamodel.model.domain.JpaMetamodel;
import org.hibernate.metamodel.model.domain.ManagedDomainType;
import org.hibernate.metamodel.model.domain.MappedSuperclassDomainType;
import org.hibernate.metamodel.model.domain.PersistentAttribute;
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
import org.hibernate.persister.entity.Queryable;
import org.hibernate.query.sqm.tree.domain.SqmPolymorphicRootDescriptor;
@ -83,15 +83,16 @@ public class JpaMetamodelImpl implements JpaMetamodel, Serializable {
private final JpaCompliance jpaCompliance;
private final Map<String, EntityDomainType<?>> jpaEntityTypeMap = new TreeMap<>(); // Need ordering for deterministic implementers list in SqmPolymorphicRootDescriptor
private final Map<Class<?>, MappedSuperclassDomainType<?>> jpaMappedSuperclassTypeMap = new HashMap<>();
private final Map<Class, EmbeddableDomainType<?>> jpaEmbeddableDescriptorMap = new HashMap<>();
private final Map<Class<?>, ManagedDomainType<?>> jpaManagedTypeMap = new HashMap<>();
private final Set<ManagedDomainType<?>> jpaManagedTypes = new HashSet<>();
private final Set<EmbeddableDomainType<?>> jpaEmbeddables = new HashSet<>();
private final Map<String, Map<Class<?>, Enum<?>>> allowedEnumLiteralTexts = new HashMap<>();
private final transient Map<String, RootGraphImplementor> entityGraphMap = new ConcurrentHashMap<>();
private final transient Map<String, RootGraphImplementor<?>> entityGraphMap = new ConcurrentHashMap<>();
private final Map<Class, SqmPolymorphicRootDescriptor<?>> polymorphicEntityReferenceMap = new ConcurrentHashMap<>();
private final Map<Class<?>, SqmPolymorphicRootDescriptor<?>> polymorphicEntityReferenceMap = new ConcurrentHashMap<>();
private final Map<Class, String> entityProxyInterfaceMap = new HashMap<>();
private final Map<Class<?>, String> entityProxyInterfaceMap = new HashMap<>();
private final Map<String, ImportInfo<?>> nameToImportMap = new ConcurrentHashMap<>();
@ -114,7 +115,7 @@ public class JpaMetamodelImpl implements JpaMetamodel, Serializable {
@Override
public <X> EntityDomainType<X> entity(String entityName) {
//noinspection unchecked
return (EntityDomainType) jpaEntityTypeMap.get( entityName );
return (EntityDomainType<X>) jpaEntityTypeMap.get( entityName );
}
@Override
@ -153,71 +154,25 @@ public class JpaMetamodelImpl implements JpaMetamodel, Serializable {
return hqlEntityReference;
}
@Override
@SuppressWarnings("unchecked")
public void visitManagedTypes(Consumer<ManagedDomainType<?>> action) {
visitEntityTypes( (Consumer) action );
visitEmbeddables( (Consumer) action );
jpaMappedSuperclassTypeMap.values().forEach( action );
}
@Override
public <X> ManagedDomainType<X> findManagedType(Class<X> cls) {
ManagedType<?> type = jpaEntityTypeMap.get( cls.getName() );
if ( type == null ) {
type = jpaMappedSuperclassTypeMap.get( cls );
}
if ( type == null ) {
type = jpaEmbeddableDescriptorMap.get( cls );
}
if ( type == null ) {
return null;
}
//noinspection unchecked
return (ManagedDomainType<X>) type;
}
@Override
public void visitEntityTypes(Consumer<EntityDomainType<?>> action) {
jpaEntityTypeMap.values().forEach( action );
return (ManagedDomainType<X>) jpaManagedTypeMap.get( cls );
}
@Override
public <X> EntityDomainType<X> findEntityType(Class<X> cls) {
final EntityType<?> entityType = jpaEntityTypeMap.get( cls.getName() );
if ( entityType == null ) {
final ManagedType<?> type = jpaManagedTypeMap.get( cls );
if ( !( type instanceof EntityType<?> ) ) {
return null;
}
//noinspection unchecked
return (EntityDomainType<X>) entityType;
}
@Override
public void visitRootEntityTypes(Consumer<EntityDomainType<?>> action) {
jpaEntityTypeMap.values().forEach(
entityDomainType -> {
if ( entityDomainType.getSuperType() == null ) {
action.accept( entityDomainType );
}
}
);
}
@Override
public void visitEmbeddables(Consumer<EmbeddableDomainType<?>> action) {
jpaEmbeddableDescriptorMap.values().forEach( action );
return (EntityDomainType<X>) type;
}
@Override
public <X> ManagedDomainType<X> managedType(Class<X> cls) {
ManagedType<?> type = jpaEntityTypeMap.get( cls.getName() );
if ( type == null ) {
type = jpaMappedSuperclassTypeMap.get( cls );
}
if ( type == null ) {
type = jpaEmbeddableDescriptorMap.get( cls );
}
final ManagedType<?> type = jpaManagedTypeMap.get( cls );
if ( type == null ) {
// per JPA
throw new IllegalArgumentException( "Not a managed type: " + cls );
@ -229,44 +184,43 @@ public class JpaMetamodelImpl implements JpaMetamodel, Serializable {
@Override
public <X> EntityDomainType<X> entity(Class<X> cls) {
final EntityType<?> entityType = jpaEntityTypeMap.get( cls.getName() );
if ( entityType == null ) {
final ManagedType<?> type = jpaManagedTypeMap.get( cls );
if ( !( type instanceof EntityDomainType<?> ) ) {
throw new IllegalArgumentException( "Not an entity: " + cls.getName() );
}
//noinspection unchecked
return (EntityDomainType<X>) entityType;
return (EntityDomainType<X>) type;
}
@Override
public <X> EmbeddableDomainType<X> embeddable(Class<X> cls) {
final EmbeddableDomainType<?> embeddableType = jpaEmbeddableDescriptorMap.get( cls );
if ( embeddableType == null ) {
throw new IllegalArgumentException( "Not an embeddable: " + cls );
final ManagedType<?> type = jpaManagedTypeMap.get( cls );
if ( !( type instanceof EntityDomainType<?> ) ) {
throw new IllegalArgumentException( "Not an embeddable: " + cls.getName() );
}
//noinspection unchecked
return (EmbeddableDomainType<X>) embeddableType;
return (EmbeddableDomainType<X>) type;
}
@Override
public Set<ManagedType<?>> getManagedTypes() {
final int setSize = CollectionHelper.determineProperSizing(
jpaEntityTypeMap.size() + jpaMappedSuperclassTypeMap.size() + jpaEmbeddableDescriptorMap.size()
);
final Set<ManagedType<?>> managedTypes = CollectionHelper.setOfSize( setSize );
managedTypes.addAll( jpaEntityTypeMap.values() );
managedTypes.addAll( jpaMappedSuperclassTypeMap.values() );
managedTypes.addAll( jpaEmbeddableDescriptorMap.values() );
return managedTypes;
return new HashSet<>( jpaManagedTypes );
}
@Override
public Set<EntityType<?>> getEntities() {
return new HashSet<>( jpaEntityTypeMap.values() );
final Set<EntityType<?>> entityTypes = new HashSet<>( jpaEntityTypeMap.size() );
for ( ManagedDomainType<?> value : jpaManagedTypes ) {
if ( value instanceof EntityType<?> ) {
entityTypes.add( (EntityType<?>) value );
}
}
return entityTypes;
}
@Override
public Set<EmbeddableType<?>> getEmbeddables() {
return new HashSet<>( jpaEmbeddableDescriptorMap.values() );
return new HashSet<>( jpaEmbeddables );
}
@Override
@ -276,7 +230,7 @@ public class JpaMetamodelImpl implements JpaMetamodel, Serializable {
@Override
public <T> void addNamedEntityGraph(String graphName, RootGraphImplementor<T> entityGraph) {
final EntityGraph old = entityGraphMap.put(
final EntityGraph<?> old = entityGraphMap.put(
graphName,
entityGraph.makeImmutableCopy( graphName )
);
@ -289,7 +243,7 @@ public class JpaMetamodelImpl implements JpaMetamodel, Serializable {
@Override
public <T> RootGraphImplementor<T> findEntityGraphByName(String name) {
//noinspection unchecked
return entityGraphMap.get( name );
return (RootGraphImplementor<T>) entityGraphMap.get( name );
}
@Override
@ -301,15 +255,14 @@ public class JpaMetamodelImpl implements JpaMetamodel, Serializable {
final List<RootGraphImplementor<? super T>> results = new ArrayList<>();
for ( EntityGraph entityGraph : entityGraphMap.values() ) {
for ( EntityGraph<?> entityGraph : entityGraphMap.values() ) {
if ( !( entityGraph instanceof RootGraphImplementor ) ) {
continue;
}
final RootGraphImplementor egi = (RootGraphImplementor) entityGraph;
//noinspection unchecked
final RootGraphImplementor<T> egi = (RootGraphImplementor<T>) entityGraph;
if ( egi.appliesTo( entityType ) ) {
//noinspection unchecked
results.add( egi );
}
}
@ -326,6 +279,7 @@ public class JpaMetamodelImpl implements JpaMetamodel, Serializable {
private <T> ImportInfo<T> resolveImport(final String name) {
final ImportInfo<?> importInfo = nameToImportMap.get( name );
if ( importInfo != null ) {
//noinspection unchecked
return importInfo == INVALID_IMPORT ? null : (ImportInfo<T>) importInfo;
}
else {
@ -360,7 +314,7 @@ public class JpaMetamodelImpl implements JpaMetamodel, Serializable {
definition.getEntityName(),
definition.getJpaEntityName()
);
final EntityDomainType entityType = entity( definition.getEntityName() );
final EntityDomainType<Object> entityType = entity( definition.getEntityName() );
if ( entityType == null ) {
throw new IllegalArgumentException(
"Attempted to register named entity graph [" + definition.getRegisteredName()
@ -369,7 +323,7 @@ public class JpaMetamodelImpl implements JpaMetamodel, Serializable {
);
}
final RootGraphImpl entityGraph = new RootGraphImpl(
final RootGraphImpl<Object> entityGraph = new RootGraphImpl<>(
definition.getRegisteredName(),
entityType,
this
@ -378,8 +332,8 @@ public class JpaMetamodelImpl implements JpaMetamodel, Serializable {
final NamedEntityGraph namedEntityGraph = definition.getAnnotation();
if ( namedEntityGraph.includeAllAttributes() ) {
for ( Object attributeObject : entityType.getAttributes() ) {
entityGraph.addAttributeNodes( (Attribute) attributeObject );
for ( Attribute<? super Object, ?> attribute : entityType.getAttributes() ) {
entityGraph.addAttributeNodes( attribute );
}
}
@ -394,12 +348,12 @@ public class JpaMetamodelImpl implements JpaMetamodel, Serializable {
private void applyNamedAttributeNodes(
NamedAttributeNode[] namedAttributeNodes,
NamedEntityGraph namedEntityGraph,
GraphImplementor graphNode) {
GraphImplementor<?> graphNode) {
for ( NamedAttributeNode namedAttributeNode : namedAttributeNodes ) {
final String value = namedAttributeNode.value();
AttributeNodeImplementor attributeNode = graphNode.addAttributeNode( value );
AttributeNodeImplementor<?> attributeNode = graphNode.addAttributeNode( value );
if ( StringHelper.isNotEmpty( namedAttributeNode.subgraph() ) ) {
final SubGraphImplementor subgraph = attributeNode.makeSubGraph();
final SubGraphImplementor<?> subgraph = attributeNode.makeSubGraph();
applyNamedSubgraphs(
namedEntityGraph,
namedAttributeNode.subgraph(),
@ -407,7 +361,7 @@ public class JpaMetamodelImpl implements JpaMetamodel, Serializable {
);
}
if ( StringHelper.isNotEmpty( namedAttributeNode.keySubgraph() ) ) {
final SubGraphImplementor subgraph = attributeNode.makeKeySubGraph();
final SubGraphImplementor<?> subgraph = attributeNode.makeKeySubGraph();
applyNamedSubgraphs(
namedEntityGraph,
@ -421,7 +375,7 @@ public class JpaMetamodelImpl implements JpaMetamodel, Serializable {
private void applyNamedSubgraphs(
NamedEntityGraph namedEntityGraph,
String subgraphName,
SubGraphImplementor subgraph) {
SubGraphImplementor<?> subgraph) {
for ( NamedSubgraph namedSubgraph : namedEntityGraph.subgraphs() ) {
if ( subgraphName.equals( namedSubgraph.name() ) ) {
applyNamedAttributeNodes(
@ -468,30 +422,28 @@ public class JpaMetamodelImpl implements JpaMetamodel, Serializable {
}
final Set<EntityDomainType<?>> matchingDescriptors = new HashSet<>();
visitEntityTypes(
entityDomainType -> {
if ( javaType.isAssignableFrom( entityDomainType.getJavaType() ) ) {
final ManagedDomainType<?> superType = entityDomainType.getSuperType();
// If the entity super type is also assignable, skip adding this entity type
if ( superType instanceof EntityDomainType<?>
&& javaType.isAssignableFrom( superType.getJavaType() ) ) {
final Queryable entityPersister = (Queryable) typeConfiguration.getSessionFactory()
.getMetamodel()
.getEntityDescriptor( ( (EntityDomainType<?>) superType ).getHibernateEntityName() );
// But only skip adding this type if the parent doesn't require explicit polymorphism
if ( !entityPersister.isExplicitPolymorphism() ) {
return;
}
}
final Queryable entityPersister = (Queryable) typeConfiguration.getSessionFactory()
.getMetamodel()
.getEntityDescriptor( entityDomainType.getHibernateEntityName() );
if ( !entityPersister.isExplicitPolymorphism() ) {
matchingDescriptors.add( entityDomainType );
}
for ( EntityDomainType<?> entityDomainType : jpaEntityTypeMap.values() ) {
if ( javaType.isAssignableFrom( entityDomainType.getJavaType() ) ) {
final ManagedDomainType<?> superType = entityDomainType.getSuperType();
// If the entity super type is also assignable, skip adding this entity type
if ( superType instanceof EntityDomainType<?>
&& javaType.isAssignableFrom( superType.getJavaType() ) ) {
final Queryable entityPersister = (Queryable) typeConfiguration.getSessionFactory()
.getMetamodel()
.getEntityDescriptor( ( (EntityDomainType<?>) superType ).getHibernateEntityName() );
// But only skip adding this type if the parent doesn't require explicit polymorphism
if ( !entityPersister.isExplicitPolymorphism() ) {
continue;
}
}
);
final Queryable entityPersister = (Queryable) typeConfiguration.getSessionFactory()
.getMetamodel()
.getEntityDescriptor( entityDomainType.getHibernateEntityName() );
if ( !entityPersister.isExplicitPolymorphism() ) {
matchingDescriptors.add( entityDomainType );
}
}
}
if ( !matchingDescriptors.isEmpty() ) {
final SqmPolymorphicRootDescriptor<T> descriptor = new SqmPolymorphicRootDescriptor<>(
typeConfiguration.getJavaTypeDescriptorRegistry().resolveDescriptor( javaType ),
@ -508,8 +460,9 @@ public class JpaMetamodelImpl implements JpaMetamodel, Serializable {
public void processJpa(
MetadataImplementor bootMetamodel,
MappingMetamodel mappingMetamodel,
Map<Class, String> entityProxyInterfaceMap,
Map<Class<?>, String> entityProxyInterfaceMap,
JpaStaticMetaModelPopulationSetting jpaStaticMetaModelPopulationSetting,
JpaMetaModelPopulationSetting jpaMetaModelPopulationSetting,
Collection<NamedEntityGraphDefinition> namedEntityGraphDefinitions,
RuntimeModelCreationContext runtimeModelCreationContext) {
bootMetamodel.getImports().forEach( ( k, v ) -> this.nameToImportMap.put( k, new ImportInfo<>( v, null ) ) );
@ -532,19 +485,50 @@ public class JpaMetamodelImpl implements JpaMetamodel, Serializable {
context.wrapUp();
this.jpaEntityTypeMap.putAll( context.getEntityTypesByEntityName() );
this.jpaMappedSuperclassTypeMap.putAll( context.getMappedSuperclassTypeMap() );
for ( EmbeddableDomainType<?> embeddable : context.getEmbeddableTypeSet() ) {
this.jpaEmbeddableDescriptorMap.put( embeddable.getJavaType(), embeddable );
this.jpaManagedTypeMap.putAll( context.getEntityTypeMap() );
this.jpaManagedTypeMap.putAll( context.getMappedSuperclassTypeMap() );
this.jpaManagedTypes.addAll( context.getMappedSuperclassTypeMap().values() );
switch ( jpaMetaModelPopulationSetting ) {
case IGNORE_UNSUPPORTED:
this.jpaManagedTypes.addAll( context.getEntityTypeMap().values() );
break;
case ENABLED:
this.jpaManagedTypes.addAll( context.getEntityTypesByEntityName().values() );
break;
}
domainTypeStream( context ).forEach( (managedDomainType) -> managedDomainType.visitAttributes( persistentAttribute -> {
for ( EmbeddableDomainType<?> embeddable : context.getEmbeddableTypeSet() ) {
switch ( jpaMetaModelPopulationSetting ) {
case IGNORE_UNSUPPORTED:
if ( embeddable.getJavaType() != null && embeddable.getJavaType() != Map.class ) {
this.jpaEmbeddables.add( embeddable );
this.jpaManagedTypes.add( embeddable );
this.jpaManagedTypeMap.put( embeddable.getJavaType(), embeddable );
}
break;
case ENABLED:
this.jpaEmbeddables.add( embeddable );
this.jpaManagedTypes.add( embeddable );
if ( embeddable.getJavaType() != null ) {
this.jpaManagedTypeMap.put( embeddable.getJavaType(), embeddable );
}
break;
case DISABLED:
if ( embeddable.getJavaType() == null ) {
throw new UnsupportedOperationException( "ANY not supported" );
}
this.jpaManagedTypeMap.put( embeddable.getJavaType(), embeddable );
break;
}
}
final Consumer<PersistentAttribute<?, ?>> attributeConsumer = persistentAttribute -> {
if ( persistentAttribute.getJavaType() != null && persistentAttribute.getJavaType().isEnum() ) {
@SuppressWarnings("unchecked")
Class<Enum<?>> enumClass = (Class<Enum<?>>) persistentAttribute.getJavaType();
Enum<?>[] enumConstants = enumClass.getEnumConstants();
final Class<Enum<?>> enumClass = (Class<Enum<?>>) persistentAttribute.getJavaType();
final Enum<?>[] enumConstants = enumClass.getEnumConstants();
for ( Enum<?> enumConstant : enumConstants ) {
String qualifiedEnumLiteral = enumConstant.getDeclaringClass()
final String qualifiedEnumLiteral = enumConstant.getDeclaringClass()
.getSimpleName() + "." + enumConstant.name();
this.allowedEnumLiteralTexts.computeIfAbsent(
@ -557,7 +541,10 @@ public class JpaMetamodelImpl implements JpaMetamodel, Serializable {
).put( enumClass, enumConstant );
}
}
} ) );
};
domainTypeStream( context ).forEach(
managedDomainType -> managedDomainType.visitAttributes( attributeConsumer )
);
applyNamedEntityGraphs( namedEntityGraphDefinitions );
}

View File

@ -47,6 +47,7 @@ import org.hibernate.mapping.Component;
import org.hibernate.mapping.MappedSuperclass;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.metamodel.MappingMetamodel;
import org.hibernate.metamodel.internal.JpaMetaModelPopulationSetting;
import org.hibernate.metamodel.internal.JpaStaticMetaModelPopulationSetting;
import org.hibernate.metamodel.mapping.MappingModelExpressable;
import org.hibernate.metamodel.mapping.internal.MappingModelCreationProcess;
@ -75,7 +76,8 @@ import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.spi.TypeConfiguration;
import static org.hibernate.metamodel.internal.JpaStaticMetaModelPopulationSetting.determineJpaMetaModelPopulationSetting;
import static org.hibernate.metamodel.internal.JpaMetaModelPopulationSetting.determineJpaMetaModelPopulationSetting;
import static org.hibernate.metamodel.internal.JpaStaticMetaModelPopulationSetting.determineJpaStaticMetaModelPopulationSetting;
/**
* Hibernate implementation of the JPA {@link jakarta.persistence.metamodel.Metamodel} contract.
@ -103,7 +105,7 @@ public class MappingMetamodelImpl implements MappingMetamodel, MetamodelImplemen
private final JpaMetamodel jpaMetamodel;
private final Map<Class, String> entityProxyInterfaceMap = new ConcurrentHashMap<>();
private final Map<Class<?>, String> entityProxyInterfaceMap = new ConcurrentHashMap<>();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -195,7 +197,8 @@ public class MappingMetamodelImpl implements MappingMetamodel, MetamodelImplemen
final PersisterFactory persisterFactory = sessionFactory.getServiceRegistry().getService( PersisterFactory.class );
final JpaStaticMetaModelPopulationSetting jpaStaticMetaModelPopulationSetting = determineJpaMetaModelPopulationSetting( sessionFactory.getProperties() );
final JpaStaticMetaModelPopulationSetting jpaStaticMetaModelPopulationSetting = determineJpaStaticMetaModelPopulationSetting( sessionFactory.getProperties() );
final JpaMetaModelPopulationSetting jpaMetaModelPopulationSetting = determineJpaMetaModelPopulationSetting( sessionFactory.getProperties() );
bootModel.visitRegisteredComponents( Component::prepareForMappingModel );
bootModel.getMappedSuperclassMappingsCopy().forEach( MappedSuperclass::prepareForMappingModel );
@ -238,6 +241,7 @@ public class MappingMetamodelImpl implements MappingMetamodel, MetamodelImplemen
this,
entityProxyInterfaceMap,
jpaStaticMetaModelPopulationSetting,
jpaMetaModelPopulationSetting,
bootModel.getNamedEntityGraphs().values(),
runtimeModelCreationContext
);
@ -485,36 +489,16 @@ public class MappingMetamodelImpl implements MappingMetamodel, MetamodelImplemen
return jpaMetamodel.resolveHqlEntityReference( entityName );
}
@Override
public void visitManagedTypes(Consumer<ManagedDomainType<?>> action) {
jpaMetamodel.visitManagedTypes( action );
}
@Override
public <X> ManagedDomainType<X> findManagedType(Class<X> cls) {
return jpaMetamodel.findManagedType( cls );
}
@Override
public void visitEntityTypes(Consumer<EntityDomainType<?>> action) {
jpaMetamodel.visitEntityTypes( action );
}
@Override
public <X> EntityDomainType<X> findEntityType(Class<X> cls) {
return jpaMetamodel.findEntityType( cls );
}
@Override
public void visitRootEntityTypes(Consumer<EntityDomainType<?>> action) {
jpaMetamodel.visitRootEntityTypes( action );
}
@Override
public void visitEmbeddables(Consumer<EmbeddableDomainType<?>> action) {
jpaMetamodel.visitEmbeddables( action );
}
@Override
public String qualifyImportableName(String queryName) {
return jpaMetamodel.qualifyImportableName( queryName );

View File

@ -169,12 +169,12 @@ public class SqmPolymorphicRootDescriptor<T> implements EntityDomainType<T> {
}
@Override
public void visitAttributes(Consumer<PersistentAttribute<T, ?>> action) {
public void visitAttributes(Consumer<? super PersistentAttribute<T, ?>> action) {
commonAttributes.values().forEach( (Consumer) action );
}
@Override
public void visitDeclaredAttributes(Consumer<PersistentAttribute<T, ?>> action) {
public void visitDeclaredAttributes(Consumer<? super PersistentAttribute<T, ?>> action) {
}
@Override

View File

@ -28,7 +28,7 @@ public class IdClassHbmTest extends BaseCoreFunctionalTestCase {
@Override
protected void configure(Configuration configuration) {
// the Customer entity has a composite id that is not embeddable ( not supported by JPA ).
configuration.setProperty( AvailableSettings.STATIC_METAMODEL_POPULATION, "disabled" );
configuration.setProperty( AvailableSettings.JPA_METAMODEL_POPULATION, "disabled" );
}
@Test

View File

@ -24,7 +24,7 @@ import org.junit.jupiter.api.Test;
* @author Brad Koehn
*/
@Jpa(
integrationSettings = { @Setting(name = AvailableSettings.STATIC_METAMODEL_POPULATION, value = "enabled") },
integrationSettings = { @Setting(name = AvailableSettings.JPA_METAMODEL_POPULATION, value = "enabled") },
xmlMappings = { "org/hibernate/orm/test/jpa/criteria/paths/PolicyAndDistribution.hbm.xml" }
)
public class SingularAttributeJoinTest {

View File

@ -76,7 +76,7 @@ public abstract class AbstractJpaMetamodelPopulationTest extends BaseEntityManag
@Override
protected void addConfigOptions(Map options) {
super.addConfigOptions( options );
options.put( AvailableSettings.STATIC_METAMODEL_POPULATION, getJpaMetamodelPopulationValue() );
options.put( AvailableSettings.JPA_METAMODEL_POPULATION, getJpaMetamodelPopulationValue() );
}
@Test
@ -120,7 +120,7 @@ public abstract class AbstractJpaMetamodelPopulationTest extends BaseEntityManag
assertEquals( 8, metamodel.getManagedTypes().size() );
}
else {
// When skipUnsupported is used, any managed-type that refers to a dynamic-map entity type
// When ignoreUnsupported is used, any managed-type that refers to a dynamic-map entity type
// or a managed type that is owned by said dynamic-map entity type should be excluded.
// Therefore this collection should only include 3 elements
// EntityType(SimpleAnnotated)

View File

@ -4,9 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.jpa.test.metamodel;
import org.hibernate.orm.test.jpa.metamodel.AbstractJpaMetamodelPopulationTest;
package org.hibernate.orm.test.jpa.metamodel;
import org.hibernate.testing.TestForIssue;

View File

@ -4,9 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.jpa.test.metamodel;
import org.hibernate.orm.test.jpa.metamodel.AbstractJpaMetamodelPopulationTest;
package org.hibernate.orm.test.jpa.metamodel;
import org.hibernate.testing.TestForIssue;

View File

@ -4,9 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.jpa.test.metamodel;
import org.hibernate.orm.test.jpa.metamodel.AbstractJpaMetamodelPopulationTest;
package org.hibernate.orm.test.jpa.metamodel;
import org.hibernate.testing.TestForIssue;
@ -14,9 +12,9 @@ import org.hibernate.testing.TestForIssue;
* @author Chris Cranford
*/
@TestForIssue(jiraKey = "HHH-12871")
public class JpaMetamodelskipUnsupportedPopulationTest extends AbstractJpaMetamodelPopulationTest {
public class JpaMetamodelIgnoreUnsupportedPopulationTest extends AbstractJpaMetamodelPopulationTest {
@Override
protected String getJpaMetamodelPopulationValue() {
return "skipUnsupported";
return "ignoreUnsupported";
}
}

View File

@ -33,8 +33,8 @@ import static org.hamcrest.Matchers.nullValue;
)
@ServiceRegistry(
settings = @Setting(
name = AvailableSettings.STATIC_METAMODEL_POPULATION,
value = "skipUnsupported"
name = AvailableSettings.JPA_METAMODEL_POPULATION,
value = "ignoreUnsupported"
)
)
@DomainModel( annotatedClasses = BasicContributorTests.MainEntity.class )

View File

@ -10,13 +10,17 @@ import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.envers.Audited;
import org.hibernate.envers.DefaultRevisionEntity;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.metamodel.RuntimeMetamodels;
import org.hibernate.metamodel.model.domain.EntityDomainType;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.DomainModelScope;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
@ -24,6 +28,7 @@ import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
/**
@ -31,8 +36,8 @@ import static org.hamcrest.Matchers.notNullValue;
*/
@ServiceRegistry(
settings = @Setting(
name = AvailableSettings.STATIC_METAMODEL_POPULATION,
value = "skipUnsupported"
name = AvailableSettings.JPA_METAMODEL_POPULATION,
value = "ignoreUnsupported"
)
)
@DomainModel(