Skip to content

Commit

Permalink
#34 - Create AnnotationUsage for one-to-many attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
mbladel authored and sebersole committed Dec 4, 2023
1 parent dfb3363 commit b7e7b77
Show file tree
Hide file tree
Showing 10 changed files with 660 additions and 293 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
import org.hibernate.annotations.SortNatural;
import org.hibernate.boot.internal.CollectionClassification;
import org.hibernate.boot.internal.LimitedCollectionClassification;
import org.hibernate.boot.jaxb.mapping.spi.JaxbMapKeyImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbOrderColumnImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbPluralAttribute;
import org.hibernate.boot.models.categorize.xml.internal.XmlAnnotationHelper;
import org.hibernate.boot.models.categorize.xml.spi.XmlDocumentContext;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.models.internal.MutableAnnotationUsage;
import org.hibernate.models.internal.MutableMemberDetails;
import org.hibernate.models.spi.AnnotationDescriptor;
import org.hibernate.models.spi.ClassDetails;
import org.hibernate.models.spi.ClassDetailsRegistry;
import org.hibernate.models.spi.SourceModelBuildingContext;
Expand All @@ -34,8 +36,8 @@
import jakarta.persistence.OrderBy;
import jakarta.persistence.OrderColumn;

import static org.hibernate.boot.models.categorize.xml.internal.XmlAnnotationHelper.applyOr;
import static org.hibernate.boot.models.categorize.xml.internal.XmlProcessingHelper.getOrMakeAnnotation;
import static org.hibernate.boot.models.categorize.xml.internal.XmlProcessingHelper.setIf;

