Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
package org.hibernate.boot.jaxb.hbm.transform;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.hibernate.MappingException;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping;
import org.hibernate.boot.jaxb.spi.Binding;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.mapping.Collection;
import org.hibernate.mapping.Component;
Expand All @@ -21,33 +18,27 @@
import org.hibernate.mapping.RootClass;
import org.hibernate.mapping.Table;
import org.hibernate.mapping.ToOne;
import org.hibernate.service.ServiceRegistry;

/**
* @author Steve Ebersole
*/
public class BootModelPreprocessor {
private static final Map<String,String> entityByNameMap = new HashMap<>();

static void preprocessBooModel(
List<Binding<JaxbHbmHibernateMapping>> hbmXmlBindings,
MetadataImplementor bootModel,
ServiceRegistry serviceRegistry,
TransformationState transformationState) {
static void preprocessBooModel(MetadataImplementor bootModel, TransformationState transformationState) {
entityByNameMap.clear();

bootModel.getEntityBindings().forEach( (persistentClass) -> {
final Table table = TransformationHelper.determineEntityTable( persistentClass );
final EntityTypeInfo entityTypeInfo = new EntityTypeInfo( table, persistentClass );
transformationState.getEntityInfoByName().put( persistentClass.getEntityName(), entityTypeInfo );
buildPersistentClassPropertyInfos( persistentClass, entityTypeInfo, bootModel, transformationState );
buildPersistentClassPropertyInfos( persistentClass, entityTypeInfo, transformationState );
} );
}

private static void buildPersistentClassPropertyInfos(
PersistentClass persistentClass,
EntityTypeInfo entityTypeInfo,
MetadataImplementor bootModel,
TransformationState transformationState) {
if ( persistentClass.getClassName() != null ) {
final String previous = entityByNameMap.put( persistentClass.getClassName(), persistentClass.getEntityName() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@

import static org.hibernate.boot.jaxb.hbm.transform.HbmTransformationLogging.TRANSFORMATION_LOGGER;
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
import static org.hibernate.internal.util.StringHelper.nullIfEmpty;

/**
* Transforms {@code hbm.xml} {@linkplain JaxbHbmHibernateMapping JAXB} bindings into
Expand Down Expand Up @@ -230,22 +231,15 @@ public static List<Binding<JaxbEntityMappingsImpl>> transform(
final List<Binding<JaxbEntityMappingsImpl>> transformations = XmlPreprocessor.preprocessHbmXml( hbmXmlBindings, transformationState );

// build and perform a pass over the boot model building the rest of the transformation-state
BootModelPreprocessor.preprocessBooModel(
hbmXmlBindings,
bootModel,
serviceRegistry,
transformationState
);
BootModelPreprocessor.preprocessBooModel( bootModel, transformationState );

// now we are ready to fully build the mapping.xml transformations
for ( int i = 0; i < hbmXmlBindings.size(); i++ ) {
final HbmXmlTransformer hbmXmlTransformer = new HbmXmlTransformer(
hbmXmlBindings.get( i ),
transformations.get( i ),
transformationState,
bootModel,
unsupportedFeatureHandling,
serviceRegistry
unsupportedFeatureHandling
);

hbmXmlTransformer.performTransformation();
Expand All @@ -259,10 +253,8 @@ public static List<Binding<JaxbEntityMappingsImpl>> transform(
private final Binding<JaxbEntityMappingsImpl> mappingXmlBinding;

private final TransformationState transformationState;
private final MetadataImplementor bootModel;

private final UnsupportedFeatureHandling unsupportedFeatureHandling;
private final ServiceRegistry serviceRegistry;

// todo (7.0) : use transformation-state instead
private final Map<String,JaxbEmbeddableImpl> jaxbEmbeddableByClassName = new HashMap<>();
Expand All @@ -273,15 +265,11 @@ private HbmXmlTransformer(
Binding<JaxbHbmHibernateMapping> hbmXmlBinding,
Binding<JaxbEntityMappingsImpl> mappingXmlBinding,
TransformationState transformationState,
MetadataImplementor bootModel,
UnsupportedFeatureHandling unsupportedFeatureHandling,
ServiceRegistry serviceRegistry) {
UnsupportedFeatureHandling unsupportedFeatureHandling) {
this.hbmXmlBinding = hbmXmlBinding;
this.mappingXmlBinding = mappingXmlBinding;
this.transformationState = transformationState;
this.bootModel = bootModel;
this.unsupportedFeatureHandling = unsupportedFeatureHandling;
this.serviceRegistry = serviceRegistry;
}


Expand Down Expand Up @@ -1205,7 +1193,7 @@ else if ( !source.getColumnOrFormula().isEmpty() ) {
}
}
}
else if ( StringHelper.isNotEmpty( tableName ) ) {
else if ( isNotEmpty( tableName ) ) {
// this is the case of transforming a <join/> where the property did not specify columns or formula.
// we need to create a column still to pass along the secondary table name
final TargetColumnAdapter column = target.makeColumnAdapter( columnDefaults );
Expand Down Expand Up @@ -1590,7 +1578,7 @@ public Boolean isUpdateable() {
}

private JaxbUserTypeImpl interpretBasicType(String typeName, JaxbHbmConfigParameterContainer typeLocalParams, JaxbHbmTypeDefinitionType typeDef) {
assert StringHelper.isNotEmpty( typeName );
assert isNotEmpty( typeName );

final JaxbUserTypeImpl typeNode = new JaxbUserTypeImpl();

Expand Down Expand Up @@ -1624,7 +1612,7 @@ private JaxbEmbeddableImpl applyEmbeddable(
JaxbHbmCompositeAttributeType hbmComponent,
ComponentTypeInfo componentTypeInfo) {
final String embeddableClassName = componentTypeInfo.getComponent().getComponentClassName();
if ( StringHelper.isNotEmpty( embeddableClassName ) ) {
if ( isNotEmpty( embeddableClassName ) ) {
final JaxbEmbeddableImpl existing = jaxbEmbeddableByClassName.get( embeddableClassName );
if ( existing != null ) {
return existing;
Expand All @@ -1641,7 +1629,7 @@ private JaxbEmbeddableImpl applyEmbeddable(
);
mappingXmlBinding.getRoot().getEmbeddables().add( jaxbEmbeddable );

if ( StringHelper.isNotEmpty( embeddableClassName ) ) {
if ( isNotEmpty( embeddableClassName ) ) {
jaxbEmbeddableByClassName.put( embeddableClassName, jaxbEmbeddable );
}

Expand All @@ -1668,7 +1656,7 @@ private JaxbEmbeddableImpl convertEmbeddable(

private int counter = 1;
private String determineEmbeddableName(String componentClassName, String attributeName) {
if ( StringHelper.isNotEmpty( componentClassName ) ) {
if ( isNotEmpty( componentClassName ) ) {
return componentClassName;
}
return attributeName + "_" + counter++;
Expand All @@ -1692,7 +1680,7 @@ private void transferOneToOne(JaxbHbmOneToOneType hbmOneToOne, PropertyInfo prop
oneToOne.setOrphanRemoval( isOrphanRemoval( hbmOneToOne.getCascade() ) );
oneToOne.setForeignKey( new JaxbForeignKeyImpl() );
oneToOne.getForeignKey().setName( hbmOneToOne.getForeignKey() );
if ( StringHelper.isNotEmpty( hbmOneToOne.getPropertyRef() ) ) {
if ( isNotEmpty( hbmOneToOne.getPropertyRef() ) ) {
oneToOne.setPropertyRef( new JaxbPropertyRefImpl() );
oneToOne.getPropertyRef().setName( hbmOneToOne.getPropertyRef() );
}
Expand Down Expand Up @@ -1736,7 +1724,7 @@ private JaxbManyToOneImpl transformManyToOne(JaxbHbmManyToOneType hbmNode, Prope
jaxbManyToOne.setAttributeAccessor( hbmNode.getAccess() );
jaxbManyToOne.setCascade( convertCascadeType( hbmNode.getCascade() ) );

if ( StringHelper.isNotEmpty( hbmNode.getPropertyRef() ) ) {
if ( isNotEmpty( hbmNode.getPropertyRef() ) ) {
jaxbManyToOne.setPropertyRef( new JaxbPropertyRefImpl() );
jaxbManyToOne.getPropertyRef().setName( hbmNode.getPropertyRef() );
}
Expand Down Expand Up @@ -1905,23 +1893,23 @@ private void transferCollectionCommonInfo(PluralAttributeInfo source, JaxbPlural
target.setFetchMode( convert( source.getFetch() ) );
target.setFetch( convert( source.getLazy() ) );

if ( StringHelper.isNotEmpty( source.getCollectionType() ) ) {
if ( isNotEmpty( source.getCollectionType() ) ) {
final JaxbCollectionUserTypeImpl jaxbCollectionUserType = new JaxbCollectionUserTypeImpl();
target.setCollectionType( jaxbCollectionUserType );
jaxbCollectionUserType.setType( source.getCollectionType() );
}

if ( source instanceof JaxbHbmSetType set ) {
final String sort = set.getSort();
if ( StringHelper.isNotEmpty( sort ) && !"unsorted".equals( sort ) ) {
if ( isNotEmpty( sort ) && !"unsorted".equals( sort ) ) {
target.setSort( sort );
}
target.setOrderBy( set.getOrderBy() );
target.setClassification( LimitedCollectionClassification.SET );
}
else if ( source instanceof JaxbHbmMapType map ) {
final String sort = map.getSort();
if ( StringHelper.isNotEmpty( sort ) && !"unsorted".equals( sort ) ) {
if ( isNotEmpty( sort ) && !"unsorted".equals( sort ) ) {
target.setSort( sort );
}
target.setOrderBy( map.getOrderBy() );
Expand Down Expand Up @@ -2025,7 +2013,7 @@ else if ( source.getMapKey() != null ) {
return;
}

if ( StringHelper.isNotEmpty( source.getMapKey().getNode() ) ) {
if ( isNotEmpty( source.getMapKey().getNode() ) ) {
handleUnsupported(
"Transformation of `node` attribute is not supported - %s",
origin()
Expand All @@ -2040,7 +2028,7 @@ else if ( source.getMapKey() != null ) {
jaxbMapKeyType.setValue( mapKeyType );
}

if ( StringHelper.isNotEmpty( source.getMapKey().getColumnAttribute() ) ) {
if ( isNotEmpty( source.getMapKey().getColumnAttribute() ) ) {
final JaxbMapKeyColumnImpl mapKeyColumn = new JaxbMapKeyColumnImpl();
mapKeyColumn.setName( source.getMapKey().getColumnAttribute() );
target.setMapKeyColumn( mapKeyColumn );
Expand All @@ -2049,38 +2037,32 @@ else if ( source.getMapKey() != null ) {
}

private String resolveMapKeyType(JaxbHbmMapKeyBasicType mapKey) {
if ( StringHelper.isNotEmpty( mapKey.getTypeAttribute() ) ) {
if ( isNotEmpty( mapKey.getTypeAttribute() ) ) {
return mapKey.getTypeAttribute();
}

if ( mapKey.getType() != null ) {
return StringHelper.nullIfEmpty( mapKey.getType().getName() );
else if ( mapKey.getType() != null ) {
return nullIfEmpty( mapKey.getType().getName() );
}
else {
return null;
}

return null;
}

private Boolean invert(Boolean value) {
return invert( value, null );
}

private Boolean invert(Boolean value, Boolean defaultValue) {
if ( value == null ) {
return defaultValue;
}
return !value;
return value == null ? null : !value;
}

private JaxbPluralFetchModeImpl convert(JaxbHbmFetchStyleWithSubselectEnum fetch) {
if ( fetch != null ) {
if ( fetch == null ) {
return null;
}
else {
return switch ( fetch ) {
case SELECT -> JaxbPluralFetchModeImpl.SELECT;
case JOIN -> JaxbPluralFetchModeImpl.JOIN;
case SUBSELECT -> JaxbPluralFetchModeImpl.SUBSELECT;
};
}

return null;
}


Expand Down Expand Up @@ -2181,7 +2163,7 @@ private void transferElementInfo(
final ComponentTypeInfo componentTypeInfo = transformationState.getEmbeddableInfoByRole().get( partRole );

target.setTarget( embeddableName );
if ( StringHelper.isNotEmpty( embeddableClassName ) ) {
if ( isNotEmpty( embeddableClassName ) ) {
target.setTargetClass( embeddableClassName );
}

Expand Down Expand Up @@ -2217,7 +2199,7 @@ private void transferOneToManyInfo(
}

transferCollectionCommonInfo( hbmAttributeInfo, target );
target.setTargetEntity( StringHelper.isNotEmpty( hbmOneToMany.getClazz() ) ? hbmOneToMany.getClazz() : hbmOneToMany.getEntityName() );
target.setTargetEntity( isNotEmpty( hbmOneToMany.getClazz() ) ? hbmOneToMany.getClazz() : hbmOneToMany.getEntityName() );

final Property bootModelProperty = propertyInfo.bootModelProperty();
final Collection bootModelValue = (Collection) bootModelProperty.getValue();
Expand Down Expand Up @@ -2291,7 +2273,7 @@ public void addFormula(String formula) {
target.getFilters().add( convert( hbmFilter ) );
}

if ( StringHelper.isNotEmpty( hbmAttributeInfo.getWhere() ) ) {
if ( isNotEmpty( hbmAttributeInfo.getWhere() ) ) {
target.setSqlRestriction( hbmAttributeInfo.getWhere() );
}
if ( hbmAttributeInfo.getSqlInsert() != null ) {
Expand Down Expand Up @@ -2320,7 +2302,7 @@ private String resolveMappedBy(
PluralAttributeInfo hbmAttributeInfo,
Property bootModelProperty,
Collection bootModelValue) {
if ( StringHelper.isNotEmpty( bootModelValue.getMappedByProperty() ) ) {
if ( isNotEmpty( bootModelValue.getMappedByProperty() ) ) {
return bootModelValue.getMappedByProperty();
}

Expand Down Expand Up @@ -2369,8 +2351,8 @@ private boolean matches(KeyValue collectionKey, List<Selectable> candidate) {

final Column collectionKeyColumn = (Column) collectionKeySelectable;
final Column candidateColumn = (Column) candidateSelectable;
assert StringHelper.isNotEmpty( collectionKeyColumn.getCanonicalName() );
assert StringHelper.isNotEmpty( candidateColumn.getCanonicalName() );
assert isNotEmpty( collectionKeyColumn.getCanonicalName() );
assert isNotEmpty( candidateColumn.getCanonicalName() );
if ( !collectionKeyColumn.getCanonicalName().equals( candidateColumn.getCanonicalName() ) ) {
return false;
}
Expand Down Expand Up @@ -2400,7 +2382,7 @@ private void transferManyToManyInfo(
if ( manyToMany.isEmbedXml() != null ) {
handleUnsupported( "`embed-xml` no longer supported" );
}
if ( StringHelper.isNotEmpty( manyToMany.getNode() ) ) {
if ( isNotEmpty( manyToMany.getNode() ) ) {
handleUnsupported( "`node` no longer supported" );
}

Expand All @@ -2409,7 +2391,7 @@ private void transferManyToManyInfo(

final JaxbJoinTableImpl joinTable = new JaxbJoinTableImpl();
final String tableName = hbmCollection.getTable();
if ( StringHelper.isNotEmpty( tableName ) ) {
if ( isNotEmpty( tableName ) ) {
joinTable.setName( tableName );
}
target.setJoinTable( joinTable );
Expand Down Expand Up @@ -2503,7 +2485,7 @@ public void addFormula(String formula) {
);

transferCollectionCommonInfo( hbmCollection, target );
target.setTargetEntity( StringHelper.isNotEmpty( manyToMany.getClazz() ) ? manyToMany.getClazz() : manyToMany.getEntityName() );
target.setTargetEntity( isNotEmpty( manyToMany.getClazz() ) ? manyToMany.getClazz() : manyToMany.getEntityName() );

if ( manyToMany.getNotFound() == JaxbHbmNotFoundEnum.IGNORE ) {
target.setNotFound( NotFoundAction.IGNORE );
Expand All @@ -2513,7 +2495,7 @@ public void addFormula(String formula) {
target.getFilters().add( convert( hbmFilter ) );
}

if ( StringHelper.isNotEmpty( hbmCollection.getWhere() ) ) {
if ( isNotEmpty( hbmCollection.getWhere() ) ) {
target.setSqlRestriction( hbmCollection.getWhere() );
}
if ( hbmCollection.getSqlInsert() != null ) {
Expand Down Expand Up @@ -2664,7 +2646,7 @@ private JaxbEmbeddableImpl transformEmbeddedIdEmbeddable(
EntityTypeInfo bootEntityInfo,
Property idProperty) {
final String embeddableClassName = hbmCompositeId.getClazz();
if ( StringHelper.isNotEmpty( embeddableClassName ) ) {
if ( isNotEmpty( embeddableClassName ) ) {
final JaxbEmbeddableImpl existing = jaxbEmbeddableByClassName.get( embeddableClassName );
if ( existing != null ) {
return existing;
Expand Down

This file was deleted.

Loading
Loading