Skip to content

Commit

Permalink
Preliminary support for complete XML entity mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed Oct 19, 2023
1 parent a859344 commit 731bfd4
Show file tree
Hide file tree
Showing 46 changed files with 1,213 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,26 @@ public static String nullIfEmpty(String value) {
public static String classNameToResourceName(String className) {
return className.replace( '.', '/' ) + ".class";
}

public static boolean isQualified(String name) {
int loc = name.lastIndexOf( '.' );
return loc > 0;
}

public static String qualify(String name, String qualifier) {
assert isNotEmpty( name );
return isEmpty( qualifier )
? name
: qualifier + "." + name;
}

public static String unqualify(String qualifiedName) {
int loc = qualifiedName.lastIndexOf( '.' );
return ( loc < 0 ) ? qualifiedName : qualifiedName.substring( loc + 1 );
}

public static String qualifier(String qualifiedName) {
int loc = qualifiedName.lastIndexOf( '.' );
return ( loc < 0 ) ? "" : qualifiedName.substring( 0, loc );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public AbstractIdentifiableTypeMetadata(
}

private AccessType determineAccessType(AccessType defaultAccessType) {
final AnnotationUsage<Access> annotation = getClassDetails().getUsage( JpaAnnotations.ACCESS );
final AnnotationUsage<Access> annotation = getClassDetails().getAnnotationUsage( JpaAnnotations.ACCESS );
if ( annotation != null ) {
return annotation.getAttributeValue( "value" );
}
Expand Down Expand Up @@ -162,11 +162,11 @@ protected void addSubclass(IdentifiableTypeMetadata subclass) {
}

protected boolean isMappedSuperclass(ClassDetails classDetails) {
return classDetails.getUsage( JpaAnnotations.MAPPED_SUPERCLASS ) != null;
return classDetails.getAnnotationUsage( JpaAnnotations.MAPPED_SUPERCLASS ) != null;
}

protected boolean isEntity(ClassDetails classDetails) {
return classDetails.getUsage( JpaAnnotations.ENTITY ) != null;
return classDetails.getAnnotationUsage( JpaAnnotations.ENTITY ) != null;
}

private void walkSubclasses(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ public Collection<AttributeMetadata> getAttributes() {
return attributeList();
}

@Override
public AttributeMetadata findAttribute(String name) {
final List<AttributeMetadata> attributeList = attributeList();
for ( int i = 0; i < attributeList.size(); i++ ) {
final AttributeMetadata attribute = attributeList.get( i );
if ( attribute.getName().equals( name ) ) {
return attribute;
}
}
return null;
}

@Override
public void forEachAttribute(IndexedConsumer<AttributeMetadata> consumer) {
for ( int i = 0; i < attributeList().size(); i++ ) {
Expand Down Expand Up @@ -167,24 +179,24 @@ private AttributeMetadata.AttributeNature determineAttributeNature(MemberDetails
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// first, look for explicit nature annotations

final AnnotationUsage<Any> any = backingMember.getUsage( HibernateAnnotations.ANY );
final AnnotationUsage<Basic> basic = backingMember.getUsage( JpaAnnotations.BASIC );
final AnnotationUsage<ElementCollection> elementCollection = backingMember.getUsage( JpaAnnotations.ELEMENT_COLLECTION );
final AnnotationUsage<Embedded> embedded = backingMember.getUsage( JpaAnnotations.EMBEDDED );
final AnnotationUsage<EmbeddedId> embeddedId = backingMember.getUsage( JpaAnnotations.EMBEDDED_ID );
final AnnotationUsage<ManyToAny> manyToAny = backingMember.getUsage( HibernateAnnotations.MANY_TO_ANY );
final AnnotationUsage<ManyToMany> manyToMany = backingMember.getUsage( JpaAnnotations.MANY_TO_MANY );
final AnnotationUsage<ManyToOne> manyToOne = backingMember.getUsage( JpaAnnotations.MANY_TO_ONE );
final AnnotationUsage<OneToMany> oneToMany = backingMember.getUsage( JpaAnnotations.ONE_TO_MANY );
final AnnotationUsage<OneToOne> oneToOne = backingMember.getUsage( JpaAnnotations.ONE_TO_ONE );
final AnnotationUsage<Any> any = backingMember.getAnnotationUsage( HibernateAnnotations.ANY );
final AnnotationUsage<Basic> basic = backingMember.getAnnotationUsage( JpaAnnotations.BASIC );
final AnnotationUsage<ElementCollection> elementCollection = backingMember.getAnnotationUsage( JpaAnnotations.ELEMENT_COLLECTION );
final AnnotationUsage<Embedded> embedded = backingMember.getAnnotationUsage( JpaAnnotations.EMBEDDED );
final AnnotationUsage<EmbeddedId> embeddedId = backingMember.getAnnotationUsage( JpaAnnotations.EMBEDDED_ID );
final AnnotationUsage<ManyToAny> manyToAny = backingMember.getAnnotationUsage( HibernateAnnotations.MANY_TO_ANY );
final AnnotationUsage<ManyToMany> manyToMany = backingMember.getAnnotationUsage( JpaAnnotations.MANY_TO_MANY );
final AnnotationUsage<ManyToOne> manyToOne = backingMember.getAnnotationUsage( JpaAnnotations.MANY_TO_ONE );
final AnnotationUsage<OneToMany> oneToMany = backingMember.getAnnotationUsage( JpaAnnotations.ONE_TO_MANY );
final AnnotationUsage<OneToOne> oneToOne = backingMember.getAnnotationUsage( JpaAnnotations.ONE_TO_ONE );

if ( basic != null ) {
natures.add( AttributeMetadata.AttributeNature.BASIC );
}

if ( embedded != null
|| embeddedId != null
|| ( backingMember.getType() != null && backingMember.getType().getUsage( JpaAnnotations.EMBEDDABLE ) != null ) ) {
|| ( backingMember.getType() != null && backingMember.getType().getAnnotationUsage( JpaAnnotations.EMBEDDABLE ) != null ) ) {
natures.add( AttributeMetadata.AttributeNature.EMBEDDED );
}

Expand All @@ -211,37 +223,37 @@ private AttributeMetadata.AttributeNature determineAttributeNature(MemberDetails

if ( !plural ) {
// first implicit basic nature
if ( backingMember.getUsage( JpaAnnotations.TEMPORAL ) != null
|| backingMember.getUsage( JpaAnnotations.LOB ) != null
|| backingMember.getUsage( JpaAnnotations.ENUMERATED ) != null
|| backingMember.getUsage( JpaAnnotations.CONVERT ) != null
|| backingMember.getUsage( JpaAnnotations.VERSION ) != null
|| backingMember.getUsage( HibernateAnnotations.GENERATED ) != null
|| backingMember.getUsage( HibernateAnnotations.NATIONALIZED ) != null
|| backingMember.getUsage( HibernateAnnotations.TZ_COLUMN ) != null
|| backingMember.getUsage( HibernateAnnotations.TZ_STORAGE ) != null
|| backingMember.getUsage( HibernateAnnotations.TYPE ) != null
|| backingMember.getUsage( HibernateAnnotations.TENANT_ID ) != null
|| backingMember.getUsage( HibernateAnnotations.JAVA_TYPE ) != null
|| backingMember.getUsage( HibernateAnnotations.JDBC_TYPE_CODE ) != null
|| backingMember.getUsage( HibernateAnnotations.JDBC_TYPE ) != null ) {
if ( backingMember.getAnnotationUsage( JpaAnnotations.TEMPORAL ) != null
|| backingMember.getAnnotationUsage( JpaAnnotations.LOB ) != null
|| backingMember.getAnnotationUsage( JpaAnnotations.ENUMERATED ) != null
|| backingMember.getAnnotationUsage( JpaAnnotations.CONVERT ) != null
|| backingMember.getAnnotationUsage( JpaAnnotations.VERSION ) != null
|| backingMember.getAnnotationUsage( HibernateAnnotations.GENERATED ) != null
|| backingMember.getAnnotationUsage( HibernateAnnotations.NATIONALIZED ) != null
|| backingMember.getAnnotationUsage( HibernateAnnotations.TZ_COLUMN ) != null
|| backingMember.getAnnotationUsage( HibernateAnnotations.TZ_STORAGE ) != null
|| backingMember.getAnnotationUsage( HibernateAnnotations.TYPE ) != null
|| backingMember.getAnnotationUsage( HibernateAnnotations.TENANT_ID ) != null
|| backingMember.getAnnotationUsage( HibernateAnnotations.JAVA_TYPE ) != null
|| backingMember.getAnnotationUsage( HibernateAnnotations.JDBC_TYPE_CODE ) != null
|| backingMember.getAnnotationUsage( HibernateAnnotations.JDBC_TYPE ) != null ) {
natures.add( AttributeMetadata.AttributeNature.BASIC );
}

// then embedded
if ( backingMember.getUsage( HibernateAnnotations.EMBEDDABLE_INSTANTIATOR ) != null
|| backingMember.getUsage( HibernateAnnotations.COMPOSITE_TYPE ) != null ) {
if ( backingMember.getAnnotationUsage( HibernateAnnotations.EMBEDDABLE_INSTANTIATOR ) != null
|| backingMember.getAnnotationUsage( HibernateAnnotations.COMPOSITE_TYPE ) != null ) {
natures.add( AttributeMetadata.AttributeNature.EMBEDDED );
}

// and any
if ( backingMember.getUsage( HibernateAnnotations.ANY_DISCRIMINATOR ) != null
|| backingMember.getUsage( HibernateAnnotations.ANY_DISCRIMINATOR_VALUE ) != null
|| backingMember.getUsage( HibernateAnnotations.ANY_DISCRIMINATOR_VALUES ) != null
|| backingMember.getUsage( HibernateAnnotations.ANY_KEY_JAVA_TYPE ) != null
|| backingMember.getUsage( HibernateAnnotations.ANY_KEY_JAVA_CLASS ) != null
|| backingMember.getUsage( HibernateAnnotations.ANY_KEY_JDBC_TYPE ) != null
|| backingMember.getUsage( HibernateAnnotations.ANY_KEY_JDBC_TYPE_CODE ) != null ) {
if ( backingMember.getAnnotationUsage( HibernateAnnotations.ANY_DISCRIMINATOR ) != null
|| backingMember.getAnnotationUsage( HibernateAnnotations.ANY_DISCRIMINATOR_VALUE ) != null
|| backingMember.getAnnotationUsage( HibernateAnnotations.ANY_DISCRIMINATOR_VALUES ) != null
|| backingMember.getAnnotationUsage( HibernateAnnotations.ANY_KEY_JAVA_TYPE ) != null
|| backingMember.getAnnotationUsage( HibernateAnnotations.ANY_KEY_JAVA_CLASS ) != null
|| backingMember.getAnnotationUsage( HibernateAnnotations.ANY_KEY_JDBC_TYPE ) != null
|| backingMember.getAnnotationUsage( HibernateAnnotations.ANY_KEY_JDBC_TYPE_CODE ) != null ) {
natures.add( AttributeMetadata.AttributeNature.ANY );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ protected void collectMembersMarkedTransient(
final List<FieldDetails> fields = classDetails.getFields();
for ( int i = 0; i < fields.size(); i++ ) {
final FieldDetails fieldDetails = fields.get( i );
if ( fieldDetails.getUsage( JpaAnnotations.TRANSIENT ) != null ) {
if ( fieldDetails.getAnnotationUsage( JpaAnnotations.TRANSIENT ) != null ) {
transientFieldConsumer.accept( fieldDetails );
}
}

final List<MethodDetails> methods = classDetails.getMethods();
for ( int i = 0; i < methods.size(); i++ ) {
final MethodDetails methodDetails = methods.get( i );
if ( methodDetails.getUsage( JpaAnnotations.TRANSIENT ) != null ) {
if ( methodDetails.getAnnotationUsage( JpaAnnotations.TRANSIENT ) != null ) {
transientMethodConsumer.accept( methodDetails );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import jakarta.persistence.Access;
import jakarta.persistence.AccessType;

import static org.hibernate.models.source.internal.SourceModelLogging.SOURCE_MODEL_LOGGER;

/**
* Builds {@link EntityHierarchy} references from
* {@linkplain ClassDetailsRegistry#forEachClassDetails managed classes}.
Expand Down Expand Up @@ -102,7 +100,7 @@ private AccessType determineDefaultAccessTypeForHierarchy(ClassDetails rootEntit
ClassDetails current = rootEntityType;
while ( current != null ) {
// look for `@Access` on the class
final AnnotationUsage<Access> accessAnnotation = current.getUsage( JpaAnnotations.ACCESS );
final AnnotationUsage<Access> accessAnnotation = current.getAnnotationUsage( JpaAnnotations.ACCESS );
if ( accessAnnotation != null ) {
return accessAnnotation.getAttributeValue( "value" );
}
Expand Down Expand Up @@ -137,17 +135,17 @@ private AnnotationTarget determineIdMember(ClassDetails current) {
final List<MethodDetails> methods = current.getMethods();
for ( int i = 0; i < methods.size(); i++ ) {
final MethodDetails methodDetails = methods.get( i );
if ( methodDetails.getUsage( JpaAnnotations.ID ) != null
|| methodDetails.getUsage( JpaAnnotations.EMBEDDED_ID ) != null ) {
if ( methodDetails.getAnnotationUsage( JpaAnnotations.ID ) != null
|| methodDetails.getAnnotationUsage( JpaAnnotations.EMBEDDED_ID ) != null ) {
return methodDetails;
}
}

final List<FieldDetails> fields = current.getFields();
for ( int i = 0; i < fields.size(); i++ ) {
final FieldDetails fieldDetails = fields.get( i );
if ( fieldDetails.getUsage( JpaAnnotations.ID ) != null
|| fieldDetails.getUsage( JpaAnnotations.EMBEDDED_ID ) != null ) {
if ( fieldDetails.getAnnotationUsage( JpaAnnotations.ID ) != null
|| fieldDetails.getAnnotationUsage( JpaAnnotations.EMBEDDED_ID ) != null ) {
return fieldDetails;
}
}
Expand All @@ -163,7 +161,7 @@ private static Set<ClassDetails> collectRootEntityTypes(ClassDetailsRegistry cla
final Set<ClassDetails> collectedTypes = new HashSet<>();

classDetailsRegistry.forEachClassDetails( (managedType) -> {
if ( managedType.getUsage( JpaAnnotations.ENTITY ) != null
if ( managedType.getAnnotationUsage( JpaAnnotations.ENTITY ) != null
&& isRoot( managedType ) ) {
collectedTypes.add( managedType );
}
Expand All @@ -185,7 +183,7 @@ public static boolean isRoot(ClassDetails classInfo) {

ClassDetails current = classInfo.getSuperType();
while ( current != null ) {
if ( current.getUsage( JpaAnnotations.ENTITY ) != null ) {
if ( current.getAnnotationUsage( JpaAnnotations.ENTITY ) != null ) {
// a super type has `@Entity`, cannot be root
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private InheritanceType determineInheritanceType(EntityTypeMetadata root) {
* @apiNote Used when building the {@link EntityHierarchy}
*/
private static InheritanceType getLocallyDefinedInheritanceType(ClassDetails managedClass) {
final AnnotationUsage<Inheritance> localAnnotation = managedClass.getUsage( JpaAnnotations.INHERITANCE );
final AnnotationUsage<Inheritance> localAnnotation = managedClass.getAnnotationUsage( JpaAnnotations.INHERITANCE );
if ( localAnnotation == null ) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public EntityTypeMetadataImpl(
// all of which `ClassDetails#getName` already handles for us
this.entityName = getClassDetails().getName();

final AnnotationUsage<Entity> entityAnnotation = classDetails.getUsage( JpaAnnotations.ENTITY );
final AnnotationUsage<Entity> entityAnnotation = classDetails.getAnnotationUsage( JpaAnnotations.ENTITY );
this.jpaEntityName = determineJpaEntityName( entityAnnotation, entityName );

// this.synchronizedTableNames = determineSynchronizedTableNames();
Expand Down Expand Up @@ -144,7 +144,7 @@ public EntityTypeMetadataImpl(
// all of which `ClassDetails#getName` already handles for us
this.entityName = getClassDetails().getName();

final AnnotationUsage<Entity> entityAnnotation = classDetails.getUsage( JpaAnnotations.ENTITY );
final AnnotationUsage<Entity> entityAnnotation = classDetails.getAnnotationUsage( JpaAnnotations.ENTITY );
this.jpaEntityName = determineJpaEntityName( entityAnnotation, entityName );

this.attributeList = resolveAttributes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public List<EmbeddableInstantiatorRegistration> getEmbeddableInstantiatorRegistr
// JavaTypeRegistration

public void collectJavaTypeRegistrations(AnnotationTarget annotationTarget) {
annotationTarget.forEachUsage( JAVA_TYPE_REG, (usage) -> collectJavaTypeRegistration(
annotationTarget.forEachAnnotationUsage( JAVA_TYPE_REG, (usage) -> collectJavaTypeRegistration(
usage.getAttributeValue( "javaType" ),
usage.getAttributeValue( "descriptorClass" )
) );
Expand Down Expand Up @@ -155,7 +155,7 @@ public void collectJavaTypeRegistration(ClassDetails javaType, ClassDetails des
// JdbcTypeRegistration

public void collectJdbcTypeRegistrations(AnnotationTarget annotationTarget) {
annotationTarget.forEachUsage( JDBC_TYPE_REG, (usage) -> collectJdbcTypeRegistration(
annotationTarget.forEachAnnotationUsage( JDBC_TYPE_REG, (usage) -> collectJdbcTypeRegistration(
usage.getAttributeValue( "registrationCode" ),
usage.getAttributeValue( "value" )
) );
Expand Down Expand Up @@ -184,7 +184,7 @@ public void collectJdbcTypeRegistration(Integer registrationCode, ClassDetails d
// ConversionRegistration

public void collectConverterRegistrations(AnnotationTarget annotationTarget) {
annotationTarget.forEachUsage( CONVERTER_REG, (usage) -> {
annotationTarget.forEachAnnotationUsage( CONVERTER_REG, (usage) -> {
final ClassDetails domainType = usage.getAttributeValue( "domainType" );
final ClassDetails converterType = usage.getAttributeValue( "converter" );
final boolean autoApply = usage.getAttributeValue( "autoApply" );
Expand Down Expand Up @@ -224,7 +224,7 @@ public void collectConverterRegistration(ConversionRegistration conversion) {
// UserTypeRegistration

public void collectUserTypeRegistrations(AnnotationTarget annotationTarget) {
annotationTarget.forEachUsage( TYPE_REG, (usage) -> collectUserTypeRegistration(
annotationTarget.forEachAnnotationUsage( TYPE_REG, (usage) -> collectUserTypeRegistration(
usage.getAttributeValue( "basicClass" ),
usage.getAttributeValue( "userType" )
) );
Expand Down Expand Up @@ -254,7 +254,7 @@ public void collectUserTypeRegistration(ClassDetails domainClass, ClassDetails u
// CompositeUserTypeRegistration

public void collectCompositeUserTypeRegistrations(AnnotationTarget annotationTarget) {
annotationTarget.forEachUsage( COMPOSITE_TYPE_REG, (usage) -> collectCompositeUserTypeRegistration(
annotationTarget.forEachAnnotationUsage( COMPOSITE_TYPE_REG, (usage) -> collectCompositeUserTypeRegistration(
usage.getAttributeValue( "embeddableClass" ),
usage.getAttributeValue( "userType" )
) );
Expand Down Expand Up @@ -283,7 +283,7 @@ public void collectCompositeUserTypeRegistration(ClassDetails domainClass, Class
// CollectionTypeRegistration

public void collectCollectionTypeRegistrations(AnnotationTarget annotationTarget) {
annotationTarget.forEachUsage( COLLECTION_TYPE_REG, (usage) -> collectCollectionTypeRegistration(
annotationTarget.forEachAnnotationUsage( COLLECTION_TYPE_REG, (usage) -> collectCollectionTypeRegistration(
usage.getAttributeValue( "classification" ),
usage.getAttributeValue( "type" ),
extractParameterMap( usage )
Expand Down Expand Up @@ -340,7 +340,7 @@ public void collectCollectionTypeRegistration(
// EmbeddableInstantiatorRegistration

public void collectEmbeddableInstantiatorRegistrations(AnnotationTarget annotationTarget) {
annotationTarget.forEachUsage( EMBEDDABLE_INSTANTIATOR_REG, (usage) -> collectEmbeddableInstantiatorRegistration(
annotationTarget.forEachAnnotationUsage( EMBEDDABLE_INSTANTIATOR_REG, (usage) -> collectEmbeddableInstantiatorRegistration(
usage.getAttributeValue( "embeddableClass" ),
usage.getAttributeValue( "instantiator" )
) );
Expand Down Expand Up @@ -396,9 +396,9 @@ public void collectIdGenerators(JaxbEntityMappings jaxbRoot) {
}

public void collectIdGenerators(ClassDetails classDetails) {
classDetails.forEachUsage( SequenceGenerator.class, this::collectSequenceGenerator );
classDetails.forEachUsage( TableGenerator.class, this::collectTableGenerator );
classDetails.forEachUsage( GenericGenerator.class, this::collectGenericGenerator );
classDetails.forEachAnnotationUsage( SequenceGenerator.class, this::collectSequenceGenerator );
classDetails.forEachAnnotationUsage( TableGenerator.class, this::collectTableGenerator );
classDetails.forEachAnnotationUsage( GenericGenerator.class, this::collectGenericGenerator );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private <M extends MemberDetails> void processAttributeLevelAccessMember(
Function<M,Boolean> transiencyChecker,
ClassDetails classDetails,
OrmModelBuildingContext processingContext) {
final AnnotationUsage<Access> access = memberDetails.getUsage( JpaAnnotations.ACCESS );
final AnnotationUsage<Access> access = memberDetails.getAnnotationUsage( JpaAnnotations.ACCESS );
if ( access == null ) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public interface ManagedTypeMetadata {
*/
Collection<AttributeMetadata> getAttributes();

AttributeMetadata findAttribute(String name);

/**
* Visit each declared attributes
*/
Expand Down
Loading

0 comments on commit 731bfd4

Please sign in to comment.