Skip to content

Commit eaf562b

Browse files
committed
HHH-9923 - Avoid cast to MetadataBuildingOptionsImpl in AnnotationMetadataSourceProcessorImpl#prepare()
1 parent 37cc060 commit eaf562b

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
5151
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
5252
import org.hibernate.boot.registry.selector.spi.StrategySelector;
53+
import org.hibernate.boot.spi.JpaOrmXmlPersistenceUnitDefaultAware;
5354
import org.hibernate.boot.spi.MappingDefaults;
5455
import org.hibernate.boot.spi.MetadataBuilderImplementor;
5556
import org.hibernate.boot.spi.MetadataBuilderInitializer;
@@ -517,7 +518,8 @@ public AccessType getImplicitCacheAccessType() {
517518
}
518519
}
519520

520-
public static class MetadataBuildingOptionsImpl implements MetadataBuildingOptions {
521+
public static class MetadataBuildingOptionsImpl
522+
implements MetadataBuildingOptions, JpaOrmXmlPersistenceUnitDefaultAware {
521523
private final StandardServiceRegistry serviceRegistry;
522524
private final MappingDefaultsImpl mappingDefaults;
523525

@@ -885,12 +887,6 @@ public void addAttributeConverterDefinition(AttributeConverterDefinition definit
885887
}
886888
}
887889

888-
public static interface JpaOrmXmlPersistenceUnitDefaults {
889-
public String getDefaultSchemaName();
890-
public String getDefaultCatalogName();
891-
public boolean shouldImplicitlyQuoteIdentifiers();
892-
}
893-
894890
/**
895891
* Yuck. This is needed because JPA lets users define "global building options"
896892
* in {@code orm.xml} mappings. Forget that there are generally multiple

hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/annotations/AnnotationMetadataSourceProcessorImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
import org.hibernate.annotations.common.reflection.MetadataProviderInjector;
2222
import org.hibernate.annotations.common.reflection.ReflectionManager;
2323
import org.hibernate.annotations.common.reflection.XClass;
24-
import org.hibernate.boot.internal.MetadataBuilderImpl.MetadataBuildingOptionsImpl;
25-
import org.hibernate.boot.internal.MetadataBuilderImpl.MetadataBuildingOptionsImpl.JpaOrmXmlPersistenceUnitDefaults;
2624
import org.hibernate.boot.internal.MetadataBuildingContextRootImpl;
2725
import org.hibernate.boot.jaxb.spi.Binding;
2826
import org.hibernate.boot.model.process.spi.ManagedResources;
2927
import org.hibernate.boot.model.source.spi.MetadataSourceProcessor;
3028
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
29+
import org.hibernate.boot.spi.JpaOrmXmlPersistenceUnitDefaultAware;
30+
import org.hibernate.boot.spi.JpaOrmXmlPersistenceUnitDefaultAware.JpaOrmXmlPersistenceUnitDefaults;
3131
import org.hibernate.cfg.AnnotationBinder;
3232
import org.hibernate.cfg.AttributeConverterDefinition;
3333
import org.hibernate.cfg.InheritanceState;
@@ -154,7 +154,7 @@ private XClass toXClass(String className, ReflectionManager reflectionManager) {
154154
@Override
155155
public void prepare() {
156156
// use any persistence-unit-defaults defined in orm.xml
157-
( ( MetadataBuildingOptionsImpl ) rootMetadataBuildingContext.getBuildingOptions() ).apply(
157+
( (JpaOrmXmlPersistenceUnitDefaultAware) rootMetadataBuildingContext.getBuildingOptions() ).apply(
158158
new JpaOrmXmlPersistenceUnitDefaults() {
159159
final Map persistenceUnitDefaults = reflectionManager.getDefaults();
160160

hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingMetadataBuildingOptions.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import javax.persistence.SharedCacheMode;
1313

14+
import org.hibernate.HibernateException;
1415
import org.hibernate.MultiTenancyStrategy;
1516
import org.hibernate.annotations.common.reflection.ReflectionManager;
1617
import org.hibernate.boot.CacheRegionDefinition;
@@ -33,8 +34,10 @@
3334
* Convenience base class for custom implementors of {@link MetadataBuildingOptions} using delegation.
3435
*
3536
* @author Gunnar Morling
37+
* @author Steve Ebersole
3638
*/
37-
public abstract class AbstractDelegatingMetadataBuildingOptions implements MetadataBuildingOptions {
39+
@SuppressWarnings("unused")
40+
public abstract class AbstractDelegatingMetadataBuildingOptions implements MetadataBuildingOptions, JpaOrmXmlPersistenceUnitDefaultAware {
3841

3942
private final MetadataBuildingOptions delegate;
4043

@@ -171,4 +174,18 @@ public List<AuxiliaryDatabaseObject> getAuxiliaryDatabaseObjectList() {
171174
public List<AttributeConverterDefinition> getAttributeConverters() {
172175
return delegate.getAttributeConverters();
173176
}
177+
178+
@Override
179+
public void apply(JpaOrmXmlPersistenceUnitDefaults jpaOrmXmlPersistenceUnitDefaults) {
180+
if ( delegate instanceof JpaOrmXmlPersistenceUnitDefaultAware ) {
181+
( (JpaOrmXmlPersistenceUnitDefaultAware) delegate ).apply( jpaOrmXmlPersistenceUnitDefaults );
182+
}
183+
else {
184+
throw new HibernateException(
185+
"AbstractDelegatingMetadataBuildingOptions delegate did not " +
186+
"implement JpaOrmXmlPersistenceUnitDefaultAware; " +
187+
"cannot delegate JpaOrmXmlPersistenceUnitDefaultAware#apply"
188+
);
189+
}
190+
}
174191
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.spi;
8+
9+
/**
10+
* Contract for things that need to be aware of JPA {@code orm.xml}-defined persistence-unit-defaults. Only
11+
* MetadataBuildingOptions are supported to implement this contract.
12+
* <p/>
13+
* NOTE: it is expected that this contract will go away as we migrate to Jandex for annotation processing
14+
* and move to the annotation binding constructs done on the metamodel branch.
15+
*
16+
* @since 5.0
17+
*
18+
* @author Steve Ebersole
19+
*/
20+
public interface JpaOrmXmlPersistenceUnitDefaultAware {
21+
/**
22+
* Represents the {@code persistence-unit-defaults} to be applied
23+
*/
24+
interface JpaOrmXmlPersistenceUnitDefaults {
25+
String getDefaultSchemaName();
26+
String getDefaultCatalogName();
27+
boolean shouldImplicitlyQuoteIdentifiers();
28+
}
29+
30+
/**
31+
* Apply the {@code orm.xml}-defined {@code persistence-unit-defaults} values.
32+
*
33+
* @param jpaOrmXmlPersistenceUnitDefaults The {@code persistence-unit-defaults} values
34+
*/
35+
void apply(JpaOrmXmlPersistenceUnitDefaults jpaOrmXmlPersistenceUnitDefaults);
36+
}

0 commit comments

Comments
 (0)