Skip to content

Commit

Permalink
Simple code reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed Oct 19, 2023
1 parent 731bfd4 commit efcd1a7
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import org.hibernate.boot.jaxb.mapping.JaxbLob;
import org.hibernate.boot.jaxb.mapping.JaxbNationalized;
import org.hibernate.boot.jaxb.mapping.JaxbSequenceGenerator;
import org.hibernate.boot.jaxb.mapping.JaxbTable;
import org.hibernate.boot.jaxb.mapping.JaxbTableGenerator;
import org.hibernate.boot.jaxb.mapping.JaxbUuidGenerator;
import org.hibernate.models.internal.CollectionHelper;
import org.hibernate.models.internal.StringHelper;
import org.hibernate.models.orm.xml.spi.PersistenceUnitMetadata;
import org.hibernate.models.source.internal.MutableAnnotationTarget;
import org.hibernate.models.source.internal.MutableMemberDetails;
import org.hibernate.models.source.internal.dynamic.DynamicAnnotationUsage;
Expand All @@ -39,7 +41,6 @@
import org.hibernate.models.source.spi.ClassDetails;
import org.hibernate.models.source.spi.ClassDetailsRegistry;
import org.hibernate.models.source.spi.SourceModelBuildingContext;
import org.hibernate.tuple.GenerationTiming;

import jakarta.persistence.Access;
import jakarta.persistence.AccessType;
Expand All @@ -52,11 +53,14 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Lob;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.Table;
import jakarta.persistence.TableGenerator;
import jakarta.persistence.Temporal;
import jakarta.persistence.TemporalType;

import static java.util.Collections.emptyList;
import static org.hibernate.internal.util.NullnessHelper.coalesce;
import static org.hibernate.models.internal.StringHelper.nullIfEmpty;

