HHH-14529 Configuration and wiring to prefer JAXB over DOM4J for orm.xml handling

This commit is contained in:
Yoann Rodière 2021-03-26 11:58:35 +01:00
parent e8cd9f8917
commit 8ab3a2f7e9
27 changed files with 341 additions and 72 deletions

View File

@ -31,6 +31,7 @@ import org.hibernate.boot.spi.ClassLoaderAccess;
import org.hibernate.boot.spi.MetadataBuildingOptions;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.annotations.reflection.JPAMetadataProvider;
import org.hibernate.cfg.annotations.reflection.internal.JPAXMLOverriddenMetadataProvider;
import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.jpa.internal.MutableJpaComplianceImpl;
@ -83,10 +84,10 @@ public class BootstrapContextImpl implements BootstrapContext {
final ClassLoaderService classLoaderService = serviceRegistry.getService( ClassLoaderService.class );
this.classLoaderAccess = new ClassLoaderAccessImpl( classLoaderService );
this.hcannReflectionManager = generateHcannReflectionManager();
final StrategySelector strategySelector = serviceRegistry.getService( StrategySelector.class );
final ConfigurationService configService = serviceRegistry.getService( ConfigurationService.class );
this.hcannReflectionManager = generateHcannReflectionManager();
this.jpaCompliance = new MutableJpaComplianceImpl( configService.getSettings(), false );
this.scanOptions = new StandardScanOptions(
@ -309,7 +310,13 @@ public class BootstrapContextImpl implements BootstrapContext {
private JavaReflectionManager generateHcannReflectionManager() {
final JavaReflectionManager reflectionManager = new JavaReflectionManager();
reflectionManager.setMetadataProvider( new JPAMetadataProvider( this ) );
if ( metadataBuildingOptions.getXmlMappingOptions().isPreferJaxb() ) {
reflectionManager.setMetadataProvider( new JPAXMLOverriddenMetadataProvider( this ) );
}
else {
// Legacy implementation based on DOM4J, for backwards compatibility.
reflectionManager.setMetadataProvider( new JPAMetadataProvider( this ) );
}
return reflectionManager;
}

View File

@ -30,6 +30,7 @@ import org.hibernate.boot.archive.spi.ArchiveDescriptorFactory;
import org.hibernate.boot.cfgxml.spi.CfgXmlAccessService;
import org.hibernate.boot.cfgxml.spi.LoadedConfig;
import org.hibernate.boot.cfgxml.spi.MappingReference;
import org.hibernate.boot.jaxb.spi.XmlMappingOptions;
import org.hibernate.boot.model.IdGeneratorStrategyInterpreter;
import org.hibernate.boot.model.TypeContributions;
import org.hibernate.boot.model.TypeContributor;
@ -622,7 +623,7 @@ public class MetadataBuilderImpl implements MetadataBuilderImplementor, TypeCont
private IdGeneratorInterpreterImpl idGenerationTypeInterpreter = new IdGeneratorInterpreterImpl();
private String schemaCharset;
private boolean xmlMappingEnabled;
private final XmlMappingOptions xmlMappingOptions;
public MetadataBuildingOptionsImpl(StandardServiceRegistry serviceRegistry) {
this.serviceRegistry = serviceRegistry;
@ -634,11 +635,7 @@ public class MetadataBuilderImpl implements MetadataBuilderImplementor, TypeCont
this.multiTenancyStrategy = MultiTenancyStrategy.determineMultiTenancyStrategy( configService.getSettings() );
this.xmlMappingEnabled = configService.getSetting(
AvailableSettings.XML_MAPPING_ENABLED,
StandardConverters.BOOLEAN,
true
);
xmlMappingOptions = XmlMappingOptions.get( serviceRegistry );
this.implicitDiscriminatorsForJoinedInheritanceSupported = configService.getSetting(
AvailableSettings.IMPLICIT_DISCRIMINATOR_COLUMNS_FOR_JOINED_SUBCLASS,
@ -926,8 +923,8 @@ public class MetadataBuilderImpl implements MetadataBuilderImplementor, TypeCont
}
@Override
public boolean isXmlMappingEnabled() {
return xmlMappingEnabled;
public XmlMappingOptions getXmlMappingOptions() {
return xmlMappingOptions;
}
/**

View File

@ -0,0 +1,12 @@
/*
* 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.boot.jaxb.internal;
import org.hibernate.boot.jaxb.spi.XmlMappingOptions;
public class DefaultXmlMappingOptions implements XmlMappingOptions {
}

View File

@ -20,7 +20,9 @@ import org.hibernate.boot.jaxb.Origin;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping;
import org.hibernate.boot.jaxb.internal.stax.HbmEventReader;
import org.hibernate.boot.jaxb.internal.stax.JpaOrmXmlEventReader;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings;
import org.hibernate.boot.jaxb.spi.Binding;
import org.hibernate.boot.jaxb.spi.XmlMappingOptions;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.xsd.MappingXsdSupport;
import org.hibernate.internal.util.config.ConfigurationException;
@ -39,18 +41,18 @@ public class MappingBinder extends AbstractBinder {
private final XMLEventFactory xmlEventFactory = XMLEventFactory.newInstance();
private final XmlMappingOptions options;
private JAXBContext hbmJaxbContext;
private JAXBContext entityMappingsJaxbContext;
public MappingBinder(ClassLoaderService classLoaderService) {
this( classLoaderService, true );
}
public MappingBinder(ClassLoaderService classLoaderService, boolean validateXml) {
public MappingBinder(ClassLoaderService classLoaderService, boolean validateXml, XmlMappingOptions options) {
super( classLoaderService, validateXml );
this.options = options;
}
@Override
protected Binding doBind(
protected Binding<?> doBind(
XMLEventReader staxEventReader,
StartElement rootElementStartEvent,
Origin origin) {
@ -63,12 +65,20 @@ public class MappingBinder extends AbstractBinder {
return new Binding<>( hbmBindings, origin );
}
else {
// final XMLEventReader reader = new JpaOrmXmlEventReader( staxEventReader );
// return jaxb( reader, LocalSchema.MAPPING.getSchema(), JaxbEntityMappings.class, origin );
try {
final XMLEventReader reader = new JpaOrmXmlEventReader( staxEventReader, xmlEventFactory );
return new Binding<>( toDom4jDocument( reader, origin ), origin );
if ( options.isPreferJaxb() ) {
log.debugf( "Performing JAXB binding of orm.xml document : %s", origin.toString() );
XMLEventReader reader = new JpaOrmXmlEventReader( staxEventReader, xmlEventFactory );
JaxbEntityMappings bindingRoot = jaxb( reader, MappingXsdSupport.INSTANCE.latestJpaDescriptor().getSchema(), entityMappingsJaxbContext(), origin );
return new Binding<>( bindingRoot, origin );
}
else {
log.debugf( "Performing DOM4J binding of orm.xml document : %s", origin.toString() );
final XMLEventReader reader = new JpaOrmXmlEventReader( staxEventReader, xmlEventFactory );
return new Binding<>( toDom4jDocument( reader, origin ), origin );
}
}
catch (JpaOrmXmlEventReader.BadVersionException e) {
throw new UnsupportedOrmXsdVersionException( e.getRequestedVersion(), origin );
@ -88,6 +98,18 @@ public class MappingBinder extends AbstractBinder {
return hbmJaxbContext;
}
private JAXBContext entityMappingsJaxbContext() {
if ( entityMappingsJaxbContext == null ) {
try {
entityMappingsJaxbContext = JAXBContext.newInstance( JaxbEntityMappings.class );
}
catch ( JAXBException e ) {
throw new ConfigurationException( "Unable to build orm.xml JAXBContext", e );
}
}
return entityMappingsJaxbContext;
}
private Document toDom4jDocument(XMLEventReader jpaOrmXmlEventReader, Origin origin) {
// todo : do we need to build a DocumentFactory instance for use here?
// historically we did that to set TCCL since, iirc, dom4j uses TCCL

View File

@ -0,0 +1,86 @@
/*
* 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.boot.jaxb.spi;
import org.hibernate.boot.jaxb.internal.DefaultXmlMappingOptions;
import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.service.ServiceRegistry;
/**
* The options of XML mapping.
* <p>
* We're using an interface instead of simply configuration properties,
* so that we can override the options easily in integrations (Quarkus)
* and tests (to run the tests multiple times with different options).
*/
public interface XmlMappingOptions {
String DEFAULT_NAME = "default";
static XmlMappingOptions get(ServiceRegistry serviceRegistry) {
final ConfigurationService configService = serviceRegistry.getService( ConfigurationService.class );
// The config service may be null if we're using a BootstrapServiceRegistry,
// since configuration properties are unknown at that point.
// That can happen with MetadataSources in particular,
// because for some reason we allow MetadataSources to be built before the StandardServiceRegistry
// (and Quarkus relies on that).
// That's why we prefer to rely on strategies (see below):
// they can be customized without relying on configuration properties
// through the service loader.
boolean xmlMappingEnabled = configService == null || configService.getSetting(
AvailableSettings.XML_MAPPING_ENABLED,
StandardConverters.BOOLEAN,
true
);
XmlMappingOptions result;
if ( !xmlMappingEnabled ) {
result = new XmlMappingOptions() {
@Override
public boolean isEnabled() {
return false;
}
};
}
else {
StrategySelector strategySelector = serviceRegistry.getService( StrategySelector.class );
result = strategySelector.resolveDefaultableStrategy(
XmlMappingOptions.class,
XmlMappingOptions.DEFAULT_NAME,
new DefaultXmlMappingOptions()
);
}
return result;
}
/**
* Allows to skip processing of XML Mapping.
* This is for people using exclusively annotations to define their model, and might
* be able to improve efficiency of booting Hibernate ORM.
* By default, the XML mapping is taken into account.
*/
default boolean isEnabled() {
return true;
}
/**
* Whether to prefer JAXB implementations for XML parsing,
* or to rely on legacy behavior (JAXB for hbm.xml, DOM4J for orm.xml and Envers).
* <p>
* This option will be removed in a future major version (probably ORM 6.0)
* where JAXB will always be used.
*/
default boolean isPreferJaxb() {
return false;
}
}

View File

@ -15,6 +15,7 @@ import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.internal.InFlightMetadataCollectorImpl;
import org.hibernate.boot.internal.MetadataBuildingContextRootImpl;
import org.hibernate.boot.jaxb.internal.MappingBinder;
import org.hibernate.boot.jaxb.spi.XmlMappingOptions;
import org.hibernate.boot.model.TypeContributions;
import org.hibernate.boot.model.TypeContributor;
import org.hibernate.boot.model.process.internal.ManagedResourcesImpl;
@ -32,11 +33,8 @@ import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.boot.spi.MetadataBuildingOptions;
import org.hibernate.boot.spi.MetadataContributor;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.MetadataSourceType;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.type.BasicType;
import org.hibernate.type.BasicTypeRegistry;
@ -98,16 +96,11 @@ public class MetadataBuildingProcess {
final MetadataSources sources,
final BootstrapContext bootstrapContext) {
final ManagedResourcesImpl managedResources = ManagedResourcesImpl.baseline( sources, bootstrapContext );
final ConfigurationService configService = bootstrapContext.getServiceRegistry().getService( ConfigurationService.class );
final boolean xmlMappingEnabled = configService.getSetting(
AvailableSettings.XML_MAPPING_ENABLED,
StandardConverters.BOOLEAN,
true
);
XmlMappingOptions xmlMappingOptions = XmlMappingOptions.get( bootstrapContext.getServiceRegistry() );
ScanningCoordinator.INSTANCE.coordinateScan(
managedResources,
bootstrapContext,
xmlMappingEnabled ? sources.getXmlMappingBinderAccess() : null
xmlMappingOptions.isEnabled() ? sources.getXmlMappingBinderAccess() : null
);
return managedResources;
}
@ -157,7 +150,7 @@ public class MetadataBuildingProcess {
final MetadataSourceProcessor processor = new MetadataSourceProcessor() {
private final MetadataSourceProcessor hbmProcessor =
options.isXmlMappingEnabled()
options.getXmlMappingOptions().isEnabled()
? new HbmMetadataSourceProcessorImpl( managedResources, rootMetadataBuildingContext )
: new NoOpMetadataSourceProcessorImpl();
@ -294,13 +287,14 @@ public class MetadataBuildingProcess {
metadataCollector.processSecondPasses( rootMetadataBuildingContext );
if ( options.isXmlMappingEnabled() ) {
XmlMappingOptions xmlMappingOptions = options.getXmlMappingOptions();
if ( xmlMappingOptions.isEnabled() ) {
Iterable<AdditionalJaxbMappingProducer> producers = classLoaderService.loadJavaServices( AdditionalJaxbMappingProducer.class );
if ( producers != null ) {
final EntityHierarchyBuilder hierarchyBuilder = new EntityHierarchyBuilder();
// final MappingBinder mappingBinder = new MappingBinder( true );
// We need to disable validation here. It seems Envers is not producing valid (according to schema) XML
final MappingBinder mappingBinder = new MappingBinder( classLoaderService, false );
final MappingBinder mappingBinder = new MappingBinder( classLoaderService, false, xmlMappingOptions );
for ( AdditionalJaxbMappingProducer producer : producers ) {
log.tracef( "Calling AdditionalJaxbMappingProducer : %s", producer );
Collection<MappingDocument> additionalMappings = producer.produceAdditionalMappings(

View File

@ -22,17 +22,21 @@ import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.boot.AttributeConverterInfo;
import org.hibernate.boot.internal.MetadataBuildingContextRootImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings;
import org.hibernate.boot.jaxb.spi.Binding;
import org.hibernate.boot.jaxb.spi.XmlMappingOptions;
import org.hibernate.boot.model.convert.spi.ConverterDescriptor;
import org.hibernate.boot.model.process.spi.ManagedResources;
import org.hibernate.boot.model.source.spi.MetadataSourceProcessor;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.spi.JpaOrmXmlPersistenceUnitDefaultAware;
import org.hibernate.boot.spi.JpaOrmXmlPersistenceUnitDefaultAware.JpaOrmXmlPersistenceUnitDefaults;
import org.hibernate.boot.spi.MetadataBuildingOptions;
import org.hibernate.cfg.AnnotationBinder;
import org.hibernate.cfg.InheritanceState;
import org.hibernate.cfg.annotations.reflection.AttributeConverterDefinitionCollector;
import org.hibernate.cfg.annotations.reflection.JPAMetadataProvider;
import org.hibernate.cfg.annotations.reflection.internal.JPAXMLOverriddenMetadataProvider;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper;
@ -75,11 +79,29 @@ public class AnnotationMetadataSourceProcessorImpl implements MetadataSourceProc
final AttributeConverterManager attributeConverterManager = new AttributeConverterManager( rootMetadataBuildingContext );
this.classLoaderService = rootMetadataBuildingContext.getBuildingOptions().getServiceRegistry().getService( ClassLoaderService.class );
if ( rootMetadataBuildingContext.getBuildingOptions().isXmlMappingEnabled() ) {
MetadataBuildingOptions metadataBuildingOptions = rootMetadataBuildingContext.getBuildingOptions();
XmlMappingOptions xmlMappingOptions = metadataBuildingOptions.getXmlMappingOptions();
if ( xmlMappingOptions.isEnabled() ) {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Ewww. This is temporary until we migrate to Jandex + StAX for annotation binding
if ( xmlMappingOptions.isPreferJaxb() ) {
final JPAXMLOverriddenMetadataProvider jpaMetadataProvider = (JPAXMLOverriddenMetadataProvider) ( (MetadataProviderInjector) reflectionManager )
.getMetadataProvider();
for ( Binding<?> xmlBinding : managedResources.getXmlMappingBindings() ) {
Object root = xmlBinding.getRoot();
if ( !(root instanceof JaxbEntityMappings) ) {
continue;
}
JaxbEntityMappings entityMappings = (JaxbEntityMappings) xmlBinding.getRoot();
final List<String> classNames = jpaMetadataProvider.getXMLContext().addDocument( entityMappings );
for ( String className : classNames ) {
xClasses.add( toXClass( className, reflectionManager, classLoaderService ) );
}
}
jpaMetadataProvider.getXMLContext().applyDiscoveredAttributeConverters( attributeConverterManager );
}
else {
final JPAMetadataProvider jpaMetadataProvider = (JPAMetadataProvider) ( (MetadataProviderInjector) reflectionManager )
.getMetadataProvider();
for ( Binding xmlBinding : managedResources.getXmlMappingBindings() ) {
@ -94,6 +116,7 @@ public class AnnotationMetadataSourceProcessorImpl implements MetadataSourceProc
}
}
jpaMetadataProvider.getXMLContext().applyDiscoveredAttributeConverters( attributeConverterManager );
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}

View File

@ -9,6 +9,8 @@ package org.hibernate.boot.registry.selector.internal;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.boot.jaxb.internal.DefaultXmlMappingOptions;
import org.hibernate.boot.jaxb.spi.XmlMappingOptions;
import org.hibernate.boot.model.naming.ImplicitNamingStrategy;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;
@ -100,6 +102,8 @@ public class StrategySelectorBuilder {
addMultiTableBulkIdStrategies( strategySelector );
addImplicitNamingStrategies( strategySelector );
addCacheKeysFactories( strategySelector );
strategySelector.registerStrategyImplementor( XmlMappingOptions.class, XmlMappingOptions.DEFAULT_NAME,
DefaultXmlMappingOptions.class );
// apply auto-discovered registrations
for ( StrategyRegistrationProvider provider : classLoaderService.loadJavaServices( StrategyRegistrationProvider.class ) ) {

View File

@ -18,6 +18,7 @@ import org.hibernate.boot.CacheRegionDefinition;
import org.hibernate.boot.archive.scan.spi.ScanEnvironment;
import org.hibernate.boot.archive.scan.spi.ScanOptions;
import org.hibernate.boot.archive.spi.ArchiveDescriptorFactory;
import org.hibernate.boot.jaxb.spi.XmlMappingOptions;
import org.hibernate.boot.model.IdGeneratorStrategyInterpreter;
import org.hibernate.boot.model.naming.ImplicitNamingStrategy;
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
@ -203,8 +204,8 @@ public abstract class AbstractDelegatingMetadataBuildingOptions implements Metad
}
@Override
public boolean isXmlMappingEnabled() {
return delegate.isXmlMappingEnabled();
public XmlMappingOptions getXmlMappingOptions() {
return delegate.getXmlMappingOptions();
}
}

View File

@ -17,6 +17,7 @@ import org.hibernate.boot.CacheRegionDefinition;
import org.hibernate.boot.archive.scan.spi.ScanEnvironment;
import org.hibernate.boot.archive.scan.spi.ScanOptions;
import org.hibernate.boot.archive.spi.ArchiveDescriptorFactory;
import org.hibernate.boot.jaxb.spi.XmlMappingOptions;
import org.hibernate.boot.model.IdGeneratorStrategyInterpreter;
import org.hibernate.boot.model.naming.ImplicitNamingStrategy;
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
@ -251,8 +252,8 @@ public interface MetadataBuildingOptions {
return null;
}
default boolean isXmlMappingEnabled() {
return true;
default XmlMappingOptions getXmlMappingOptions() {
return XmlMappingOptions.get( getServiceRegistry() );
}
/**

View File

@ -20,7 +20,11 @@ import org.hibernate.boot.jaxb.internal.InputStreamXmlSource;
import org.hibernate.boot.jaxb.internal.MappingBinder;
import org.hibernate.boot.jaxb.internal.UrlXmlSource;
import org.hibernate.boot.jaxb.spi.Binding;
import org.hibernate.boot.jaxb.spi.XmlMappingOptions;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.service.ServiceRegistry;
import org.jboss.logging.Logger;
@ -37,10 +41,12 @@ public class XmlMappingBinderAccess {
public XmlMappingBinderAccess(ServiceRegistry serviceRegistry) {
this.classLoaderService = serviceRegistry.getService( ClassLoaderService.class );
XmlMappingOptions xmlMappingOptions = XmlMappingOptions.get( serviceRegistry );
// NOTE : The boolean here indicates whether or not to perform validation as we load XML documents.
// Should we expose this setting? Disabling would speed up JAXP and JAXB at runtime, but potentially
// at the cost of less obvious errors when a document is not valid.
this.mappingBinder = new MappingBinder( serviceRegistry.getService( ClassLoaderService.class ), true );
this.mappingBinder = new MappingBinder( classLoaderService, true, xmlMappingOptions );
}
public MappingBinder getMappingBinder() {

View File

@ -698,7 +698,6 @@ public interface AvailableSettings extends org.hibernate.jpa.AvailableSettings {
*/
String XML_MAPPING_ENABLED = "hibernate.xml_mapping_enabled";
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// SessionFactoryBuilder level settings
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -30,6 +30,7 @@ import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.boot.spi.ClassLoaderAccess;
import org.hibernate.boot.spi.ClassLoaderAccessDelegateImpl;
import org.hibernate.boot.spi.MetadataBuildingOptions;
import org.hibernate.cfg.annotations.reflection.internal.JPAXMLOverriddenMetadataProvider;
import org.dom4j.Element;
@ -37,7 +38,13 @@ import org.dom4j.Element;
* MetadataProvider aware of the JPA Deployment descriptor
*
* @author Emmanuel Bernard
*
* @deprecated This class is not API: do not use it from application code.
* This class will be removed in Hibernate ORM 6.0.
* For implementation code, use {@link JPAXMLOverriddenMetadataProvider}
* instead.
*/
@Deprecated
@SuppressWarnings("unchecked")
public final class JPAMetadataProvider implements MetadataProvider {
@ -74,12 +81,12 @@ public final class JPAMetadataProvider implements MetadataProvider {
return delegate;
}
},
metadataBuildingOptions.isXmlMappingEnabled() );
metadataBuildingOptions.getXmlMappingOptions().isEnabled() );
}
public JPAMetadataProvider(BootstrapContext bootstrapContext) {
this( bootstrapContext.getClassLoaderAccess(),
bootstrapContext.getMetadataBuildingOptions().isXmlMappingEnabled() );
bootstrapContext.getMetadataBuildingOptions().getXmlMappingOptions().isEnabled() );
}
JPAMetadataProvider(ClassLoaderAccess classLoaderAccess, boolean xmlMetadataEnabled) {
@ -147,7 +154,8 @@ public final class JPAMetadataProvider implements MetadataProvider {
defaults.put( SequenceGenerator.class, sequenceGenerators );
}
for ( Element subelement : elements ) {
sequenceGenerators.add( JPAOverriddenAnnotationReader.buildSequenceGeneratorAnnotation( subelement ) );
sequenceGenerators.add( JPAOverriddenAnnotationReader
.buildSequenceGeneratorAnnotation( subelement ) );
}
elements = element.elements( "table-generator" );
@ -169,7 +177,8 @@ public final class JPAMetadataProvider implements MetadataProvider {
namedQueries = new ArrayList<>();
defaults.put( NamedQuery.class, namedQueries );
}
List<NamedQuery> currentNamedQueries = JPAOverriddenAnnotationReader.buildNamedQueries(
List<NamedQuery> currentNamedQueries = JPAOverriddenAnnotationReader
.buildNamedQueries(
element,
false,
xmlDefaults,
@ -182,7 +191,8 @@ public final class JPAMetadataProvider implements MetadataProvider {
namedNativeQueries = new ArrayList<>();
defaults.put( NamedNativeQuery.class, namedNativeQueries );
}
List<NamedNativeQuery> currentNamedNativeQueries = JPAOverriddenAnnotationReader.buildNamedQueries(
List<NamedNativeQuery> currentNamedNativeQueries = JPAOverriddenAnnotationReader
.buildNamedQueries(
element,
true,
xmlDefaults,
@ -197,7 +207,8 @@ public final class JPAMetadataProvider implements MetadataProvider {
sqlResultSetMappings = new ArrayList<>();
defaults.put( SqlResultSetMapping.class, sqlResultSetMappings );
}
List<SqlResultSetMapping> currentSqlResultSetMappings = JPAOverriddenAnnotationReader.buildSqlResultsetMappings(
List<SqlResultSetMapping> currentSqlResultSetMappings = JPAOverriddenAnnotationReader
.buildSqlResultsetMappings(
element,
xmlDefaults,
classLoaderAccess
@ -209,7 +220,8 @@ public final class JPAMetadataProvider implements MetadataProvider {
namedStoredProcedureQueries = new ArrayList<>( );
defaults.put( NamedStoredProcedureQuery.class, namedStoredProcedureQueries );
}
List<NamedStoredProcedureQuery> currentNamedStoredProcedureQueries = JPAOverriddenAnnotationReader.buildNamedStoreProcedureQueries(
List<NamedStoredProcedureQuery> currentNamedStoredProcedureQueries = JPAOverriddenAnnotationReader
.buildNamedStoreProcedureQueries(
element,
xmlDefaults,
classLoaderAccess

View File

@ -126,6 +126,7 @@ import org.hibernate.annotations.common.reflection.ReflectionUtil;
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.boot.spi.ClassLoaderAccess;
import org.hibernate.cfg.annotations.reflection.internal.JPAXMLOverriddenAnnotationReader;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper;
@ -140,7 +141,13 @@ import org.dom4j.Element;
* @author Davide Marchignoli
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*
* @deprecated This class is not API: do not use it from application code.
* This class will be removed in Hibernate ORM 6.0.
* For implementation code, use {@link JPAXMLOverriddenAnnotationReader}
* instead.
*/
@Deprecated
@SuppressWarnings("unchecked")
public class JPAOverriddenAnnotationReader implements AnnotationReader {
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( JPAOverriddenAnnotationReader.class );
@ -884,7 +891,7 @@ public class JPAOverriddenAnnotationReader implements AnnotationReader {
* field or property. Thus, any methods which might in some contexts merge
* with annotations must not do so in this context.
*
* @see #getElementCollection(List, org.hibernate.cfg.annotations.reflection.XMLContext.Default)
* @see #getElementCollection(List, XMLContext.Default)
*/
private void getAssociation(
Class<? extends Annotation> annotationType, List<Annotation> annotationList, XMLContext.Default defaults
@ -2691,7 +2698,8 @@ public class JPAOverriddenAnnotationReader implements AnnotationReader {
AnnotationDescriptor ad = new AnnotationDescriptor( IdClass.class );
Class clazz;
try {
clazz = classLoaderAccess.classForName( XMLContext.buildSafeClassName( attr.getValue(), defaults )
clazz = classLoaderAccess.classForName( XMLContext
.buildSafeClassName( attr.getValue(), defaults )
);
}
catch ( ClassLoadingException e ) {

View File

@ -32,7 +32,12 @@ import org.dom4j.Element;
*
* @author Emmanuel Bernard
* @author Brett Meyer
*
* @deprecated This class is not API: do not use it from application code.
* This class will be removed in Hibernate ORM 6.0.
* For implementation code, use {@link org.hibernate.cfg.annotations.reflection.internal.XMLContext} instead.
*/
@Deprecated
public class XMLContext implements Serializable {
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( XMLContext.class );
@ -222,7 +227,7 @@ public class XMLContext implements Serializable {
return className;
}
public static String buildSafeClassName(String className, XMLContext.Default defaults) {
public static String buildSafeClassName(String className, Default defaults) {
return buildSafeClassName( className, defaults.getPackageName() );
}

View File

@ -142,6 +142,8 @@ import org.dom4j.Element;
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*/
// FIXME HHH-14529 Change this class to use JaxbEntityMappings instead of Document.
// I'm delaying this change in order to keep the commits simpler and easier to review.
@SuppressWarnings("unchecked")
public class JPAXMLOverriddenAnnotationReader implements AnnotationReader {
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( JPAXMLOverriddenAnnotationReader.class );
@ -323,7 +325,6 @@ public class JPAXMLOverriddenAnnotationReader implements AnnotationReader {
this( el, xmlContext, bootstrapContext.getClassLoaderAccess() );
}
public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
initAnnotations();
return (T) annotationsMap.get( annotationType );

View File

@ -23,6 +23,7 @@ import javax.persistence.TableGenerator;
import org.hibernate.annotations.common.reflection.AnnotationReader;
import org.hibernate.annotations.common.reflection.MetadataProvider;
import org.hibernate.annotations.common.reflection.java.JavaMetadataProvider;
import org.hibernate.boot.jaxb.spi.XmlMappingOptions;
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.boot.spi.ClassLoaderAccess;
@ -34,6 +35,8 @@ import org.dom4j.Element;
*
* @author Emmanuel Bernard
*/
// FIXME HHH-14529 Change this class to use JaxbEntityMappings instead of Document.
// I'm delaying this change in order to keep the commits simpler and easier to review.
@SuppressWarnings("unchecked")
public final class JPAXMLOverriddenMetadataProvider implements MetadataProvider {
@ -46,7 +49,7 @@ public final class JPAXMLOverriddenMetadataProvider implements MetadataProvider
* We allow fully disabling XML sources so to improve the efficiency of
* the boot process for those not using it.
*/
private final boolean xmlMappingEnabled;
private final XmlMappingOptions xmlMappingOptions;
private Map<Object, Object> defaults;
private Map<AnnotatedElement, AnnotationReader> cache;
@ -54,7 +57,7 @@ public final class JPAXMLOverriddenMetadataProvider implements MetadataProvider
public JPAXMLOverriddenMetadataProvider(BootstrapContext bootstrapContext) {
this.classLoaderAccess = bootstrapContext.getClassLoaderAccess();
this.xmlContext = new XMLContext( classLoaderAccess );
this.xmlMappingEnabled = bootstrapContext.getMetadataBuildingOptions().isXmlMappingEnabled();
this.xmlMappingOptions = bootstrapContext.getMetadataBuildingOptions().getXmlMappingOptions();
}
//all of the above can be safely rebuilt from XMLContext: only XMLContext this object is serialized
@ -87,7 +90,7 @@ public final class JPAXMLOverriddenMetadataProvider implements MetadataProvider
@Override
public Map<Object, Object> getDefaults() {
if ( xmlMappingEnabled == false ) {
if ( !xmlMappingOptions.isEnabled() ) {
return Collections.emptyMap();
}
else {

View File

@ -16,10 +16,12 @@ import javax.persistence.AttributeConverter;
import org.hibernate.AnnotationException;
import org.hibernate.boot.AttributeConverterInfo;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings;
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.boot.spi.ClassLoaderAccess;
import org.hibernate.cfg.AttributeConverterDefinition;
import org.hibernate.cfg.NotYetImplementedException;
import org.hibernate.cfg.annotations.reflection.AttributeConverterDefinitionCollector;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
@ -34,6 +36,8 @@ import org.dom4j.Element;
* @author Emmanuel Bernard
* @author Brett Meyer
*/
// FIXME HHH-14529 Change this class to use JaxbEntityMappings instead of Document.
// I'm delaying this change in order to keep the commits simpler and easier to review.
public class XMLContext implements Serializable {
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( XMLContext.class );
@ -55,6 +59,14 @@ public class XMLContext implements Serializable {
this.classLoaderAccess = bootstrapContext.getClassLoaderAccess();
}
/**
* @param entityMappings The xml entity mappings to add
* @return Add an xml document to this context and return the list of added class names.
*/
public List<String> addDocument(JaxbEntityMappings entityMappings) {
throw new NotYetImplementedException("HHH-14529 Implementation in progress");
}
/**
* @param doc The xml document to add
* @return Add an xml document to this context and return the list of added class names.

View File

@ -0,0 +1,66 @@
/*
* 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.internal.util.xml;
import java.util.Collections;
import org.hibernate.boot.jaxb.spi.XmlMappingOptions;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.selector.SimpleStrategyRegistrationImpl;
import org.hibernate.boot.registry.selector.StrategyRegistration;
import org.hibernate.boot.registry.selector.StrategyRegistrationProvider;
import org.jboss.logging.Logger;
/**
* A strategy registration provider that allows running the whole test suite with different XML mapping options.
* <p>
* By default, this provider does nothing.
* In some CI jobs, we set the system property {@value STRATEGY_PROPERTY_KEY}
* to re-run the whole test suite using JAXB for orm.xml parsing instead of dom4j.
*/
public class XmlMappingOptionsStrategyRegistrationProvider implements StrategyRegistrationProvider {
protected final Logger log = Logger.getLogger( getClass() );
private static final String STRATEGY_PROPERTY_KEY = "testing.mapping.xml.strategy";
public static void applyJaxbStrategy(BootstrapServiceRegistryBuilder builder) {
builder.applyStrategySelector( XmlMappingOptions.class, XmlMappingOptions.DEFAULT_NAME,
PreferJaxbXmlMappingOptions.class
);
}
@Override
public Iterable<StrategyRegistration> getStrategyRegistrations() {
switch ( getStrategyFromSystemProperties() ) {
case "jaxb":
log.warn( "Overriding the default configuration because of a test system property:"
+ " will favor jaxb when parsing XML mapping." );
return Collections.singleton(
new SimpleStrategyRegistrationImpl<>( XmlMappingOptions.class,
PreferJaxbXmlMappingOptions.class,
XmlMappingOptions.DEFAULT_NAME )
);
case "default":
default:
return Collections.emptyList();
}
}
private static String getStrategyFromSystemProperties() {
String strategy = System.getProperty( STRATEGY_PROPERTY_KEY );
return strategy == null || strategy.isEmpty() ? "default" : strategy;
}
public static class PreferJaxbXmlMappingOptions implements XmlMappingOptions {
@Override
public boolean isPreferJaxb() {
return true;
}
}
}

View File

@ -7,6 +7,7 @@
package org.hibernate.test.annotations.reflection;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.internal.util.xml.XmlMappingOptionsStrategyRegistrationProvider;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
@ -20,8 +21,8 @@ public class ElementCollectionConverterTest extends BaseCoreFunctionalTestCase {
@Override
protected void prepareBootstrapRegistryBuilder(BootstrapServiceRegistryBuilder builder) {
// FIXME HHH-14529 configure the BootstrapServiceRegistry to use JAXB for orm.xml mappings
super.prepareBootstrapRegistryBuilder( builder );
XmlMappingOptionsStrategyRegistrationProvider.applyJaxbStrategy( builder );
}
@Override

View File

@ -17,6 +17,7 @@ import org.hibernate.dialect.CockroachDB192Dialect;
import org.hibernate.dialect.PostgreSQL81Dialect;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.dialect.TeradataDialect;
import org.hibernate.internal.util.xml.XmlMappingOptionsStrategyRegistrationProvider;
import org.hibernate.persister.collection.BasicCollectionPersister;
import org.hibernate.testing.SkipForDialect;
@ -35,8 +36,8 @@ public class Ejb3XmlTest extends BaseCoreFunctionalTestCase {
@Override
protected void prepareBootstrapRegistryBuilder(BootstrapServiceRegistryBuilder builder) {
// FIXME HHH-14529 configure the BootstrapServiceRegistry to use JAXB for orm.xml mappings
super.prepareBootstrapRegistryBuilder( builder );
XmlMappingOptionsStrategyRegistrationProvider.applyJaxbStrategy( builder );
}
@Test

View File

@ -10,6 +10,7 @@ import org.hibernate.InvalidMappingException;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.internal.util.xml.UnsupportedOrmXsdVersionException;
import org.hibernate.internal.util.xml.XmlMappingOptionsStrategyRegistrationProvider;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
@ -21,9 +22,10 @@ import static org.junit.Assert.fail;
public class NonExistentOrmVersionTest extends BaseUnitTestCase {
@Test
public void testNonExistentOrmVersion() {
// FIXME HHH-14529 configure the BootstrapServiceRegistry to use JAXB for orm.xml mappings
try {
new MetadataSources( new BootstrapServiceRegistryBuilder().build() )
BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder();
XmlMappingOptionsStrategyRegistrationProvider.applyJaxbStrategy( builder );
new MetadataSources( builder.build() )
.addResource( "org/hibernate/test/annotations/xml/ejb3/orm5.xml" )
.buildMetadata();
fail( "Expecting failure due to unsupported xsd version" );

View File

@ -11,6 +11,7 @@ import org.hibernate.Transaction;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.xml.ErrorLogger;
import org.hibernate.internal.util.xml.XmlMappingOptionsStrategyRegistrationProvider;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
@ -29,8 +30,8 @@ public class OrmVersion1SupportedTest extends BaseCoreFunctionalTestCase {
@Override
protected void prepareBootstrapRegistryBuilder(BootstrapServiceRegistryBuilder builder) {
// FIXME HHH-14529 configure the BootstrapServiceRegistry to use JAXB for orm.xml mappings
super.prepareBootstrapRegistryBuilder( builder );
XmlMappingOptionsStrategyRegistrationProvider.applyJaxbStrategy( builder );
}
@Rule

View File

@ -13,6 +13,7 @@ import java.io.UncheckedIOException;
import org.hibernate.boot.jaxb.spi.Binding;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.internal.util.xml.XmlMappingOptionsStrategyRegistrationProvider;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
@ -25,8 +26,8 @@ public class PreParsedOrmXmlTest extends BaseCoreFunctionalTestCase {
@Override
protected void prepareBootstrapRegistryBuilder(BootstrapServiceRegistryBuilder builder) {
// FIXME HHH-14529 configure the BootstrapServiceRegistry to use JAXB for orm.xml mappings
super.prepareBootstrapRegistryBuilder( builder );
XmlMappingOptionsStrategyRegistrationProvider.applyJaxbStrategy( builder );
}
@Override

View File

@ -0,0 +1 @@
org.hibernate.internal.util.xml.XmlMappingOptionsStrategyRegistrationProvider

View File

@ -24,11 +24,14 @@ import org.hibernate.boot.jaxb.SourceType;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping;
import org.hibernate.boot.jaxb.internal.MappingBinder;
import org.hibernate.boot.jaxb.spi.Binding;
import org.hibernate.boot.jaxb.spi.XmlMappingOptions;
import org.hibernate.boot.model.source.internal.hbm.MappingDocument;
import org.hibernate.boot.spi.AdditionalJaxbMappingProducer;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.boot.spi.MetadataBuildingOptions;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.envers.configuration.internal.MappingCollector;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.ServiceRegistry;
import org.jboss.jandex.IndexView;
@ -39,6 +42,8 @@ import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import static org.hibernate.cfg.AvailableSettings.XML_MAPPING_ENABLED;
/**
* @author Steve Ebersole
*/
@ -51,7 +56,8 @@ public class AdditionalJaxbMappingProducerImpl implements AdditionalJaxbMappingP
IndexView jandexIndex,
final MappingBinder mappingBinder,
final MetadataBuildingContext buildingContext) {
final ServiceRegistry serviceRegistry = metadata.getMetadataBuildingOptions().getServiceRegistry();
MetadataBuildingOptions metadataBuildingOptions = metadata.getMetadataBuildingOptions();
final ServiceRegistry serviceRegistry = metadataBuildingOptions.getServiceRegistry();
final EnversService enversService = serviceRegistry.getService( EnversService.class );
if ( !enversService.isEnabled() ) {
@ -59,6 +65,12 @@ public class AdditionalJaxbMappingProducerImpl implements AdditionalJaxbMappingP
return Collections.emptyList();
}
if ( !metadataBuildingOptions.getXmlMappingOptions().isEnabled() ) {
throw new HibernateException( "Hibernate Envers currently requires XML mapping to be enabled."
+ " Please don't disable setting `" + XML_MAPPING_ENABLED
+ "`; alternatively disable Hibernate Envers." );
}
final ArrayList<MappingDocument> additionalMappingDocuments = new ArrayList<>();
// atm we do not have distinct origin info for envers

View File

@ -9,14 +9,11 @@ package org.hibernate.envers.boot.internal;
import java.util.Map;
import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.envers.configuration.internal.AuditEntitiesConfiguration;
import org.hibernate.envers.configuration.internal.EntitiesConfigurator;
import org.hibernate.envers.configuration.internal.GlobalConfiguration;
@ -39,8 +36,6 @@ import org.hibernate.service.spi.Stoppable;
import org.jboss.logging.Logger;
import static org.hibernate.cfg.AvailableSettings.XML_MAPPING_ENABLED;
/**
* Provides central access to Envers' configuration.
*
@ -89,10 +84,6 @@ public class EnversServiceImpl implements EnversService, Configurable, Stoppable
);
}
this.integrationEnabled = ConfigurationHelper.getBoolean( INTEGRATION_ENABLED, configurationValues, true );
boolean xmlMappingEnabled = ConfigurationHelper.getBoolean( XML_MAPPING_ENABLED, configurationValues, true );
if ( this.integrationEnabled && !xmlMappingEnabled ) {
throw new HibernateException( "Hibernate Envers currently requires XML mapping to be enabled. Please don't disable setting `" + XML_MAPPING_ENABLED + "`; alternatively disable Hibernate Envers." );
}
log.infof( "Envers integration enabled? : %s", integrationEnabled );
}