From 4a60011cb7d1b7859501de37bba65af9c571bbdd Mon Sep 17 00:00:00 2001 From: Chris Cranford Date: Tue, 9 Aug 2016 19:01:45 -0500 Subject: [PATCH] HHH-10978 - Fix building join table metadata with default catalogs or schema values. (cherry picked from commit 1dcb391f83b771942e40919ba8081bcaff7be0ea) Conflicts: hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java hibernate-core/src/main/java/org/hibernate/cfg/annotations/TableBinder.java --- .../java/org/hibernate/cfg/BinderHelper.java | 72 +++++++++++++------ .../cfg/annotations/TableBinder.java | 9 +-- 2 files changed, 57 insertions(+), 24 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java b/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java index eba3f2b70b..a0ac351157 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/BinderHelper.java @@ -59,7 +59,7 @@ public class BinderHelper { public static final String ANNOTATION_STRING_DEFAULT = ""; - private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, BinderHelper.class.getName()); + private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class, BinderHelper.class.getName() ); private BinderHelper() { } @@ -237,7 +237,9 @@ public class BinderHelper { boolean inverse, MetadataBuildingContext context) { //associated entity only used for more precise exception, yuk! - if ( columns[0].isImplicit() || StringHelper.isNotEmpty( columns[0].getMappedBy() ) ) return; + if ( columns[0].isImplicit() || StringHelper.isNotEmpty( columns[0].getMappedBy() ) ) { + return; + } int fkEnum = Ejb3JoinColumn.checkReferencedColumnsType( columns, ownerEntity, context ); PersistentClass associatedClass = columns[0].getPropertyHolder() != null ? columns[0].getPropertyHolder().getPersistentClass() : @@ -407,13 +409,18 @@ public class BinderHelper { break; } } - if ( !found ) return null; //have to find it the hard way + if ( !found ) { + //have to find it the hard way + return null; + } } return orderedProperties; } private static void matchColumnsByProperty(Property property, Map> columnsToProperty) { - if ( property == null ) return; + if ( property == null ) { + return; + } if ( "noop".equals( property.getPropertyAccessorName() ) || "embedded".equals( property.getPropertyAccessorName() ) ) { return; @@ -428,7 +435,8 @@ public class BinderHelper { else { Iterator columnIt = property.getColumnIterator(); while ( columnIt.hasNext() ) { - Object column = columnIt.next(); //can be a Formula so we don't cast + //can be a Formula so we don't cast + Object column = columnIt.next(); //noinspection SuspiciousMethodCalls if ( columnsToProperty.containsKey( column ) ) { columnsToProperty.get( column ).add( property ); @@ -464,7 +472,9 @@ public class BinderHelper { property = associatedClass.getProperty( element ); } else { - if ( !property.isComposite() ) return null; + if ( !property.isComposite() ) { + return null; + } property = ( (Component) property.getValue() ).getProperty( element ); } } @@ -473,7 +483,9 @@ public class BinderHelper { catch (MappingException e) { try { //if we do not find it try to check the identifier mapper - if ( associatedClass.getIdentifierMapper() == null ) return null; + if ( associatedClass.getIdentifierMapper() == null ) { + return null; + } StringTokenizer st = new StringTokenizer( propertyName, ".", false ); while ( st.hasMoreElements() ) { String element = (String) st.nextElement(); @@ -481,7 +493,9 @@ public class BinderHelper { property = associatedClass.getIdentifierMapper().getProperty( element ); } else { - if ( !property.isComposite() ) return null; + if ( !property.isComposite() ) { + return null; + } property = ( (Component) property.getValue() ).getProperty( element ); } } @@ -512,7 +526,9 @@ public class BinderHelper { property = component.getProperty( element ); } else { - if ( !property.isComposite() ) return null; + if ( !property.isComposite() ) { + return null; + } property = ( (Component) property.getValue() ).getProperty( element ); } } @@ -521,7 +537,9 @@ public class BinderHelper { catch (MappingException e) { try { //if we do not find it try to check the identifier mapper - if ( component.getOwner().getIdentifierMapper() == null ) return null; + if ( component.getOwner().getIdentifierMapper() == null ) { + return null; + } StringTokenizer st = new StringTokenizer( propertyName, ".", false ); while ( st.hasMoreElements() ) { String element = (String) st.nextElement(); @@ -529,7 +547,9 @@ public class BinderHelper { property = component.getOwner().getIdentifierMapper().getProperty( element ); } else { - if ( !property.isComposite() ) return null; + if ( !property.isComposite() ) { + return null; + } property = ( (Component) property.getValue() ).getProperty( element ); } } @@ -542,7 +562,9 @@ public class BinderHelper { } public static String getRelativePath(PropertyHolder propertyHolder, String propertyName) { - if ( propertyHolder == null ) return propertyName; + if ( propertyHolder == null ) { + return propertyName; + } String path = propertyHolder.getPath(); String entityName = propertyHolder.getPersistentClass().getEntityName(); if ( path.length() == entityName.length() ) { @@ -562,7 +584,8 @@ public class BinderHelper { String columnName, MetadataBuildingContext context) { if ( StringHelper.isEmpty( columnName ) ) { - return persistentClass; //shortcut for implicit referenced column names + //shortcut for implicit referenced column names + return persistentClass; } PersistentClass current = persistentClass; Object result; @@ -656,7 +679,9 @@ public class BinderHelper { params.setProperty( (String) elt.getKey(), (String) elt.getValue() ); } } - if ( "assigned".equals( generatorType ) ) id.setNullValue( "undefined" ); + if ( "assigned".equals( generatorType ) ) { + id.setNullValue( "undefined" ); + } id.setIdentifierGeneratorProperties( params ); } @@ -679,6 +704,10 @@ public class BinderHelper { //equivalent to (but faster) ANNOTATION_STRING_DEFAULT.equals( annotationString ); } + public static boolean isEmptyOrNullAnnotationValue(String annotationString) { + return annotationString == null || annotationString.length() == 0; + } + public static Any buildAnyValue( String anyMetaDefName, Ejb3JoinColumn[] columns, @@ -722,7 +751,9 @@ public class BinderHelper { throw new MappingException( "could not interpret metaValue", e ); } } - if ( !values.isEmpty() ) value.setMetaValues( values ); + if ( !values.isEmpty() ) { + value.setMetaValues( values ); + } } else { throw new AnnotationException( "Unable to find @AnyMetaDef for an @(ManyTo)Any mapping: " @@ -792,7 +823,8 @@ public class BinderHelper { private static void bindAnyMetaDef(AnyMetaDef defAnn, MetadataBuildingContext context) { if ( isEmptyAnnotationValue( defAnn.name() ) ) { - return; //don't map not named definitions + //don't map not named definitions + return; } if ( LOG.isDebugEnabled() ) { LOG.debugf( "Binding Any Meta definition: %s", defAnn.name() ); @@ -863,9 +895,9 @@ public class BinderHelper { public static Map toAliasTableMap(SqlFragmentAlias[] aliases){ Map ret = new HashMap(); - for (int i = 0; i < aliases.length; i++){ - if (StringHelper.isNotEmpty(aliases[i].table())){ - ret.put(aliases[i].alias(), aliases[i].table()); + for ( int i = 0; i < aliases.length; i++ ){ + if ( StringHelper.isNotEmpty( aliases[i].table() ) ){ + ret.put( aliases[i].alias(), aliases[i].table() ); } } return ret; @@ -875,7 +907,7 @@ public class BinderHelper { Map ret = new HashMap(); for (int i = 0; i < aliases.length; i++){ if (aliases[i].entity() != void.class){ - ret.put(aliases[i].alias(), aliases[i].entity().getName()); + ret.put( aliases[i].alias(), aliases[i].entity().getName() ); } } return ret; diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/TableBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/TableBinder.java index 87391077c0..8c3ada12a4 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/TableBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/TableBinder.java @@ -53,7 +53,7 @@ import org.jboss.logging.Logger; @SuppressWarnings("unchecked") public class TableBinder { //TODO move it to a getter/setter strategy - private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, TableBinder.class.getName()); + private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class, TableBinder.class.getName() ); MetadataBuildingContext buildingContext; @@ -487,10 +487,10 @@ public class TableBinder { MetadataBuildingContext buildingContext, String subselect, InFlightMetadataCollector.EntityTableXref denormalizedSuperTableXref) { - schema = BinderHelper.isEmptyAnnotationValue( schema ) + schema = BinderHelper.isEmptyOrNullAnnotationValue( schema ) ? extract( buildingContext.getMetadataCollector().getDatabase().getDefaultNamespace().getPhysicalName().getSchema() ) : schema; - catalog = BinderHelper.isEmptyAnnotationValue( catalog ) + catalog = BinderHelper.isEmptyOrNullAnnotationValue( catalog ) ? extract( buildingContext.getMetadataCollector().getDatabase().getDefaultNamespace().getPhysicalName().getCatalog() ) : catalog; @@ -668,7 +668,8 @@ public class TableBinder { //explicit referencedColumnName Iterator idColItr = referencedEntity.getKey().getColumnIterator(); org.hibernate.mapping.Column col; - Table table = referencedEntity.getTable(); //works cause the pk has to be on the primary table + //works cause the pk has to be on the primary table + Table table = referencedEntity.getTable(); if ( !idColItr.hasNext() ) { LOG.debug( "No column in the identifier!" ); }