Skip to content

Commit 395ce0d

Browse files
committed
HHH-18060 - HbmXmlTransformer
testing an alternative approach using the boot model
1 parent f36f416 commit 395ce0d

File tree

61 files changed

+2775
-2143
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+2775
-2143
lines changed

hibernate-core/src/main/java/org/hibernate/boot/internal/MetadataBuilderImpl.java

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
import org.hibernate.boot.cfgxml.spi.CfgXmlAccessService;
2727
import org.hibernate.boot.cfgxml.spi.LoadedConfig;
2828
import org.hibernate.boot.cfgxml.spi.MappingReference;
29+
import org.hibernate.boot.jaxb.Origin;
30+
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping;
31+
import org.hibernate.boot.jaxb.hbm.transform.HbmXmlTransformer;
32+
import org.hibernate.boot.jaxb.hbm.transform.UnsupportedFeatureHandling;
33+
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappingsImpl;
34+
import org.hibernate.boot.jaxb.spi.Binding;
2935
import org.hibernate.boot.model.FunctionContributions;
3036
import org.hibernate.boot.model.FunctionContributor;
3137
import org.hibernate.boot.model.TypeContributions;
@@ -59,15 +65,18 @@
5965
import org.hibernate.cache.spi.RegionFactory;
6066
import org.hibernate.cache.spi.access.AccessType;
6167
import org.hibernate.cfg.AvailableSettings;
68+
import org.hibernate.cfg.MappingSettings;
6269
import org.hibernate.dialect.Dialect;
6370
import org.hibernate.dialect.TimeZoneSupport;
6471
import org.hibernate.engine.config.spi.ConfigurationService;
72+
import org.hibernate.engine.config.spi.StandardConverters;
6573
import org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl;
6674
import org.hibernate.engine.jdbc.spi.JdbcServices;
6775
import org.hibernate.internal.CoreLogging;
6876
import org.hibernate.internal.CoreMessageLogger;
6977
import org.hibernate.internal.log.DeprecationLogger;
7078
import org.hibernate.internal.util.NullnessHelper;
79+
import org.hibernate.internal.util.collections.CollectionHelper;
7180
import org.hibernate.metamodel.CollectionClassification;
7281
import org.hibernate.query.sqm.function.SqmFunctionDescriptor;
7382
import org.hibernate.query.sqm.function.SqmFunctionRegistry;
@@ -105,7 +114,7 @@ public MetadataBuilderImpl(MetadataSources sources) {
105114
this( sources, getStandardServiceRegistry( sources.getServiceRegistry() ) );
106115
}
107116