/**
* @author Marco Belladelli
Expand All @@ -44,93 +46,87 @@ public class CommonPluralAttributeProcessing {
public static void applyPluralAttributeStructure(
JaxbPluralAttribute jaxbPluralAttribute,
MutableMemberDetails memberDetails,
XmlDocumentContext documentContext) {
final SourceModelBuildingContext buildingContext = documentContext.getModelBuildingContext();
XmlDocumentContext xmlDocumentContext) {
final SourceModelBuildingContext buildingContext = xmlDocumentContext.getModelBuildingContext();
final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry();

if ( jaxbPluralAttribute.getFetchMode() != null ) {
final MutableAnnotationUsage<Fetch> fetchAnn = getOrMakeAnnotation( Fetch.class, memberDetails, documentContext );
final MutableAnnotationUsage<Fetch> fetchAnn = getOrMakeAnnotation( Fetch.class, memberDetails, xmlDocumentContext );
fetchAnn.setAttributeValue( "value", jaxbPluralAttribute.getFetchMode() );
}

if ( jaxbPluralAttribute.getClassification() != null ) {
final MutableAnnotationUsage<CollectionClassification> collectionClassificationAnn = getOrMakeAnnotation(
CollectionClassification.class,
memberDetails,
documentContext
xmlDocumentContext
);
setIf( jaxbPluralAttribute.getClassification(), "value", collectionClassificationAnn );
collectionClassificationAnn.setAttributeValue( "value", jaxbPluralAttribute.getClassification() );
if ( jaxbPluralAttribute.getClassification() == LimitedCollectionClassification.BAG ) {
getOrMakeAnnotation( Bag.class, memberDetails, documentContext );
getOrMakeAnnotation( Bag.class, memberDetails, xmlDocumentContext );
}
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// collection-structure

XmlAnnotationHelper.applyCollectionUserType( jaxbPluralAttribute.getCollectionType(), memberDetails, documentContext );
XmlAnnotationHelper.applyCollectionUserType( jaxbPluralAttribute.getCollectionType(), memberDetails, xmlDocumentContext );

XmlAnnotationHelper.applyCollectionId( jaxbPluralAttribute.getCollectionId(), memberDetails, documentContext );
XmlAnnotationHelper.applyCollectionId( jaxbPluralAttribute.getCollectionId(), memberDetails, xmlDocumentContext );

if ( StringHelper.isNotEmpty( jaxbPluralAttribute.getOrderBy() ) ) {
final MutableAnnotationUsage<OrderBy> orderByAnn = getOrMakeAnnotation(
OrderBy.class,
memberDetails,
documentContext
xmlDocumentContext
);
orderByAnn.setAttributeValue( "value", jaxbPluralAttribute.getOrderBy() );
}

final JaxbOrderColumnImpl orderColumn = jaxbPluralAttribute.getOrderColumn();
if ( orderColumn != null ) {
final MutableAnnotationUsage<OrderColumn> orderByAnn = getOrMakeAnnotation(
OrderColumn.class,
memberDetails,
documentContext
);
setIf( orderColumn.getName(), "name", orderByAnn );
setIf( orderColumn.isNullable(), "nullable", orderByAnn );
setIf( orderColumn.isInsertable(), "insertable", orderByAnn );
setIf( orderColumn.isUpdatable(), "updatable", orderByAnn );
setIf( orderColumn.getColumnDefinition(), "columnDefinition", orderByAnn );
}
applyOrderColumn( jaxbPluralAttribute, memberDetails, xmlDocumentContext );

if ( StringHelper.isNotEmpty( jaxbPluralAttribute.getSort() ) ) {
final MutableAnnotationUsage<SortComparator> sortAnn = getOrMakeAnnotation(
SortComparator.class,
memberDetails,
documentContext
xmlDocumentContext
);
final ClassDetails comparatorClassDetails = classDetailsRegistry.resolveClassDetails( jaxbPluralAttribute.getSort() );
sortAnn.setAttributeValue( "value", comparatorClassDetails );
}

if ( jaxbPluralAttribute.getSortNatural() != null ) {
getOrMakeAnnotation( SortNatural.class, memberDetails, documentContext );
getOrMakeAnnotation( SortNatural.class, memberDetails, xmlDocumentContext );
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// map-key

if ( jaxbPluralAttribute.getMapKey() != null ) {
final MutableAnnotationUsage<MapKey> mapKeyAnn = getOrMakeAnnotation( MapKey.class, memberDetails, documentContext );
setIf( jaxbPluralAttribute.getMapKey().getName(), "name", mapKeyAnn );
final MutableAnnotationUsage<MapKey> mapKeyAnn = getOrMakeAnnotation( MapKey.class, memberDetails, xmlDocumentContext );
applyOr(
jaxbPluralAttribute.getMapKey(),
JaxbMapKeyImpl::getName,
"name",
mapKeyAnn,
buildingContext.getAnnotationDescriptorRegistry().getDescriptor( MapKey.class )
);
}

if ( jaxbPluralAttribute.getMapKeyClass() != null ) {
final ClassDetails mapKeyClass = classDetailsRegistry.resolveClassDetails( jaxbPluralAttribute.getMapKeyClass().getClazz() );
getOrMakeAnnotation( MapKeyClass.class, memberDetails, documentContext ).setAttributeValue( "value", mapKeyClass );
getOrMakeAnnotation( MapKeyClass.class, memberDetails, xmlDocumentContext ).setAttributeValue( "value", mapKeyClass );
}

if ( jaxbPluralAttribute.getMapKeyTemporal() != null ) {
getOrMakeAnnotation( MapKeyTemporal.class, memberDetails, documentContext ).setAttributeValue(
getOrMakeAnnotation( MapKeyTemporal.class, memberDetails, xmlDocumentContext ).setAttributeValue(
"value",
jaxbPluralAttribute.getMapKeyTemporal()
);
}

if ( jaxbPluralAttribute.getMapKeyEnumerated() != null ) {
getOrMakeAnnotation( MapKeyEnumerated.class, memberDetails, documentContext ).setAttributeValue(
getOrMakeAnnotation( MapKeyEnumerated.class, memberDetails, xmlDocumentContext ).setAttributeValue(
"value",
jaxbPluralAttribute.getMapKeyEnumerated()
);
Expand All @@ -140,35 +136,59 @@ public static void applyPluralAttributeStructure(
jaxbPluralAttribute.getMapKeyAttributeOverrides(),
memberDetails,
"key",
documentContext
xmlDocumentContext
);

jaxbPluralAttribute.getMapKeyConverts().forEach( (jaxbConvert) -> {
XmlAnnotationHelper.applyConvert( jaxbConvert, memberDetails, "key", documentContext );
XmlAnnotationHelper.applyConvert( jaxbConvert, memberDetails, "key", xmlDocumentContext );
} );

XmlAnnotationHelper.applyMapKeyColumn( jaxbPluralAttribute.getMapKeyColumn(), memberDetails, xmlDocumentContext );

// todo : map-key-column, map-key-join-column, map-key-foreign-key
// XmlAnnotationHelper.applyMapKeyColumn( jaxbPluralAttribute.getMapKeyColumn(), memberDetails, buildingContext );
//
// jaxbPluralAttribute.getMapKeyJoinColumns().forEach( jaxbMapKeyJoinColumn -> {
// XmlAnnotationHelper.applyMapKeyJoinColumn( jaxbMapKeyJoinColumn, memberDetails, buildingContext );
// } );
//
// XmlAnnotationHelper.applyForeignKey( jaxbPluralAttribute.getMapKeyForeignKey(), memberDetails, buildingContext );
jaxbPluralAttribute.getMapKeyJoinColumns().forEach( jaxbMapKeyJoinColumn -> {
XmlAnnotationHelper.applyMapKeyJoinColumn( jaxbMapKeyJoinColumn, memberDetails, xmlDocumentContext );
} );

XmlAnnotationHelper.applyForeignKey( jaxbPluralAttribute.getMapKeyForeignKey(), memberDetails, xmlDocumentContext );

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// filters and custom sql

jaxbPluralAttribute.getFilters().forEach( (jaxbFilter) -> {
XmlAnnotationHelper.applyFilter( jaxbFilter, memberDetails, documentContext );
XmlAnnotationHelper.applyFilter( jaxbFilter, memberDetails, xmlDocumentContext );
} );

XmlAnnotationHelper.applySqlRestriction( jaxbPluralAttribute.getSqlRestriction(), memberDetails, documentContext );
XmlAnnotationHelper.applySqlRestriction( jaxbPluralAttribute.getSqlRestriction(), memberDetails, xmlDocumentContext );

XmlAnnotationHelper.applyCustomSql( jaxbPluralAttribute.getSqlInsert(), memberDetails, SQLInsert.class, xmlDocumentContext );
XmlAnnotationHelper.applyCustomSql( jaxbPluralAttribute.getSqlUpdate(), memberDetails, SQLUpdate.class, xmlDocumentContext );
XmlAnnotationHelper.applyCustomSql( jaxbPluralAttribute.getSqlDelete(), memberDetails, SQLDelete.class, xmlDocumentContext );
XmlAnnotationHelper.applyCustomSql( jaxbPluralAttribute.getSqlDeleteAll(), memberDetails, SQLDeleteAll.class, xmlDocumentContext );
}

XmlAnnotationHelper.applyCustomSql( jaxbPluralAttribute.getSqlInsert(), memberDetails, SQLInsert.class, documentContext );
XmlAnnotationHelper.applyCustomSql( jaxbPluralAttribute.getSqlUpdate(), memberDetails, SQLUpdate.class, documentContext );
XmlAnnotationHelper.applyCustomSql( jaxbPluralAttribute.getSqlDelete(), memberDetails, SQLDelete.class, documentContext );
XmlAnnotationHelper.applyCustomSql( jaxbPluralAttribute.getSqlDeleteAll(), memberDetails, SQLDeleteAll.class, documentContext );
private static void applyOrderColumn(
JaxbPluralAttribute jaxbPluralAttribute,
MutableMemberDetails memberDetails,
XmlDocumentContext xmlDocumentContext) {
final JaxbOrderColumnImpl jaxbOrderColumn = jaxbPluralAttribute.getOrderColumn();
if ( jaxbOrderColumn == null ) {
return;
}

final MutableAnnotationUsage<OrderColumn> orderColumnAnn = getOrMakeAnnotation(
OrderColumn.class,
memberDetails,
xmlDocumentContext
);
final AnnotationDescriptor<OrderColumn> orderColumnDescriptor = xmlDocumentContext.getModelBuildingContext()
.getAnnotationDescriptorRegistry()
.getDescriptor( OrderColumn.class );

applyOr( jaxbOrderColumn, JaxbOrderColumnImpl::getName, "name", orderColumnAnn, orderColumnDescriptor );
applyOr( jaxbOrderColumn, JaxbOrderColumnImpl::isNullable, "nullable", orderColumnAnn, orderColumnDescriptor );
applyOr( jaxbOrderColumn, JaxbOrderColumnImpl::isInsertable, "insertable", orderColumnAnn, orderColumnDescriptor );
applyOr( jaxbOrderColumn, JaxbOrderColumnImpl::isUpdatable, "updatable", orderColumnAnn, orderColumnDescriptor );
applyOr( jaxbOrderColumn, JaxbOrderColumnImpl::getColumnDefinition, "columnDefinition", orderColumnAnn, orderColumnDescriptor );
applyOr( jaxbOrderColumn, JaxbOrderColumnImpl::getOptions, "options", orderColumnAnn, orderColumnDescriptor );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,27 @@
*/
package org.hibernate.boot.models.categorize.xml.internal.attr;

