Skip to content

Commit d04783f

Browse files
committed
HHH-19388 Process <database-object> in mapping.xml
1 parent cf96edb commit d04783f

File tree

7 files changed

+119
-1
lines changed

7 files changed

+119
-1
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotationBinder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.hibernate.boot.model.IdentifierGeneratorDefinition;
2626
import org.hibernate.boot.model.NamedEntityGraphDefinition;
2727
import org.hibernate.boot.model.convert.spi.RegisteredConversion;
28+
import org.hibernate.boot.model.source.internal.hbm.AuxiliaryDatabaseObjectBinder;
2829
import org.hibernate.boot.models.HibernateAnnotations;
2930
import org.hibernate.boot.models.JpaAnnotations;
3031
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
@@ -117,6 +118,10 @@ public static void bindDefaults(MetadataBuildingContext context) {
117118
QueryBinder.bindNamedStoredProcedureQuery( queryRegistration.configuration(), context, true );
118119
} );
119120

121+
globalRegistrations.getDatabaseObjectRegistrations().forEach( databaseObjectRegistration ->
122+
AuxiliaryDatabaseObjectBinder.processAuxiliaryDatabaseObject( context, databaseObjectRegistration )
123+
);
124+
120125
}
121126

122127
private static ModelsContext modelsContext(MetadataBuildingContext context) {

hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/AuxiliaryDatabaseObjectBinder.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
*/
55
package org.hibernate.boot.model.source.internal.hbm;
66

7+
import org.hibernate.MappingException;
78
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmAuxiliaryDatabaseObjectType;
89
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmDialectScopeType;
910
import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject;
1011
import org.hibernate.boot.model.relational.SimpleAuxiliaryDatabaseObject;
12+
import org.hibernate.boot.models.spi.DatabaseObjectRegistration;
13+
import org.hibernate.boot.models.spi.DialectScopeRegistration;
1114
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
15+
import org.hibernate.boot.spi.MetadataBuildingContext;
16+
import org.hibernate.internal.util.StringHelper;
1217

1318
/**
1419
* @author Steve Ebersole
@@ -68,4 +73,51 @@ public static void processAuxiliaryDatabaseObject(
6873

6974
context.getMetadataCollector().getDatabase().addAuxiliaryDatabaseObject( auxDbObject );
7075
}
76+
77+
public static void processAuxiliaryDatabaseObject(
78+
MetadataBuildingContext context,
79+
DatabaseObjectRegistration databaseObjectRegistration) {
80+
final AuxiliaryDatabaseObject auxDbObject;
81+
82+
if ( StringHelper.isNotEmpty( databaseObjectRegistration.definition()) ) {
83+
try {
84+
auxDbObject = (AuxiliaryDatabaseObject)
85+
context.getBootstrapContext().getClassLoaderService()
86+
.classForName( databaseObjectRegistration.definition() )
87+
.newInstance();
88+
}
89+
catch (ClassLoadingException cle) {
90+
throw cle;
91+
}
92+
catch (Exception e) {
93+
throw new MappingException(
94+
String.format(
95+
"Unable to instantiate custom AuxiliaryDatabaseObject class [%s]",
96+
databaseObjectRegistration.definition()
97+
)
98+
);
99+
}
100+
}
101+
else {
102+
auxDbObject = new SimpleAuxiliaryDatabaseObject(
103+
context.getMetadataCollector().getDatabase().getDefaultNamespace(),
104+
databaseObjectRegistration.create(),
105+
databaseObjectRegistration.drop(),
106+
null
107+
);
108+
}
109+
110+
if ( !databaseObjectRegistration.dialectScopes().isEmpty() ) {
111+
if ( auxDbObject instanceof AuxiliaryDatabaseObject.Expandable expandable ) {
112+
for ( DialectScopeRegistration dialectScopeRegistration : databaseObjectRegistration.dialectScopes() ) {
113+
expandable.addDialectScope( dialectScopeRegistration.name());
114+
}
115+
}
116+
else {
117+
// error? warn?
118+
}
119+
}
120+
121+
context.getMetadataCollector().getDatabase().addAuxiliaryDatabaseObject( auxDbObject );
122+
}
71123
}

hibernate-core/src/main/java/org/hibernate/boot/models/internal/DomainModelCategorizationCollector.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public void apply(JaxbEntityMappingsImpl jaxbRoot, XmlDocumentContext xmlDocumen
8585

8686
getGlobalRegistrations().collectQueryReferences( jaxbRoot, xmlDocumentContext );
8787

88+
getGlobalRegistrations().collectDataBaseObject( jaxbRoot.getDatabaseObjects() );
8889
// todo (7.0) : named graphs?
8990
}
9091

hibernate-core/src/main/java/org/hibernate/boot/models/internal/GlobalRegistrationsImpl.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.hibernate.boot.jaxb.mapping.spi.JaxbConfigurationParameterImpl;
2525
import org.hibernate.boot.jaxb.mapping.spi.JaxbConverterImpl;
2626
import org.hibernate.boot.jaxb.mapping.spi.JaxbConverterRegistrationImpl;
27+
import org.hibernate.boot.jaxb.mapping.spi.JaxbDatabaseObjectImpl;
28+
import org.hibernate.boot.jaxb.mapping.spi.JaxbDatabaseObjectScopeImpl;
2729
import org.hibernate.boot.jaxb.mapping.spi.JaxbEmbeddableInstantiatorRegistrationImpl;
2830
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListenerImpl;
2931
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappingsImpl;
@@ -53,6 +55,8 @@
5355
import org.hibernate.boot.models.spi.CompositeUserTypeRegistration;
5456
import org.hibernate.boot.models.spi.ConversionRegistration;
5557
import org.hibernate.boot.models.spi.ConverterRegistration;
58+
import org.hibernate.boot.models.spi.DatabaseObjectRegistration;
59+
import org.hibernate.boot.models.spi.DialectScopeRegistration;
5660
import org.hibernate.boot.models.spi.EmbeddableInstantiatorRegistration;
5761
import org.hibernate.boot.models.spi.FilterDefRegistration;
5862
import org.hibernate.boot.models.spi.GenericGeneratorRegistration;
@@ -144,6 +148,8 @@ public class GlobalRegistrationsImpl implements GlobalRegistrations, GlobalRegis
144148
private Map<String, NamedNativeQueryRegistration> namedNativeQueryRegistrations;
145149
private Map<String, NamedStoredProcedureQueryRegistration> namedStoredProcedureQueryRegistrations;
146150

151+
private List<DatabaseObjectRegistration> databaseObjectRegistrations;
152+
147153
public GlobalRegistrationsImpl(ModelsContext sourceModelContext, BootstrapContext bootstrapContext) {
148154
this.sourceModelContext = sourceModelContext;
149155
this.bootstrapContext = bootstrapContext;
@@ -242,9 +248,13 @@ public Map<String, NamedNativeQueryRegistration> getNamedNativeQueryRegistration
242248

243249
@Override
244250
public Map<String, NamedStoredProcedureQueryRegistration> getNamedStoredProcedureQueryRegistrations() {
245-
return namedStoredProcedureQueryRegistrations== null ? emptyMap() : namedStoredProcedureQueryRegistrations;
251+
return namedStoredProcedureQueryRegistrations == null ? emptyMap() : namedStoredProcedureQueryRegistrations;
246252
}
247253

254+
@Override
255+
public List<DatabaseObjectRegistration> getDatabaseObjectRegistrations() {
256+
return databaseObjectRegistrations == null ? emptyList() : databaseObjectRegistrations;
257+
}
248258

249259
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
250260
// JavaTypeRegistration
@@ -1314,4 +1324,34 @@ private List<QueryHint> extractQueryHints(JaxbQueryHintContainer jaxbQuery) {
13141324
}
13151325
return hints;
13161326
}
1327+
1328+
public void collectDataBaseObject(List<JaxbDatabaseObjectImpl> databaseObjects) {
1329+
if ( isEmpty( databaseObjects ) ) {
1330+
return;
1331+
}
1332+
1333+
if ( databaseObjectRegistrations == null ) {
1334+
databaseObjectRegistrations = new ArrayList<>();
1335+
}
1336+
1337+
for ( JaxbDatabaseObjectImpl jaxbDatabaseObject : databaseObjects ) {
1338+
final JaxbDatabaseObjectImpl.JaxbDefinitionImpl definition = jaxbDatabaseObject.getDefinition();
1339+
final List<JaxbDatabaseObjectScopeImpl> dialectScopes = jaxbDatabaseObject.getDialectScopes();
1340+
final List<DialectScopeRegistration> scopeRegistrations = new ArrayList<>(dialectScopes.size());
1341+
for ( JaxbDatabaseObjectScopeImpl dialectScope : dialectScopes ) {
1342+
scopeRegistrations.add(
1343+
new DialectScopeRegistration(
1344+
dialectScope.getName(),
1345+
dialectScope.getContent(),
1346+
dialectScope.getMinimumVersion(),
1347+
dialectScope.getMaximumVersion() )
1348+
);
1349+
}
1350+
databaseObjectRegistrations.add( new DatabaseObjectRegistration(
1351+
jaxbDatabaseObject.getCreate(),
1352+
jaxbDatabaseObject.getDrop(),
1353+
definition != null ? definition.getClazz() : null,
1354+
scopeRegistrations ) );
1355+
}
1356+
}
13171357
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.boot.models.spi;
6+
7+
import java.util.List;
8+
9+
public record DatabaseObjectRegistration(String create, String drop, String definition, List<DialectScopeRegistration> dialectScopes ) {
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.boot.models.spi;
6+
7+
public record DialectScopeRegistration(String name, String content, String minimumVersion, String maximumVersion) {
8+
}

hibernate-core/src/main/java/org/hibernate/boot/models/spi/GlobalRegistrations.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public interface GlobalRegistrations {
5252

5353
Map<String, NamedStoredProcedureQueryRegistration> getNamedStoredProcedureQueryRegistrations();
5454

55+
List<DatabaseObjectRegistration> getDatabaseObjectRegistrations();
56+
5557
// todo : named entity graphs
5658

5759
<T> T as(Class<T> type);

0 commit comments

Comments
 (0)