108-
private static StandardServiceRegistry getStandardServiceRegistry(ServiceRegistry serviceRegistry) {
117+
public static StandardServiceRegistry getStandardServiceRegistry(ServiceRegistry serviceRegistry) {
109118
if ( serviceRegistry == null ) {
110119
throw new HibernateException( "ServiceRegistry passed to MetadataBuilder cannot be null" );
111120
}
@@ -435,7 +444,52 @@ public MetadataImplementor build() {
435444
}
436445
}
437446

438-
return MetadataBuildingProcess.build( sources, bootstrapContext, options );
447+
final MetadataImplementor bootModel = MetadataBuildingProcess.build( sources, bootstrapContext, options );
448+
449+
if ( CollectionHelper.isNotEmpty( sources.getHbmXmlBindings() ) ) {
450+
final ConfigurationService configurationService = bootstrapContext.getServiceRegistry().getService( ConfigurationService.class );
451+
final boolean transformHbm = configurationService != null
452+
&& configurationService.getSetting( MappingSettings.TRANSFORM_HBM_XML, BOOLEAN,false );
453+
454+
if ( !transformHbm ) {
455+
for ( Binding<JaxbHbmHibernateMapping> hbmXmlBinding : sources.getHbmXmlBindings() ) {
456+
final Origin origin = hbmXmlBinding.getOrigin();
457+
DeprecationLogger.DEPRECATION_LOGGER.logDeprecatedHbmXmlProcessing( origin.getType(), origin.getName() );
458+
}
459+
}
460+
else {
461+
final List<Binding<JaxbEntityMappingsImpl>> transformed = HbmXmlTransformer.transform(
462+
sources.getHbmXmlBindings(),
463+
bootModel,
464+
bootstrapContext.getServiceRegistry(),
465+
UnsupportedFeatureHandling.fromSetting(
466+
configurationService.getSettings().get( AvailableSettings.TRANSFORM_HBM_XML_FEATURE_HANDLING ),
467+
UnsupportedFeatureHandling.ERROR
468+
)
469+
);
470+
471+
final MetadataSources newSources = new MetadataSources( bootstrapContext.getServiceRegistry() );
472+
if ( sources.getAnnotatedClasses() != null ) {
473+
sources.getAnnotatedClasses().forEach( newSources::addAnnotatedClass );
474+
}
475+
if ( sources.getAnnotatedClassNames() != null ) {
476+
sources.getAnnotatedClassNames().forEach( newSources::addAnnotatedClassName );
477+
}
478+
if ( sources.getAnnotatedPackages() != null ) {
479+
sources.getAnnotatedPackages().forEach( newSources::addPackage );
480+
}
481+
if ( sources.getExtraQueryImports() != null ) {
482+
sources.getExtraQueryImports().forEach( newSources::addQueryImport );
483+
}
484+
for ( Binding<JaxbEntityMappingsImpl> mappingXmlBinding : transformed ) {
485+
newSources.addMappingXmlBinding( mappingXmlBinding );
486+
}
487+
488+
return (MetadataImplementor) newSources.buildMetadata();
489+
}
490+
}
491+
492+
return bootModel;
439493
}
440494

