Skip to content

Commit

Permalink
#46 - Support <entity/> overrides
Browse files Browse the repository at this point in the history
#47 - Support dynamic <entity/> and <embeddable/> mappings

#46
#47
  • Loading branch information
sebersole committed Oct 23, 2023
1 parent 3de7cbc commit 5ba6a8a
Show file tree
Hide file tree
Showing 25 changed files with 1,282 additions and 412 deletions.
2 changes: 1 addition & 1 deletion hibernate-models-orm/hibernate-models-orm.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies {
annotationProcessor libs.loggingAnnotations

testImplementation project( ":hibernate-models-testing" )
testImplementation libs.hibernateTesting
// testImplementation libs.hibernateTesting
testImplementation testLibs.jpa
testRuntimeOnly testLibs.log4j
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.annotations.internal;

import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.Retention;

/**
* Used to configure inheritance in dynamic models defined in XML
*
* @author Steve Ebersole
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Extends {
/**
* The type extended. String because dynamic models have named "classes".
*/
String superType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.function.Consumer;

import org.hibernate.annotations.*;
import org.hibernate.annotations.internal.Extends;
import org.hibernate.models.orm.internal.OrmAnnotationHelper;
import org.hibernate.models.source.spi.AnnotationDescriptor;

Expand Down Expand Up @@ -67,6 +68,7 @@ public interface HibernateAnnotations {
AnnotationDescriptor<EmbeddableInstantiator> EMBEDDABLE_INSTANTIATOR = createOrmDescriptor( EmbeddableInstantiator.class );
AnnotationDescriptor<EmbeddableInstantiatorRegistrations> EMBEDDABLE_INSTANTIATOR_REGS = createOrmDescriptor( EmbeddableInstantiatorRegistrations.class );
AnnotationDescriptor<EmbeddableInstantiatorRegistration> EMBEDDABLE_INSTANTIATOR_REG = createOrmDescriptor( EmbeddableInstantiatorRegistration.class, EMBEDDABLE_INSTANTIATOR_REGS );
AnnotationDescriptor<Extends> EXTENDS = createOrmDescriptor( Extends.class );
AnnotationDescriptor<Fetch> FETCH = createOrmDescriptor( Fetch.class );
AnnotationDescriptor<FetchProfiles> FETCH_PROFILES = createOrmDescriptor( FetchProfiles.class );
AnnotationDescriptor<FetchProfile> FETCH_PROFILE = createOrmDescriptor( FetchProfile.class, FETCH_PROFILES );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.ParamDef;
import org.hibernate.annotations.Parameter;
import org.hibernate.boot.jaxb.mapping.JaxbCollectionUserTypeRegistration;
import org.hibernate.boot.jaxb.mapping.JaxbCompositeUserTypeRegistration;
import org.hibernate.boot.jaxb.mapping.JaxbConfigurationParameter;
import org.hibernate.boot.jaxb.mapping.JaxbConverterRegistration;
import org.hibernate.boot.jaxb.mapping.JaxbEmbeddableInstantiatorRegistration;
import org.hibernate.boot.jaxb.mapping.JaxbEntityListener;
import org.hibernate.boot.jaxb.mapping.JaxbEntityMappings;
import org.hibernate.boot.jaxb.mapping.JaxbFilterDef;
import org.hibernate.boot.jaxb.mapping.JaxbGenericIdGenerator;
import org.hibernate.boot.jaxb.mapping.JaxbJavaTypeRegistration;
import org.hibernate.boot.jaxb.mapping.JaxbJdbcTypeRegistration;
import org.hibernate.boot.jaxb.mapping.JaxbSequenceGenerator;
import org.hibernate.boot.jaxb.mapping.JaxbTableGenerator;
import org.hibernate.boot.jaxb.mapping.JaxbUserTypeRegistration;
import org.hibernate.boot.jaxb.mapping.spi.JaxbCollectionUserTypeRegistrationImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbCompositeUserTypeRegistrationImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbConfigurationParameterImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbConverterRegistrationImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEmbeddableInstantiatorRegistrationImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListenerImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappingsImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbFilterDefImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbGenericIdGeneratorImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbJavaTypeRegistrationImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbJdbcTypeRegistrationImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbSequenceGeneratorImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbTableGeneratorImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbUserTypeRegistrationImpl;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.metamodel.CollectionClassification;
import org.hibernate.models.internal.StringHelper;
Expand Down Expand Up @@ -167,7 +167,7 @@ public void collectJavaTypeRegistrations(AnnotationTarget annotationTarget) {
) );
}

