HHH-15689 fix SessionFactoryBuilder#addEntityNameResolver() and add Configuration#addEntityNameResolver()

This commit is contained in:
Gavin King 2022-11-08 12:20:05 +01:00
parent f71cf01620
commit 1cc7b72c4c
3 changed files with 24 additions and 3 deletions

View File

@ -26,6 +26,7 @@ package org.hibernate;
* </ul>
*
* @see org.hibernate.boot.SessionFactoryBuilder#addEntityNameResolver(EntityNameResolver...)
* @see org.hibernate.cfg.Configuration#addEntityNameResolver(EntityNameResolver)
*
* @author Steve Ebersole
*/

View File

@ -17,6 +17,7 @@ import java.util.Map;
import java.util.Properties;
import org.hibernate.EmptyInterceptor;
import org.hibernate.EntityNameResolver;
import org.hibernate.HibernateException;
import org.hibernate.Interceptor;
import org.hibernate.MappingException;
@ -81,6 +82,7 @@ import jakarta.persistence.SharedCacheMode;
* In addition, there are convenience methods for adding
* {@link #addAttributeConverter attribute converters},
* {@link #registerTypeContributor type contributors},
* {@link #addEntityNameResolver entity name resolvers},
* {@link #addSqlFunction SQL function descriptors}, and
* {@link #addAuxiliaryDatabaseObject auxiliary database objects}, for
* setting {@link #setImplicitNamingStrategy naming strategies} and a
@ -126,6 +128,7 @@ public class Configuration {
private Map<String, SqmFunctionDescriptor> customFunctionDescriptors;
private List<AuxiliaryDatabaseObject> auxiliaryDatabaseObjectList;
private HashMap<Class<?>, ConverterDescriptor> attributeConverterDescriptorsByClass;
private List<EntityNameResolver> entityNameResolvers = new ArrayList<>();
// used to build SF
private StandardServiceRegistryBuilder standardServiceRegistryBuilder;
@ -739,6 +742,10 @@ public class Configuration {
sessionFactoryBuilder.applyInterceptor( interceptor );
}
if ( entityNameResolvers != null ) {
sessionFactoryBuilder.addEntityNameResolver( entityNameResolvers.toArray(new EntityNameResolver[0]) );
}
if ( getSessionFactoryObserver() != null ) {
sessionFactoryBuilder.addSessionFactoryObservers( getSessionFactoryObserver() );
}
@ -849,6 +856,16 @@ public class Configuration {
attributeConverterDescriptorsByClass.put( converterDescriptor.getAttributeConverterClass(), converterDescriptor );
}
/**
* Add an {@link EntityNameResolver} to this configuration.
*/
public void addEntityNameResolver(EntityNameResolver entityNameResolver) {
if ( entityNameResolvers == null ) {
entityNameResolvers = new ArrayList<>();
}
entityNameResolvers.add( entityNameResolver );
}
/**
* Sets the {@link SharedCacheMode} to use.
* <p>

View File

@ -34,12 +34,15 @@ public class CoordinatingEntityNameResolver implements EntityNameResolver {
for ( EntityNameResolver resolver : mappingMetamodel.getEntityNameResolvers() ) {
entityName = resolver.resolveEntityName( entity );
if ( entityName != null ) {
break;
return entityName;
}
}
if ( entityName != null ) {
return entityName;
for ( EntityNameResolver resolver : sessionFactory.getSessionFactoryOptions().getEntityNameResolvers() ) {
entityName = resolver.resolveEntityName( entity );
if ( entityName != null ) {
return entityName;
}
}
// the old-time stand-by...