import org.hibernate.annotations.Bag;
import org.hibernate.annotations.CollectionId;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.Nationalized;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLDeleteAll;
import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLUpdate;
import org.hibernate.annotations.SortComparator;
import org.hibernate.annotations.SortNatural;
import org.hibernate.boot.internal.CollectionClassification;
import org.hibernate.boot.internal.LimitedCollectionClassification;
import org.hibernate.boot.internal.Target;
import org.hibernate.boot.jaxb.mapping.spi.JaxbCollectionIdImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbColumnImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbCollectionTableImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbElementCollectionImpl;
import org.hibernate.boot.jaxb.mapping.spi.JaxbGeneratedValueImpl;
import org.hibernate.boot.models.categorize.xml.internal.XmlAnnotationHelper;
import org.hibernate.boot.models.categorize.xml.internal.XmlProcessingHelper;
import org.hibernate.boot.models.categorize.xml.spi.XmlDocumentContext;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.models.internal.MutableAnnotationUsage;
import org.hibernate.models.internal.MutableClassDetails;
import org.hibernate.models.internal.MutableMemberDetails;
import org.hibernate.models.spi.ClassDetails;
import org.hibernate.models.spi.ClassDetailsRegistry;
import org.hibernate.models.spi.AnnotationDescriptor;