public void collectJavaTypeRegistrations(List<JaxbJavaTypeRegistration> registrations) {
public void collectJavaTypeRegistrations(List<JaxbJavaTypeRegistrationImpl> registrations) {
if ( CollectionHelper.isEmpty( registrations ) ) {
return;
}
Expand Down Expand Up @@ -200,7 +200,7 @@ public void collectJdbcTypeRegistrations(AnnotationTarget annotationTarget) {
) );
}

public void collectJdbcTypeRegistrations(List<JaxbJdbcTypeRegistration> registrations) {
public void collectJdbcTypeRegistrations(List<JaxbJdbcTypeRegistrationImpl> registrations) {
if ( CollectionHelper.isEmpty( registrations ) ) {
return;
}
Expand Down Expand Up @@ -231,7 +231,7 @@ public void collectConverterRegistrations(AnnotationTarget annotationTarget) {
} );
}

public void collectConverterRegistrations(List<JaxbConverterRegistration> registrations) {
public void collectConverterRegistrations(List<JaxbConverterRegistrationImpl> registrations) {
if ( CollectionHelper.isEmpty( registrations ) ) {
return;
}
Expand Down Expand Up @@ -269,7 +269,7 @@ public void collectUserTypeRegistrations(AnnotationTarget annotationTarget) {
) );
}

public void collectUserTypeRegistrations(List<JaxbUserTypeRegistration> registrations) {
public void collectUserTypeRegistrations(List<JaxbUserTypeRegistrationImpl> registrations) {
if ( CollectionHelper.isEmpty( registrations ) ) {
return;
}
Expand Down Expand Up @@ -299,7 +299,7 @@ public void collectCompositeUserTypeRegistrations(AnnotationTarget annotationTar
) );
}