441495
@Override
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
6+
*/
7+
package org.hibernate.boot.jaxb.hbm.transform;
8+
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
import org.hibernate.boot.internal.MetadataImpl;
13+
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping;
14+
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityImpl;
15+
import org.hibernate.boot.jaxb.spi.Binding;
16+
import org.hibernate.boot.spi.MetadataImplementor;
17+
import org.hibernate.mapping.Collection;
18+
import org.hibernate.mapping.Component;
19+
import org.hibernate.mapping.IndexedCollection;
20+
import org.hibernate.mapping.PersistentClass;
21+
import org.hibernate.mapping.Property;
22+
import org.hibernate.mapping.RootClass;
23+
import org.hibernate.mapping.Table;
24+
import org.hibernate.service.ServiceRegistry;
25+
26+
/**
27+
* @author Steve Ebersole
28+
*/
29+
public class BootModelPreprocessor {
30+
static void preprocessBooModel(
31+
List<Binding<JaxbHbmHibernateMapping>> hbmXmlBindings,
32+
MetadataImplementor bootModel,
33+
ServiceRegistry serviceRegistry,
34+
TransformationState transformationState) {
35+
bootModel.getEntityBindings().forEach( (persistentClass) -> {
36+
final Table table = TransformationHelper.determineEntityTable( persistentClass );
37+
final EntityTypeInfo entityTypeInfo = new EntityTypeInfo( table, persistentClass );
38+
transformationState.getEntityInfoByName().put( persistentClass.getEntityName(), entityTypeInfo );
39+
buildPersistentClassPropertyInfos( persistentClass, entityTypeInfo, bootModel, transformationState );
40+
} );
41+
}
42+
43+
private static void buildPersistentClassPropertyInfos(
44+
PersistentClass persistentClass,
45+
EntityTypeInfo entityTypeInfo,
46+
MetadataImplementor bootModel,
47+
TransformationState transformationState) {
48+
if ( persistentClass instanceof RootClass rootClass ) {
49+
if ( persistentClass.getIdentifierProperty() != null ) {
50+
if ( persistentClass.getIdentifierProperty().getValue() instanceof Component component ) {
51+
final String componentRole = rootClass.getEntityName() + "." + persistentClass.getIdentifierProperty().getName();
52+
buildComponentEntries( componentRole, component, transformationState );
53+
}
54+
}
55+
else {
56+
assert rootClass.getIdentifier() instanceof Component;
57+
final String componentRole = rootClass.getEntityName() + ".id";
58+
buildComponentEntries( componentRole, (Component) rootClass.getIdentifier(), transformationState );
59+
}
60+
}
61+
62+
persistentClass.getProperties().forEach( (property) -> processProperty(
63+
entityTypeInfo.propertyInfoMap(),
64+
property,
65+
persistentClass.getEntityName(),
66+
transformationState
67+
) );
68+
69+
persistentClass.getJoins().forEach( (join) -> {
70+
join.getProperties().forEach( (property) -> processProperty(
71+
entityTypeInfo.propertyInfoMap(),
72+
property,
73+
persistentClass.getEntityName(),
74+
transformationState
75+
) );
76+
} );
77+
}
78+
79+
private static void processProperty(
80+
Map<String, PropertyInfo> entityTypeInfo,
81+
Property property,
82+
String entityName,
83+
TransformationState transformationState) {
84+
entityTypeInfo.put( property.getName(), new PropertyInfo( property ) );
85+
86+
if ( property.getValue() instanceof Component component ) {
87+
final String componentRole = entityName + "." + property.getName();
88+
buildComponentEntries( componentRole, component, transformationState );
89+
}
90+
else if ( property.getValue() instanceof IndexedCollection indexedCollection ) {
91+
if ( indexedCollection.getIndex() instanceof Component index ) {
92+
final String componentRole = entityName + "." + property.getName() + ".key";
93+
buildComponentEntries( componentRole, index, transformationState );
94+
}
95+
if ( indexedCollection.getElement() instanceof Component element ) {
96+
final String componentRole = entityName + "." + property.getName() + ".value";
97+
buildComponentEntries( componentRole, element, transformationState );
98+
}
99+
}
100+
else if ( property.getValue() instanceof Collection collection ) {
101+
if ( collection.getElement() instanceof Component element ) {
102+
final String componentRole = entityName + "." + property.getName() + ".value";
103+
buildComponentEntries( componentRole, element, transformationState );
104+
}
105+
}
106+
}
107+
108+
private static void buildComponentEntries(
109+
String role,
110+
Component component,
111+
TransformationState transformationState) {
112+
final ComponentTypeInfo componentTypeInfo = new ComponentTypeInfo( component );
113+
transformationState.getEmbeddableInfoByRole().put( role, componentTypeInfo );
114+
115+
buildComponentPropertyInfos( role, component, componentTypeInfo, transformationState );
116+
}
117+
118+
private static void buildComponentPropertyInfos(
119+
String componentRole,
120+
Component component,
121+
ComponentTypeInfo componentTypeInfo,
122+
TransformationState transformationState) {
123+
124+
component.getProperties().forEach( (property) -> {
125+
processProperty( componentTypeInfo.propertyInfoMap(), property, componentRole, transformationState );
126+
} );
127+
128+
}
129+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
6+
*/
7+
package org.hibernate.boot.jaxb.hbm.transform;
8+
9+
import org.hibernate.mapping.Component;
10+
11+
/**
12+
* @author Steve Ebersole
13+
*/
14+
public class ComponentTypeInfo extends ManagedTypeInfo {
15+
private final Component component;
16+
17+
public ComponentTypeInfo(Component component) {
18+
super( component.getTable() );
19+
this.component = component;
20+
}
21+
22+
public Component getComponent() {
23+
return component;
24+
}
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
6+
*/
7+
package org.hibernate.boot.jaxb.hbm.transform;
8+
9+
import java.util.Map;
10+
11+
import org.hibernate.mapping.PersistentClass;
12+
import org.hibernate.mapping.Table;
13+
14+
/**
15+
* @author Steve Ebersole
16+
*/
17+
public class EntityTypeInfo extends ManagedTypeInfo {
18+
private final PersistentClass persistentClass;
19+
20+
public EntityTypeInfo(
21+
Table table,
22+
PersistentClass persistentClass) {
23+
super( table );
24+
this.persistentClass = persistentClass;
25+
}
26+
27+
public PersistentClass getPersistentClass() {
28+
return persistentClass;
29+
}
30+
}

0 commit comments

Comments
 (0)