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
  • Loading branch information
sebersole committed Oct 24, 2023
1 parent 6d478f7 commit e2908e3
Show file tree
Hide file tree
Showing 98 changed files with 7,679 additions and 205 deletions.
3 changes: 2 additions & 1 deletion hibernate-models-orm/hibernate-models-orm.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependencies {
implementation project( ":hibernate-models-common" )
implementation project( ":hibernate-models-source" )

implementation project( ":hibernate-orm" )
implementation libs.hibernateCore
implementation libs.jandex
implementation libs.byteBuddy
Expand All @@ -20,6 +21,6 @@ dependencies {

testImplementation project( ":hibernate-models-testing" )
// testImplementation libs.hibernateTesting
testImplementation testLibs.jpa
testImplementation jakartaLibs.jpa
testRuntimeOnly testLibs.log4j
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import java.util.function.Consumer;

import org.hibernate.annotations.*;
import org.hibernate.annotations.internal.Extends;
import org.hibernate.boot.internal.Abstract;
import org.hibernate.boot.internal.CollectionClassification;
import org.hibernate.boot.internal.Extends;
import org.hibernate.boot.internal.Target;
import org.hibernate.models.orm.internal.OrmAnnotationHelper;
import org.hibernate.models.source.spi.AnnotationDescriptor;

Expand Down Expand Up @@ -68,7 +71,6 @@ 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 Expand Up @@ -156,7 +158,6 @@ public interface HibernateAnnotations {
AnnotationDescriptor<Synchronize> SYNCHRONIZE = createOrmDescriptor( Synchronize.class );
AnnotationDescriptor<Tables> TABLES = createOrmDescriptor( Tables.class );
AnnotationDescriptor<Table> TABLE = createOrmDescriptor( Table.class, TABLES );
AnnotationDescriptor<Target> TARGET = createOrmDescriptor( Target.class );
AnnotationDescriptor<TenantId> TENANT_ID = createOrmDescriptor( TenantId.class );
AnnotationDescriptor<TimeZoneColumn> TZ_COLUMN = createOrmDescriptor( TimeZoneColumn.class );
AnnotationDescriptor<TimeZoneStorage> TZ_STORAGE = createOrmDescriptor( TimeZoneStorage.class );
Expand All @@ -170,6 +171,10 @@ public interface HibernateAnnotations {
AnnotationDescriptor<Where> WHERE = createOrmDescriptor( Where.class );
AnnotationDescriptor<WhereJoinTable> WHERE_JOIN_TABLE = createOrmDescriptor( WhereJoinTable.class );

AnnotationDescriptor<Abstract> ABSTRACT = createOrmDescriptor( Abstract.class );
AnnotationDescriptor<CollectionClassification> COLLECTION_CLASSIFICATION = createOrmDescriptor( CollectionClassification.class );
AnnotationDescriptor<Extends> EXTENDS = createOrmDescriptor( Extends.class );
AnnotationDescriptor<Target> TARGET = createOrmDescriptor( Target.class );

AnnotationDescriptor<DialectOverride.Checks> DIALECT_OVERRIDE_CHECKS = createOrmDescriptor( DialectOverride.Checks.class );
AnnotationDescriptor<DialectOverride.Check> DIALECT_OVERRIDE_CHECK = createOrmDescriptor( DialectOverride.Check.class, DIALECT_OVERRIDE_CHECKS );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,6 @@ public static void processNaturalId(
processBaseAttributes( jaxbNaturalId, mutableClassDetails, classAccessType, memberAdjuster, sourceModelBuildingContext );
}

public static void processBaseAttributes(
JaxbBaseAttributesContainer attributesContainer,
MutableClassDetails mutableClassDetails,
AccessType classAccessType,
SourceModelBuildingContext sourceModelBuildingContext) {
processBaseAttributes(
attributesContainer,
mutableClassDetails,
classAccessType,
null,
sourceModelBuildingContext
);
}

public static void processBaseAttributes(
JaxbBaseAttributesContainer attributesContainer,
MutableClassDetails mutableClassDetails,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
*/
package org.hibernate.models.orm.xml.internal;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.annotations.internal.Extends;
import org.hibernate.boot.internal.Abstract;
import org.hibernate.boot.internal.Extends;
import org.hibernate.boot.internal.LimitedCollectionClassification;
import org.hibernate.boot.jaxb.mapping.spi.JaxbAnyMappingImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbAttributesContainer;
import org.hibernate.boot.jaxb.mapping.spi.JaxbAttributesContainerImpl;
Expand All @@ -23,6 +28,7 @@
import org.hibernate.boot.jaxb.mapping.spi.JaxbMappedSuperclassImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbOneToOneImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbPersistentAttribute;
import org.hibernate.boot.jaxb.mapping.spi.JaxbPluralAttribute;
import org.hibernate.models.ModelsException;
import org.hibernate.models.internal.CollectionHelper;
import org.hibernate.models.internal.StringHelper;
Expand Down Expand Up @@ -301,6 +307,7 @@ private static void prepareDynamicClass(
private static ClassDetails determineDynamicAttributeJavaType(
JaxbPersistentAttribute jaxbPersistentAttribute,
SourceModelBuildingContext sourceModelBuildingContext) {
final ClassDetailsRegistry classDetailsRegistry = sourceModelBuildingContext.getClassDetailsRegistry();

if ( jaxbPersistentAttribute instanceof JaxbIdImpl ) {
final JaxbIdImpl jaxbId = (JaxbIdImpl) jaxbPersistentAttribute;
Expand All @@ -309,11 +316,10 @@ private static ClassDetails determineDynamicAttributeJavaType(

if ( jaxbPersistentAttribute instanceof JaxbEmbeddedIdImpl ) {
final JaxbEmbeddedIdImpl jaxbEmbeddedId = (JaxbEmbeddedIdImpl) jaxbPersistentAttribute;
final String target = jaxbEmbeddedId.getEmbeddable();
final String target = jaxbEmbeddedId.getTarget();
if ( StringHelper.isEmpty( target ) ) {
return null;
}
final ClassDetailsRegistry classDetailsRegistry = sourceModelBuildingContext.getClassDetailsRegistry();
return classDetailsRegistry.resolveClassDetails(
target,
() -> new DynamicClassDetails( target, sourceModelBuildingContext )
Expand All @@ -331,7 +337,6 @@ private static ClassDetails determineDynamicAttributeJavaType(
if ( StringHelper.isEmpty( target ) ) {
return null;
}
final ClassDetailsRegistry classDetailsRegistry = sourceModelBuildingContext.getClassDetailsRegistry();
return classDetailsRegistry.resolveClassDetails(
target,
() -> new DynamicClassDetails( target, sourceModelBuildingContext )
Expand All @@ -344,7 +349,6 @@ private static ClassDetails determineDynamicAttributeJavaType(
if ( StringHelper.isEmpty( target ) ) {
return null;
}
final ClassDetailsRegistry classDetailsRegistry = sourceModelBuildingContext.getClassDetailsRegistry();
return classDetailsRegistry.resolveClassDetails(
target,
() -> new DynamicClassDetails(
Expand All @@ -358,9 +362,27 @@ private static ClassDetails determineDynamicAttributeJavaType(
}

if ( jaxbPersistentAttribute instanceof JaxbAnyMappingImpl ) {
return sourceModelBuildingContext.getClassDetailsRegistry().getClassDetails( Object.class.getName() );
return classDetailsRegistry.getClassDetails( Object.class.getName() );
}

if ( jaxbPersistentAttribute instanceof JaxbPluralAttribute ) {
final JaxbPluralAttribute jaxbPluralAttribute = (JaxbPluralAttribute) jaxbPersistentAttribute;
final LimitedCollectionClassification classification = jaxbPluralAttribute.getClassification();
switch ( classification ) {
case BAG: {
return classDetailsRegistry.resolveClassDetails( Collection.class.getName() );
}
case LIST: {
return classDetailsRegistry.resolveClassDetails( List.class.getName() );
}
case SET: {
return classDetailsRegistry.resolveClassDetails( Set.class.getName() );
}
case MAP: {
return classDetailsRegistry.resolveClassDetails( Map.class.getName() );
}
}
}
throw new UnsupportedOperationException( "Resolution of dynamic attribute Java type not yet implemented for " + jaxbPersistentAttribute );
}

Expand All @@ -375,23 +397,6 @@ private static void adjustDynamicTypeMember(
);
}

private static void processNonDynamicEntity(
MutableClassDetails classDetails,
JaxbEntityImpl jaxbEntity,
AccessType classAccessType,
PersistenceUnitMetadata persistenceUnitMetadata,
SourceModelBuildingContext sourceModelBuildingContext) {
processEntityMetadata(
classDetails,
jaxbEntity,
classAccessType,
ManagedTypeProcessor::adjustNonDynamicTypeMember,
persistenceUnitMetadata,
sourceModelBuildingContext
);

}

private static void processEntityMetadata(
MutableClassDetails classDetails,
JaxbEntityImpl jaxbEntity,
Expand All @@ -403,6 +408,10 @@ private static void processEntityMetadata(
XmlAnnotationHelper.applyInheritance( jaxbEntity, classDetails, sourceModelBuildingContext );
classDetails.addAnnotationUsage( XmlAnnotationHelper.createAccessAnnotation( classAccessType, classDetails ) );

if ( jaxbEntity.isAbstract() != null ) {
XmlProcessingHelper.makeAnnotation( Abstract.class, classDetails );
}

if ( StringHelper.isNotEmpty( jaxbEntity.getExtends() ) ) {
final DynamicAnnotationUsage<Extends> extendsAnn = XmlProcessingHelper.makeAnnotation(
Extends.class,
Expand Down Expand Up @@ -456,129 +465,6 @@ private static void adjustNonDynamicTypeMember(
);
}

private static void applyAttributesToDynamicClass(
MutableClassDetails dynamicClassDetails,
JaxbAttributesContainer attributes,
SourceModelBuildingContext sourceModelBuildingContext) {
attributes.getBasicAttributes().forEach( (jaxbBasic) -> {
final MapModeFieldDetails member = new MapModeFieldDetails(
jaxbBasic.getName(),
XmlAnnotationHelper.resolveJavaType( jaxbBasic.getType().getValue(), sourceModelBuildingContext ),
sourceModelBuildingContext
);
dynamicClassDetails.addField( member );
XmlAnnotationHelper.applyAttributeAccessor(
BuiltInPropertyAccessStrategies.MAP.getExternalName(),
member,
sourceModelBuildingContext
);
} );
attributes.getEmbeddedAttributes().forEach( (jaxbEmbedded) -> {
final MapModeFieldDetails member = new MapModeFieldDetails(
jaxbEmbedded.getName(),
null,
sourceModelBuildingContext
);
dynamicClassDetails.addField( member );
XmlAnnotationHelper.applyAttributeAccessor(
BuiltInPropertyAccessStrategies.MAP.getExternalName(),
member,
sourceModelBuildingContext
);
} );
attributes.getOneToOneAttributes().forEach( (jaxbOneToOne) -> {
final MapModeFieldDetails member = new MapModeFieldDetails(
jaxbOneToOne.getName(),
null,
sourceModelBuildingContext
);
dynamicClassDetails.addField( member );
XmlAnnotationHelper.applyAttributeAccessor(
BuiltInPropertyAccessStrategies.MAP.getExternalName(),
member,
sourceModelBuildingContext
);
} );
attributes.getManyToOneAttributes().forEach( (jaxbOneToOne) -> {
final MapModeFieldDetails member = new MapModeFieldDetails(
jaxbOneToOne.getName(),
null,
sourceModelBuildingContext
);
dynamicClassDetails.addField( member );
XmlAnnotationHelper.applyAttributeAccessor(
BuiltInPropertyAccessStrategies.MAP.getExternalName(),
member,
sourceModelBuildingContext
);
} );
attributes.getAnyMappingAttributes().forEach( (jaxbAny) -> {
final MapModeFieldDetails member = new MapModeFieldDetails(
jaxbAny.getName(),
null,
sourceModelBuildingContext
);
dynamicClassDetails.addField( member );
XmlAnnotationHelper.applyAttributeAccessor(
BuiltInPropertyAccessStrategies.MAP.getExternalName(),
member,
sourceModelBuildingContext
);
} );
attributes.getElementCollectionAttributes().forEach( (jaxbElementCollection) -> {
final MapModeFieldDetails member = new MapModeFieldDetails(
jaxbElementCollection.getName(),
null,
sourceModelBuildingContext
);
dynamicClassDetails.addField( member );
XmlAnnotationHelper.applyAttributeAccessor(
BuiltInPropertyAccessStrategies.MAP.getExternalName(),
member,
sourceModelBuildingContext
);
} );
attributes.getOneToManyAttributes().forEach( (jaxbOneToMany) -> {
final MapModeFieldDetails member = new MapModeFieldDetails(
jaxbOneToMany.getName(),
null,
sourceModelBuildingContext
);
dynamicClassDetails.addField( member );
XmlAnnotationHelper.applyAttributeAccessor(
BuiltInPropertyAccessStrategies.MAP.getExternalName(),
member,
sourceModelBuildingContext
);
} );
attributes.getManyToManyAttributes().forEach( (jaxbManyToMany) -> {
final MapModeFieldDetails member = new MapModeFieldDetails(
jaxbManyToMany.getName(),
null,
sourceModelBuildingContext
);
dynamicClassDetails.addField( member );
XmlAnnotationHelper.applyAttributeAccessor(
BuiltInPropertyAccessStrategies.MAP.getExternalName(),
member,
sourceModelBuildingContext
);
} );
attributes.getPluralAnyMappingAttributes().forEach( (jaxbManyToAny) -> {
final MapModeFieldDetails member = new MapModeFieldDetails(
jaxbManyToAny.getName(),
null,
sourceModelBuildingContext
);
dynamicClassDetails.addField( member );
XmlAnnotationHelper.applyAttributeAccessor(
BuiltInPropertyAccessStrategies.MAP.getExternalName(),
member,
sourceModelBuildingContext
);
} );

}
public static void processOverrideEntity(
List<Processor.OverrideTuple<JaxbEntityImpl>> entityOverrides,
PersistenceUnitMetadata persistenceUnitMetadata,
Expand Down Expand Up @@ -717,14 +603,17 @@ public static void processCompleteEmbeddable(
final AttributeProcessor.MemberAdjuster memberAdjuster;

if ( StringHelper.isEmpty( jaxbEmbeddable.getClazz() ) ) {
if ( StringHelper.isEmpty( jaxbEmbeddable.getName() ) ) {
throw new ModelsException( "Embeddable did not define class nor name" );
}
// no class == dynamic...
// classDetails = (MutableClassDetails) sourceModelBuildingContext
// .getClassDetailsRegistry()
// .resolveClassDetails( jaxbEmbeddable.getName() );
// classAccessType = AccessType.FIELD;
// memberAdjuster = ManagedTypeProcessor::adjustDynamicTypeMember;
//
// prepareDynamicClass( classDetails, jaxbEmbeddable, persistenceUnitMetadata, sourceModelBuildingContext );
classDetails = (MutableClassDetails) sourceModelBuildingContext
.getClassDetailsRegistry()
.resolveClassDetails( jaxbEmbeddable.getName() );
classAccessType = AccessType.FIELD;
memberAdjuster = ManagedTypeProcessor::adjustDynamicTypeMember;

prepareDynamicClass( classDetails, jaxbEmbeddable, persistenceUnitMetadata, sourceModelBuildingContext );

throw new UnsupportedOperationException( "Not yet implemented" );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import org.hibernate.annotations.NaturalIdCache;
import org.hibernate.annotations.OptimisticLock;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Target;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.UuidGenerator;
import org.hibernate.boot.internal.Target;
import org.hibernate.boot.jaxb.mapping.spi.JaxbAssociationOverrideImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbAttributeOverrideImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbBasicImpl;
Expand Down
Loading

0 comments on commit e2908e3

Please sign in to comment.