public void collectCompositeUserTypeRegistrations(List<JaxbCompositeUserTypeRegistration> registrations) {
public void collectCompositeUserTypeRegistrations(List<JaxbCompositeUserTypeRegistrationImpl> registrations) {
if ( CollectionHelper.isEmpty( registrations ) ) {
return;
}
Expand Down Expand Up @@ -342,7 +342,7 @@ private Map<String,String> extractParameterMap(AnnotationUsage<? extends Annotat
return result;
}

public void collectCollectionTypeRegistrations(List<JaxbCollectionUserTypeRegistration> registrations) {
public void collectCollectionTypeRegistrations(List<JaxbCollectionUserTypeRegistrationImpl> registrations) {
if ( CollectionHelper.isEmpty( registrations ) ) {
return;
}
Expand All @@ -354,7 +354,7 @@ public void collectCollectionTypeRegistrations(List<JaxbCollectionUserTypeRegist
) );
}

private Map<String, String> extractParameterMap(List<JaxbConfigurationParameter> parameters) {
private Map<String, String> extractParameterMap(List<JaxbConfigurationParameterImpl> parameters) {
if ( CollectionHelper.isEmpty( parameters ) ) {
return Collections.emptyMap();
}
Expand Down Expand Up @@ -385,7 +385,7 @@ public void collectEmbeddableInstantiatorRegistrations(AnnotationTarget annotati
) );
}

public void collectEmbeddableInstantiatorRegistrations(List<JaxbEmbeddableInstantiatorRegistration> registrations) {
public void collectEmbeddableInstantiatorRegistrations(List<JaxbEmbeddableInstantiatorRegistrationImpl> registrations) {
if ( CollectionHelper.isEmpty( registrations ) ) {
return;
}
Expand Down Expand Up @@ -424,7 +424,7 @@ private Map<String, ClassDetails> extractFilterParameters(AnnotationUsage<Filter
return result;
}

public void collectFilterDefinitions(List<JaxbFilterDef> filterDefinitions) {
public void collectFilterDefinitions(List<JaxbFilterDefImpl> filterDefinitions) {
if ( CollectionHelper.isEmpty( filterDefinitions ) ) {
return;
}
Expand All @@ -436,14 +436,14 @@ public void collectFilterDefinitions(List<JaxbFilterDef> filterDefinitions) {
) );
}

private Map<String, ClassDetails> extractFilterParameters(JaxbFilterDef source) {
final List<JaxbFilterDef.JaxbFilterParam> parameters = source.getFilterParam();
private Map<String, ClassDetails> extractFilterParameters(JaxbFilterDefImpl source) {
final List<JaxbFilterDefImpl.JaxbFilterParamImpl> parameters = source.getFilterParam();

// todo : update the mapping.xsd to account for new @ParamDef definition
// todo : handle simplified type names for XML, e.g. "String" instead of "java.lang.String"

final Map<String, ClassDetails> result = new HashMap<>( parameters.size() );
for ( JaxbFilterDef.JaxbFilterParam parameter : parameters ) {
for ( JaxbFilterDefImpl.JaxbFilterParamImpl parameter : parameters ) {
result.put( parameter.getName(), classDetailsRegistry.resolveClassDetails( parameter.getType() ) );
}
return result;
Expand All @@ -463,7 +463,7 @@ public void collectFilterDefinition(String name, String defaultCondition, Map<St
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// EntityListenerRegistration

public void collectEntityListenerRegistrations(List<JaxbEntityListener> listeners) {
public void collectEntityListenerRegistrations(List<JaxbEntityListenerImpl> listeners) {
if ( CollectionHelper.isEmpty( listeners ) ) {
return;
}
Expand All @@ -482,7 +482,7 @@ public void collectEntityListenerRegistrations(List<JaxbEntityListener> listener
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Id generators

public void collectIdGenerators(JaxbEntityMappings jaxbRoot) {
public void collectIdGenerators(JaxbEntityMappingsImpl jaxbRoot) {
collectSequenceGenerators( jaxbRoot.getSequenceGenerators() );
collectTableGenerators( jaxbRoot.getTableGenerators() );
collectGenericGenerators( jaxbRoot.getGenericGenerators() );
Expand All @@ -500,7 +500,7 @@ public void collectIdGenerators(ClassDetails classDetails) {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Sequence generator

public void collectSequenceGenerators(List<JaxbSequenceGenerator> sequenceGenerators) {
public void collectSequenceGenerators(List<JaxbSequenceGeneratorImpl> sequenceGenerators) {
if ( CollectionHelper.isEmpty( sequenceGenerators ) ) {
return;
}
Expand Down Expand Up @@ -534,7 +534,7 @@ public void collectSequenceGenerator(SequenceGeneratorRegistration generatorRegi
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Table generator

public void collectTableGenerators(List<JaxbTableGenerator> tableGenerators) {
public void collectTableGenerators(List<JaxbTableGeneratorImpl> tableGenerators) {
if ( CollectionHelper.isEmpty( tableGenerators ) ) {
return;
}
Expand Down Expand Up @@ -571,7 +571,7 @@ public void collectTableGenerator(TableGeneratorRegistration generatorRegistrati
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Generic generators

private void collectGenericGenerators(List<JaxbGenericIdGenerator> genericGenerators) {
private void collectGenericGenerators(List<JaxbGenericIdGeneratorImpl> genericGenerators) {
if ( CollectionHelper.isEmpty( genericGenerators ) ) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import java.util.Map;
import java.util.Set;

import org.hibernate.boot.jaxb.mapping.JaxbEntityListeners;
import org.hibernate.boot.jaxb.mapping.JaxbEntityMappings;
import org.hibernate.boot.jaxb.mapping.JaxbPersistenceUnitDefaults;
import org.hibernate.boot.jaxb.mapping.JaxbPersistenceUnitMetadata;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListenersImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappingsImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbPersistenceUnitDefaultsImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbPersistenceUnitMetadataImpl;
import org.hibernate.models.orm.spi.ProcessResult;
import org.hibernate.models.orm.spi.EntityHierarchy;
import org.hibernate.models.source.spi.ClassDetails;
Expand Down Expand Up @@ -42,7 +42,7 @@ public GlobalRegistrationsImpl getGlobalRegistrations() {
}


public void apply(JaxbEntityMappings jaxbRoot) {
public void apply(JaxbEntityMappingsImpl jaxbRoot) {
getGlobalRegistrations().collectJavaTypeRegistrations( jaxbRoot.getJavaTypeRegistrations() );
getGlobalRegistrations().collectJdbcTypeRegistrations( jaxbRoot.getJdbcTypeRegistrations() );
getGlobalRegistrations().collectConverterRegistrations( jaxbRoot.getConverterRegistrations() );
Expand All @@ -52,10 +52,10 @@ public void apply(JaxbEntityMappings jaxbRoot) {
getGlobalRegistrations().collectEmbeddableInstantiatorRegistrations( jaxbRoot.getEmbeddableInstantiatorRegistrations() );
getGlobalRegistrations().collectFilterDefinitions( jaxbRoot.getFilterDefinitions() );

final JaxbPersistenceUnitMetadata persistenceUnitMetadata = jaxbRoot.getPersistenceUnitMetadata();
final JaxbPersistenceUnitMetadataImpl persistenceUnitMetadata = jaxbRoot.getPersistenceUnitMetadata();
if ( persistenceUnitMetadata != null ) {
final JaxbPersistenceUnitDefaults persistenceUnitDefaults = persistenceUnitMetadata.getPersistenceUnitDefaults();
final JaxbEntityListeners entityListeners = persistenceUnitDefaults.getEntityListeners();
final JaxbPersistenceUnitDefaultsImpl persistenceUnitDefaults = persistenceUnitMetadata.getPersistenceUnitDefaults();
final JaxbEntityListenersImpl entityListeners = persistenceUnitDefaults.getEntityListeners();
if ( entityListeners != null ) {
getGlobalRegistrations().collectEntityListenerRegistrations( entityListeners.getEntityListener() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
package org.hibernate.models.orm.spi;

import org.hibernate.boot.jaxb.mapping.JaxbEntityListener;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListenerImpl;
import org.hibernate.internal.util.MutableObject;
import org.hibernate.models.ModelsException;
import org.hibernate.models.source.spi.ClassDetails;
Expand Down Expand Up @@ -96,7 +96,7 @@ public MethodDetails getPostUpdateMethod() {
public MethodDetails getPostLoadMethod() {
return postLoadMethod;
}
public static EntityListenerRegistration from(JaxbEntityListener jaxbMapping, ClassDetailsRegistry classDetailsRegistry) {
public static EntityListenerRegistration from(JaxbEntityListenerImpl jaxbMapping, ClassDetailsRegistry classDetailsRegistry) {
final ClassDetails listenerClassDetails = classDetailsRegistry.resolveClassDetails( jaxbMapping.getClazz() );
final MutableObject<MethodDetails> prePersistMethod = new MutableObject<>();
final MutableObject<MethodDetails> postPersistMethod = new MutableObject<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import java.util.Set;

import org.hibernate.boot.internal.ClassmateContext;
import org.hibernate.boot.jaxb.mapping.JaxbEmbeddable;
import org.hibernate.boot.jaxb.mapping.JaxbEntity;
import org.hibernate.boot.jaxb.mapping.JaxbEntityMappings;
import org.hibernate.boot.jaxb.mapping.JaxbMappedSuperclass;
import org.hibernate.boot.jaxb.mapping.ManagedType;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEmbeddableImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappingsImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbManagedType;
import org.hibernate.boot.jaxb.mapping.spi.JaxbMappedSuperclassImpl;
import org.hibernate.models.internal.CollectionHelper;
import org.hibernate.models.internal.StringHelper;
import org.hibernate.models.orm.JpaAnnotations;
Expand Down Expand Up @@ -78,16 +78,16 @@ public static ProcessResult process(
return process( managedResources, explicitlyListedClasses, options, sourceModelBuildingContext, ormModelBuildingContext );
}

public static class OverrideTuple<M extends ManagedType> {
private final JaxbEntityMappings jaxbRoot;
public static class OverrideTuple<M extends JaxbManagedType> {
private final JaxbEntityMappingsImpl jaxbRoot;
private final M managedType;

public OverrideTuple(JaxbEntityMappings jaxbRoot, M managedType) {
public OverrideTuple(JaxbEntityMappingsImpl jaxbRoot, M managedType) {
this.jaxbRoot = jaxbRoot;
this.managedType = managedType;
}

public JaxbEntityMappings getJaxbRoot() {
public JaxbEntityMappingsImpl getJaxbRoot() {
return jaxbRoot;
}

Expand Down Expand Up @@ -116,9 +116,9 @@ public static ProcessResult process(
final XmlResources collectedXmlResources = XmlResources.collectXmlResources( managedResources, sourceModelBuildingContext );
final boolean xmlMappingsGloballyComplete = collectedXmlResources.getPersistenceUnitMetadata().areXmlMappingsComplete();

final List<OverrideTuple<JaxbEntity>> entityOverrides = new ArrayList<>();
final List<OverrideTuple<JaxbMappedSuperclass>> mappedSuperclassesOverrides = new ArrayList<>();
final List<OverrideTuple<JaxbEmbeddable>> embeddableOverrides = new ArrayList<>();
final List<OverrideTuple<JaxbEntityImpl>> entityOverrides = new ArrayList<>();
final List<OverrideTuple<JaxbMappedSuperclassImpl>> mappedSuperclassesOverrides = new ArrayList<>();
final List<OverrideTuple<JaxbEmbeddableImpl>> embeddableOverrides = new ArrayList<>();

collectedXmlResources.getDocuments().forEach( (jaxbRoot) -> {
processResultCollector.apply( jaxbRoot );
Expand Down
Loading

0 comments on commit 5ba6a8a

Please sign in to comment.