import jakarta.persistence.AccessType;
import jakarta.persistence.Column;
import jakarta.persistence.CollectionTable;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Lob;
import jakarta.persistence.OrderBy;
import jakarta.persistence.OrderColumn;
import jakarta.persistence.Temporal;

import static org.hibernate.boot.models.categorize.xml.internal.XmlAnnotationHelper.applyIndexes;
import static org.hibernate.boot.models.categorize.xml.internal.XmlAnnotationHelper.applyOr;
import static org.hibernate.boot.models.categorize.xml.internal.XmlAnnotationHelper.applyUniqueConstraints;
import static org.hibernate.boot.models.categorize.xml.internal.XmlAnnotationHelper.createForeignKeyAnnotation;
import static org.hibernate.boot.models.categorize.xml.internal.XmlAnnotationHelper.createJoinColumns;
import static org.hibernate.internal.util.NullnessHelper.coalesce;

/**
Expand Down Expand Up @@ -82,36 +67,17 @@ public static MutableMemberDetails processElementCollectionAttribute(

CommonPluralAttributeProcessing.applyPluralAttributeStructure( jaxbElementCollection, memberDetails, xmlDocumentContext );

applyCollectionTable( jaxbElementCollection.getCollectionTable(), memberDetails, xmlDocumentContext );

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// elements

if ( jaxbElementCollection.getEnumerated() != null ) {
final MutableAnnotationUsage<Enumerated> enumeratedAnn = XmlProcessingHelper.getOrMakeAnnotation(
Enumerated.class,
memberDetails,
xmlDocumentContext
);
enumeratedAnn.setAttributeValue( "value", jaxbElementCollection.getEnumerated() );
}

if ( jaxbElementCollection.getLob() != null ) {
XmlProcessingHelper.getOrMakeAnnotation( Lob.class, memberDetails, xmlDocumentContext );
}

if ( jaxbElementCollection.getNationalized() != null ) {
XmlProcessingHelper.getOrMakeAnnotation( Nationalized.class, memberDetails, xmlDocumentContext );
}

if ( jaxbElementCollection.getTemporal() != null ) {
final MutableAnnotationUsage<Temporal> temporalAnn = XmlProcessingHelper.getOrMakeAnnotation(
Temporal.class,
memberDetails,
xmlDocumentContext
);
temporalAnn.setAttributeValue( "value", jaxbElementCollection.getTemporal() );
}

XmlAnnotationHelper.applyEnumerated( jaxbElementCollection.getEnumerated(), memberDetails, xmlDocumentContext );
XmlAnnotationHelper.applyLob( jaxbElementCollection.getLob(), memberDetails, xmlDocumentContext );
XmlAnnotationHelper.applyNationalized( jaxbElementCollection.getNationalized(), memberDetails, xmlDocumentContext );
XmlAnnotationHelper.applyTemporal( jaxbElementCollection.getTemporal(), memberDetails, xmlDocumentContext );
XmlAnnotationHelper.applyBasicTypeComposition( jaxbElementCollection, memberDetails, xmlDocumentContext );

if ( StringHelper.isNotEmpty( jaxbElementCollection.getTargetClass() ) ) {
final MutableAnnotationUsage<Target> targetAnn = XmlProcessingHelper.getOrMakeAnnotation( Target.class, memberDetails, xmlDocumentContext );
targetAnn.setAttributeValue( "value", jaxbElementCollection.getTargetClass() );
Expand All @@ -121,20 +87,45 @@ public static MutableMemberDetails processElementCollectionAttribute(
XmlAnnotationHelper.applyConvert( jaxbConvert, memberDetails, xmlDocumentContext );
} );

XmlAnnotationHelper.applyAttributeOverrides(
jaxbElementCollection.getAttributeOverrides(),
memberDetails,
"value",
xmlDocumentContext
);
XmlAnnotationHelper.applyAttributeOverrides( jaxbElementCollection.getAttributeOverrides(), memberDetails, xmlDocumentContext );

XmlAnnotationHelper.applyAssociationOverrides(
jaxbElementCollection.getAssociationOverrides(),
XmlAnnotationHelper.applyAssociationOverrides( jaxbElementCollection.getAssociationOverrides(), memberDetails, xmlDocumentContext );

return memberDetails;
}

public static void applyCollectionTable(
JaxbCollectionTableImpl jaxbCollectionTable,
MutableMemberDetails memberDetails,
XmlDocumentContext xmlDocumentContext) {
if ( jaxbCollectionTable == null ) {
return;
}

final MutableAnnotationUsage<CollectionTable> collectionTableAnn = XmlProcessingHelper.getOrMakeAnnotation(
CollectionTable.class,
memberDetails,
"value",
xmlDocumentContext
);
final AnnotationDescriptor<CollectionTable> collectionTableDescriptor = xmlDocumentContext.getModelBuildingContext()
.getAnnotationDescriptorRegistry()
.getDescriptor( CollectionTable.class );

return memberDetails;
applyOr( jaxbCollectionTable, JaxbCollectionTableImpl::getName, "name", collectionTableAnn, collectionTableDescriptor );
applyOr( jaxbCollectionTable, JaxbCollectionTableImpl::getSchema, "schema", collectionTableAnn, collectionTableDescriptor );
applyOr( jaxbCollectionTable, JaxbCollectionTableImpl::getOptions, "options", collectionTableAnn, collectionTableDescriptor );

collectionTableAnn.setAttributeValue( "joinColumns", createJoinColumns( jaxbCollectionTable.getJoinColumns(), memberDetails, xmlDocumentContext ) );

if ( jaxbCollectionTable.getForeignKeys() != null ) {
collectionTableAnn.setAttributeValue(
"foreignKey",
createForeignKeyAnnotation( jaxbCollectionTable.getForeignKeys(), memberDetails, xmlDocumentContext )
);
}

applyUniqueConstraints( jaxbCollectionTable.getUniqueConstraints(), memberDetails, collectionTableAnn, xmlDocumentContext );

applyIndexes( jaxbCollectionTable.getIndexes(), memberDetails, collectionTableAnn, xmlDocumentContext );
}
}
Loading

0 comments on commit b7e7b77

Please sign in to comment.