/**
* Helper for creating annotation from equivalent JAXB
Expand Down Expand Up @@ -408,4 +412,24 @@ public static void applyConvert(
annotationUsage.setAttributeValue( "attributeName", jaxbConvert.getAttributeName() );
annotationUsage.setAttributeValue( "disableConversion", jaxbConvert.isDisableConversion() );
}

public static void applyTable(
JaxbTable jaxbTable,
MutableAnnotationTarget target,
PersistenceUnitMetadata persistenceUnitMetadata) {
final DynamicAnnotationUsage<Table> tableAnn = new DynamicAnnotationUsage<>( Table.class, target );
tableAnn.setAttributeValue( "name", nullIfEmpty( jaxbTable.getName() ) );
tableAnn.setAttributeValue( "catalog", coalesce(
nullIfEmpty( jaxbTable.getCatalog() ),
persistenceUnitMetadata.getDefaultCatalog()
) );
tableAnn.setAttributeValue( "schema", coalesce(
nullIfEmpty( jaxbTable.getSchema() ),
persistenceUnitMetadata.getDefaultSchema()
) );
// todo : uniqueConstraints
// todo : indexes
target.addAnnotationUsage( tableAnn );

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@

import java.beans.Introspector;

import org.hibernate.boot.jaxb.mapping.AttributesContainer;
import org.hibernate.boot.jaxb.mapping.JaxbBasic;
import org.hibernate.boot.jaxb.mapping.JaxbElementCollection;
import org.hibernate.boot.jaxb.mapping.JaxbEmbedded;
import org.hibernate.boot.jaxb.mapping.JaxbHbmAnyMapping;
import org.hibernate.boot.jaxb.mapping.JaxbHbmManyToAny;
import org.hibernate.boot.jaxb.mapping.JaxbManyToMany;
import org.hibernate.boot.jaxb.mapping.JaxbOneToMany;
import org.hibernate.boot.jaxb.mapping.JaxbOneToOne;
import org.hibernate.boot.jaxb.mapping.PersistentAttribute;
import org.hibernate.models.source.internal.MutableClassDetails;
import org.hibernate.models.source.internal.MutableMemberDetails;
import org.hibernate.models.source.spi.FieldDetails;
Expand Down Expand Up @@ -65,6 +74,107 @@ else if ( methodDetails.getName().startsWith( "is" ) ) {
return null;
}

public static void handleAttributes(
AttributesContainer attributesContainer,
MutableClassDetails mutableClassDetails,
AccessType classAccessType,
SourceModelBuildingContext sourceModelBuildingContext) {
for ( int i = 0; i < attributesContainer.getBasicAttributes().size(); i++ ) {
handleBasicAttribute(
attributesContainer.getBasicAttributes().get( i ),
mutableClassDetails,
classAccessType,
sourceModelBuildingContext
);
}

for ( int i = 0; i < attributesContainer.getEmbeddedAttributes().size(); i++ ) {
handleEmbeddedAttribute(
attributesContainer.getEmbeddedAttributes().get( i ),
mutableClassDetails,
classAccessType,
sourceModelBuildingContext
);
}

for ( int i = 0; i < attributesContainer.getOneToOneAttributes().size(); i++ ) {
handleOneToOneAttribute(
attributesContainer.getOneToOneAttributes().get( i ),
mutableClassDetails,
classAccessType,
sourceModelBuildingContext
);
}

for ( int i = 0; i < attributesContainer.getManyToOneAttributes().size(); i++ ) {
handleManyToOneAttribute(
attributesContainer.getOneToOneAttributes().get( i ),
mutableClassDetails,
classAccessType,
sourceModelBuildingContext
);
}

for ( int i = 0; i < attributesContainer.getDiscriminatedAssociations().size(); i++ ) {
handleDiscriminatedAssociationAttribute(
attributesContainer.getDiscriminatedAssociations().get( i ),
mutableClassDetails,
classAccessType,
sourceModelBuildingContext
);
}

for ( int i = 0; i < attributesContainer.getElementCollectionAttributes().size(); i++ ) {
handleElementCollectionAttribute(
attributesContainer.getElementCollectionAttributes().get( i ),
mutableClassDetails,
classAccessType,
sourceModelBuildingContext
);
}

for ( int i = 0; i < attributesContainer.getOneToManyAttributes().size(); i++ ) {
handleOneToManyAttribute(
attributesContainer.getOneToManyAttributes().get( i ),
mutableClassDetails,
classAccessType,
sourceModelBuildingContext
);
}

for ( int i = 0; i < attributesContainer.getManyToManyAttributes().size(); i++ ) {
handleManyToManyAttribute(
attributesContainer.getManyToManyAttributes().get( i ),
mutableClassDetails,
classAccessType,
sourceModelBuildingContext
);
}

for ( int i = 0; i < attributesContainer.getPluralDiscriminatedAssociations().size(); i++ ) {
handlePluralDiscriminatedAssociationAttribute(
attributesContainer.getPluralDiscriminatedAssociations().get( i ),
mutableClassDetails,
classAccessType,
sourceModelBuildingContext
);
}

}

public static void applyCommonAttributeAnnotations(
PersistentAttribute jaxbAttribute,
MutableMemberDetails memberDetails,
AccessType accessType,
SourceModelBuildingContext sourceModelBuildingContext) {
XmlAnnotationHelper.applyAccess( accessType, memberDetails, sourceModelBuildingContext );
XmlAnnotationHelper.applyAttributeAccessor(
jaxbAttribute.getAttributeAccessor(),
memberDetails,
sourceModelBuildingContext
);
}

public static void handleBasicAttribute(
JaxbBasic jaxbBasic,
MutableClassDetails mutableClassDetails,
Expand Down Expand Up @@ -97,24 +207,69 @@ public static void handleBasicAttribute(
XmlAnnotationHelper.applyEnumerated( jaxbBasic.getEnumerated(), memberDetails, sourceModelBuildingContext );
XmlAnnotationHelper.applyNationalized( jaxbBasic.getNationalized(), memberDetails, sourceModelBuildingContext );
XmlAnnotationHelper.applyJdbcTypeCode( jaxbBasic.getJdbcTypeCode(), memberDetails, sourceModelBuildingContext );
}

public static void handleEmbeddedAttribute(
JaxbEmbedded jaxbEmbedded,
MutableClassDetails mutableClassDetails,
AccessType classAccessType,
SourceModelBuildingContext sourceModelBuildingContext) {
throw new UnsupportedOperationException( "Support for embedded attributes not yet implemented" );
}

private static void applyCommonAttributeAnnotations(
JaxbBasic jaxbBasic,
MutableMemberDetails memberDetails,
AccessType accessType,
public static void handleOneToOneAttribute(
JaxbOneToOne jaxbOneToOne,
MutableClassDetails mutableClassDetails,
AccessType classAccessType,
SourceModelBuildingContext sourceModelBuildingContext) {
XmlAnnotationHelper.applyAccess( accessType, memberDetails, sourceModelBuildingContext );
XmlAnnotationHelper.applyAttributeAccessor(
jaxbBasic.getAttributeAccessor(),
memberDetails,
sourceModelBuildingContext
);
XmlAnnotationHelper.applyAttributeAccessor(
jaxbBasic.getAttributeAccessor(),
memberDetails,
sourceModelBuildingContext
);
throw new UnsupportedOperationException( "Support for one-to-one attributes not yet implemented" );
}

public static void handleManyToOneAttribute(
JaxbOneToOne jaxbOneToOne,
MutableClassDetails mutableClassDetails,
AccessType classAccessType,
SourceModelBuildingContext sourceModelBuildingContext) {
throw new UnsupportedOperationException( "Support for many-to-one attributes not yet implemented" );
}

public static void handleDiscriminatedAssociationAttribute(
JaxbHbmAnyMapping jaxbHbmAnyMapping,
MutableClassDetails mutableClassDetails,
AccessType classAccessType,
SourceModelBuildingContext sourceModelBuildingContext) {
throw new UnsupportedOperationException( "Support for any attributes not yet implemented" );
}

public static void handleElementCollectionAttribute(
JaxbElementCollection jaxbElementCollection,
MutableClassDetails mutableClassDetails,
AccessType classAccessType,
SourceModelBuildingContext sourceModelBuildingContext) {
throw new UnsupportedOperationException( "Support for element-collection attributes not yet implemented" );
}

public static void handleOneToManyAttribute(
JaxbOneToMany jaxbOneToMany,
MutableClassDetails mutableClassDetails,
AccessType classAccessType,
SourceModelBuildingContext sourceModelBuildingContext) {
throw new UnsupportedOperationException( "Support for one-to-many attributes not yet implemented" );
}

public static void handleManyToManyAttribute(
JaxbManyToMany jaxbManyToMany,
MutableClassDetails mutableClassDetails,
AccessType classAccessType,
SourceModelBuildingContext sourceModelBuildingContext) {
throw new UnsupportedOperationException( "Support for many-to-many attributes not yet implemented" );
}

public static void handlePluralDiscriminatedAssociationAttribute(
JaxbHbmManyToAny jaxbHbmManyToAny,
MutableClassDetails mutableClassDetails,
AccessType classAccessType,
SourceModelBuildingContext sourceModelBuildingContext) {
throw new UnsupportedOperationException( "Support for many-to-any attributes not yet implemented" );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@

import jakarta.persistence.AccessType;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;

import static org.hibernate.internal.util.NullnessHelper.coalesce;
import static org.hibernate.models.internal.StringHelper.nullIfEmpty;

/**
* @author Steve Ebersole
Expand Down Expand Up @@ -79,19 +77,7 @@ public static void makeCompleteEntityMapping(
annotationTarget.addAnnotationUsage( entityAnn );

if ( jaxbEntity.getTable() != null ) {
final DynamicAnnotationUsage<Table> tableAnn = new DynamicAnnotationUsage<>( Table.class, classDetails );
tableAnn.setAttributeValue( "name", nullIfEmpty( jaxbEntity.getTable().getName() ) );
tableAnn.setAttributeValue( "catalog", coalesce(
nullIfEmpty( jaxbEntity.getTable().getCatalog() ),
persistenceUnitMetadata.getDefaultCatalog()
) );
tableAnn.setAttributeValue( "schema", coalesce(
nullIfEmpty( jaxbEntity.getTable().getSchema() ),
persistenceUnitMetadata.getDefaultSchema()
) );
// todo : uniqueConstraints
// todo : indexes
annotationTarget.addAnnotationUsage( tableAnn );
XmlAnnotationHelper.applyTable( jaxbEntity.getTable(), (MutableAnnotationTarget) classDetails, persistenceUnitMetadata );
}

final AccessType classAccessType = coalesce(
Expand All @@ -113,8 +99,7 @@ public static void makeCompleteEntityMapping(
sourceModelBuildingContext
);

XmlAnnotationHelper.applyAccess( accessType, memberDetails, sourceModelBuildingContext );
XmlAnnotationHelper.applyAttributeAccessor( jaxbId.getAttributeAccessor(), memberDetails, sourceModelBuildingContext );
XmlAttributeHelper.applyCommonAttributeAnnotations( jaxbId, memberDetails, accessType, sourceModelBuildingContext );

XmlAnnotationHelper.applyColumn( jaxbId.getColumn(), memberDetails, sourceModelBuildingContext );

Expand Down Expand Up @@ -148,49 +133,10 @@ public static void makeCompleteEntityMapping(
XmlAnnotationHelper.applyAttributeOverrides( jaxbEmbeddedId.getAttributeOverride(), memberDetails, sourceModelBuildingContext );
}

for ( int i = 0; i < attributes.getBasicAttributes().size(); i++ ) {
XmlAttributeHelper.handleBasicAttribute(
attributes.getBasicAttributes().get( i ),
mutableClassDetails,
classAccessType,
sourceModelBuildingContext
);
}

if ( attributes.getNaturalId() != null ) {
throw new UnsupportedOperationException( "Support for natural-id not yet implemented" );
}

for ( int i = 0; i < attributes.getEmbeddedAttributes().size(); i++ ) {
throw new UnsupportedOperationException( "Support for embedded attributes not yet implemented" );
}

for ( int i = 0; i < attributes.getOneToOneAttributes().size(); i++ ) {
throw new UnsupportedOperationException( "Support for one-to-one attributes not yet implemented" );
}

for ( int i = 0; i < attributes.getManyToOneAttributes().size(); i++ ) {
throw new UnsupportedOperationException( "Support for many-to-one attributes not yet implemented" );
}

for ( int i = 0; i < attributes.getElementCollectionAttributes().size(); i++ ) {
throw new UnsupportedOperationException( "Support for element-collection attributes not yet implemented" );
}

for ( int i = 0; i < attributes.getOneToManyAttributes().size(); i++ ) {
throw new UnsupportedOperationException( "Support for one-to-many attributes not yet implemented" );
}

for ( int i = 0; i < attributes.getManyToManyAttributes().size(); i++ ) {
throw new UnsupportedOperationException( "Support for many-to-many attributes not yet implemented" );
}

for ( int i = 0; i < attributes.getDiscriminatedAssociations().size(); i++ ) {
throw new UnsupportedOperationException( "Support for any attributes not yet implemented" );
}

for ( int i = 0; i < attributes.getPluralDiscriminatedAssociations().size(); i++ ) {
throw new UnsupportedOperationException( "Support for many-to-any attributes not yet implemented" );
}
XmlAttributeHelper.handleAttributes( jaxbEntity.getAttributes(), mutableClassDetails, classAccessType, sourceModelBuildingContext );
}
}

0 comments on commit efcd1a7

Please sign in to comment.