Skip to content

Commit 4fd4e37

Browse files
committed
use isBlank() instead of isEmpty() to better handle annotation values
1 parent f9bdf86 commit 4fd4e37

File tree

5 files changed

+68
-51
lines changed

5 files changed

+68
-51
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/AnnotatedColumn.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@
4646
import static org.hibernate.boot.model.internal.BinderHelper.getPath;
4747
import static org.hibernate.boot.model.internal.BinderHelper.getRelativePath;
4848
import static org.hibernate.boot.model.internal.DialectOverridesAnnotationHelper.getOverridableAnnotation;
49+
import static org.hibernate.internal.util.StringHelper.isBlank;
4950
import static org.hibernate.internal.util.StringHelper.isEmpty;
5051
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
52+
import static org.hibernate.internal.util.StringHelper.nullIfBlank;
5153
import static org.hibernate.internal.util.StringHelper.nullIfEmpty;
54+
import static org.hibernate.internal.util.collections.CollectionHelper.isNotEmpty;
5255

5356
/**
5457
* A mapping to a column, logically representing a
@@ -896,7 +899,7 @@ private void applyColumnCheckConstraint(jakarta.persistence.Column column) {
896899
}
897900

898901
void applyCheckConstraints(jakarta.persistence.CheckConstraint[] checkConstraintAnnotationUsages) {
899-
if ( CollectionHelper.isNotEmpty( checkConstraintAnnotationUsages ) ) {
902+
if ( isNotEmpty( checkConstraintAnnotationUsages ) ) {
900903
for ( jakarta.persistence.CheckConstraint checkConstraintAnnotationUsage : checkConstraintAnnotationUsages ) {
901904
addCheckConstraint(
902905
checkConstraintAnnotationUsage.name(),
@@ -959,10 +962,10 @@ private void processColumnTransformerExpressions(ColumnTransformer annotation) {
959962
}
960963

961964
final String targetColumnName = annotation.forColumn();
962-
if ( isEmpty( targetColumnName )
965+
if ( isBlank( targetColumnName )
963966
|| targetColumnName.equals( logicalColumnName != null ? logicalColumnName : "" ) ) {
964-
readExpression = nullIfEmpty( annotation.read() );
965-
writeExpression = nullIfEmpty( annotation.write() );
967+
readExpression = nullIfBlank( annotation.read() );
968+
writeExpression = nullIfBlank( annotation.write() );
966969
}
967970
}
968971

@@ -1016,7 +1019,7 @@ private static AnnotatedColumns buildImplicitColumn(
10161019

10171020
@Override
10181021
public String toString() {
1019-
StringBuilder string = new StringBuilder();
1022+
final StringBuilder string = new StringBuilder();
10201023
string.append( getClass().getSimpleName() ).append( "(" );
10211024
if ( isNotEmpty( logicalColumnName ) ) {
10221025
string.append( "column='" ).append( logicalColumnName ).append( "'," );

hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,12 @@
172172
import static org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle.fromResultCheckStyle;
173173
import static org.hibernate.internal.util.ReflectHelper.getDefaultSupplier;
174174
import static org.hibernate.internal.util.StringHelper.getNonEmptyOrConjunctionIfBothNonEmpty;
175-
import static org.hibernate.internal.util.StringHelper.isEmpty;
176-
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
175+
import static org.hibernate.internal.util.StringHelper.isBlank;
176+
import static org.hibernate.internal.util.StringHelper.isNotBlank;
177177
import static org.hibernate.internal.util.StringHelper.nullIfEmpty;
178178
import static org.hibernate.internal.util.StringHelper.qualify;
179179
import static org.hibernate.internal.util.collections.CollectionHelper.isEmpty;
180+
import static org.hibernate.internal.util.collections.CollectionHelper.isNotEmpty;
180181
import static org.hibernate.mapping.MappingHelper.createLocalUserCollectionTypeBean;
181182

182183
/**
@@ -459,7 +460,7 @@ && isToManyAssociationWithinEmbeddableCollection( propertyHolder ) ) {
459460

460461
if ( property.hasDirectAnnotationUsage( OrderColumn.class )
461462
&& manyToMany != null
462-
&& StringHelper.isNotEmpty( manyToMany.mappedBy() ) ) {
463+
&& isNotBlank( manyToMany.mappedBy() ) ) {
463464
throw new AnnotationException("Collection '" + getPath( propertyHolder, inferredData ) +
464465
"' is the unowned side of a bidirectional '@ManyToMany' and may not have an '@OrderColumn'");
465466
}
@@ -740,7 +741,7 @@ private static void bindJoinedTableAssociation(
740741
}
741742

742743
collectionBinder.setExplicitAssociationTable( true );
743-
if ( CollectionHelper.isNotEmpty( jpaIndexes ) ) {
744+
if ( isNotEmpty( jpaIndexes ) ) {
744745
associationTableBinder.setJpaIndex( jpaIndexes );
745746
}
746747
if ( !schema.isEmpty() ) {
@@ -910,7 +911,7 @@ private static ManagedBean<? extends UserCollectionType> createCustomType(
910911
Class<? extends UserCollectionType> implementation,
911912
Map<String,String> parameters,
912913
MetadataBuildingContext buildingContext) {
913-
final boolean hasParameters = CollectionHelper.isNotEmpty( parameters );
914+
final boolean hasParameters = isNotEmpty( parameters );
914915
if ( !buildingContext.getBuildingOptions().isAllowExtensionsInCdi() ) {
915916
// if deferred container access is enabled, we locally create the user-type
916917
return createLocalUserCollectionTypeBean( role, implementation, hasParameters, parameters );
@@ -1094,13 +1095,13 @@ private static CollectionClassification determineCollectionClassification(
10941095

10951096
final SourceModelBuildingContext sourceModelContext = buildingContext.getMetadataCollector().getSourceModelBuildingContext();
10961097
final ManyToMany manyToMany = property.getAnnotationUsage( ManyToMany.class, sourceModelContext );
1097-
if ( manyToMany != null && !manyToMany.mappedBy().isEmpty() ) {
1098+
if ( manyToMany != null && !manyToMany.mappedBy().isBlank() ) {
10981099
// We don't support @OrderColumn on the non-owning side of a many-to-many association.
10991100
return CollectionClassification.BAG;
11001101
}
11011102

11021103
final OneToMany oneToMany = property.getAnnotationUsage( OneToMany.class, sourceModelContext );
1103-
if ( oneToMany != null && !oneToMany.mappedBy().isEmpty() ) {
1104+
if ( oneToMany != null && !oneToMany.mappedBy().isBlank() ) {
11041105
// Unowned to-many mappings are always considered BAG by default
11051106
return CollectionClassification.BAG;
11061107
}
@@ -1268,7 +1269,7 @@ private void bindOptimisticLock(boolean isMappedBy) {
12681269

12691270
private void bindCache() {
12701271
//set cache
1271-
if ( isNotEmpty( cacheConcurrencyStrategy ) ) {
1272+
if ( isNotBlank( cacheConcurrencyStrategy ) ) {
12721273
collection.setCacheConcurrencyStrategy( cacheConcurrencyStrategy );
12731274
collection.setCacheRegionName( cacheRegionName );
12741275
}
@@ -1780,7 +1781,7 @@ private void handleJpaOrderBy(Collection collection, PersistentClass associatedC
17801781
final String hqlOrderBy = extractHqlOrderBy( jpaOrderBy );
17811782
if ( hqlOrderBy != null ) {
17821783
final String orderByFragment = buildOrderByClauseFromHql( hqlOrderBy, associatedClass );
1783-
if ( isNotEmpty( orderByFragment ) ) {
1784+
if ( isNotBlank( orderByFragment ) ) {
17841785
collection.setOrderBy( orderByFragment );
17851786
}
17861787
}
@@ -1824,7 +1825,7 @@ private void addFilter(boolean hasAssociationTable, Filter filterAnnotation) {
18241825
final String alias = aliasAnnotation.alias();
18251826

18261827
final String table = aliasAnnotation.table();
1827-
if ( isNotEmpty( table ) ) {
1828+
if ( isNotBlank( table ) ) {
18281829
aliasTableMap.put( alias, table );
18291830
}
18301831

@@ -1870,7 +1871,7 @@ private void handleWhere(boolean hasAssociationTable) {
18701871
}
18711872

18721873
final String whereJoinTableClause = getWhereJoinTableClause();
1873-
if ( isNotEmpty( whereJoinTableClause ) ) {
1874+
if ( isNotBlank( whereJoinTableClause ) ) {
18741875
if ( hasAssociationTable ) {
18751876
// This is a many-to-many association.
18761877
// Collection#setWhere is used to set the "where" clause that applies to the collection table
@@ -1926,7 +1927,7 @@ private void addFilterJoinTable(boolean hasAssociationTable, FilterJoinTable fil
19261927
final String alias = aliasAnnotation.alias();
19271928

19281929
final String table = aliasAnnotation.table();
1929-
if ( isNotEmpty( table ) ) {
1930+
if ( isNotBlank( table ) ) {
19301931
aliasTableMap.put( alias, table );
19311932
}
19321933

@@ -1952,14 +1953,14 @@ private void addFilterJoinTable(boolean hasAssociationTable, FilterJoinTable fil
19521953

19531954
private String getFilterConditionForJoinTable(FilterJoinTable filterJoinTableAnnotation) {
19541955
final String condition = filterJoinTableAnnotation.condition();
1955-
return condition.isEmpty()
1956+
return condition.isBlank()
19561957
? getDefaultFilterCondition( filterJoinTableAnnotation.name(), filterJoinTableAnnotation )
19571958
: condition;
19581959
}
19591960

19601961
private String getFilterCondition(Filter filter) {
19611962
final String condition = filter.condition();
1962-
return condition.isEmpty()
1963+
return condition.isBlank()
19631964
? getDefaultFilterCondition( filter.name(), filter )
19641965
: condition;
19651966
}
@@ -1972,7 +1973,7 @@ private String getDefaultFilterCondition(String name, Annotation annotation) {
19721973
+ "' for an undefined filter named '" + name + "'" );
19731974
}
19741975
final String defaultCondition = definition.getDefaultFilterCondition();
1975-
if ( isEmpty( defaultCondition ) ) {
1976+
if ( isBlank( defaultCondition ) ) {
19761977
throw new AnnotationException( "Collection '" + qualify( propertyHolder.getPath(), propertyName ) +
19771978
"' has a '@" + annotation.annotationType().getSimpleName()
19781979
+ "' with no 'condition' and no default condition was given by the '@FilterDef' named '"
@@ -2016,7 +2017,7 @@ private static String buildOrderByClauseFromHql(String orderByFragment, Persiste
20162017
if ( orderByFragment == null ) {
20172018
return null;
20182019
}
2019-
else if ( orderByFragment.isEmpty() ) {
2020+
else if ( orderByFragment.isBlank() ) {
20202021
//order by id
20212022
return buildOrderById( associatedClass, " asc" );
20222023
}
@@ -2042,7 +2043,7 @@ private static String buildOrderById(PersistentClass associatedClass, String ord
20422043
public static String adjustUserSuppliedValueCollectionOrderingFragment(String orderByFragment) {
20432044
if ( orderByFragment != null ) {
20442045
orderByFragment = orderByFragment.trim();
2045-
if ( orderByFragment.isEmpty() || orderByFragment.equalsIgnoreCase( "asc" ) ) {
2046+
if ( orderByFragment.isBlank() || orderByFragment.equalsIgnoreCase( "asc" ) ) {
20462047
// This indicates something like either:
20472048
// `@OrderBy()`
20482049
// `@OrderBy("asc")
@@ -2124,7 +2125,7 @@ private DependantValue buildCollectionKey(AnnotatedJoinColumns joinColumns, OnDe
21242125
if ( !ArrayHelper.isEmpty( joinColumnAnnotations ) ) {
21252126
final JoinColumn joinColumnAnn = joinColumnAnnotations[0];
21262127
final ForeignKey joinColumnForeignKey = joinColumnAnn.foreignKey();
2127-
if ( foreignKeyName.isEmpty() ) {
2128+
if ( foreignKeyName.isBlank() ) {
21282129
foreignKeyName = joinColumnForeignKey.name();
21292130
foreignKeyDefinition = joinColumnForeignKey.foreignKeyDefinition();
21302131
foreignKeyOptions = joinColumnForeignKey.options();
@@ -2153,7 +2154,7 @@ private DependantValue buildCollectionKey(AnnotatedJoinColumns joinColumns, OnDe
21532154
final OneToMany oneToManyAnn = property.getDirectAnnotationUsage( OneToMany.class );
21542155
final OnDelete onDeleteAnn = property.getDirectAnnotationUsage( OnDelete.class );
21552156
if ( oneToManyAnn != null
2156-
&& !oneToManyAnn.mappedBy().isEmpty()
2157+
&& !oneToManyAnn.mappedBy().isBlank()
21572158
&& ( onDeleteAnn == null || onDeleteAnn.action() != OnDeleteAction.CASCADE ) ) {
21582159
// foreign key should be up to @ManyToOne side
21592160
// @OnDelete generate "on delete cascade" foreign key
@@ -2342,7 +2343,7 @@ private void handleCompositeCollectionElement(
23422343
inheritanceStatePerClass
23432344
);
23442345
collection.setElement( component );
2345-
if ( isNotEmpty( hqlOrderBy ) ) {
2346+
if ( isNotBlank( hqlOrderBy ) ) {
23462347
final String orderBy = adjustUserSuppliedValueCollectionOrderingFragment( hqlOrderBy );
23472348
if ( orderBy != null ) {
23482349
collection.setOrderBy( orderBy );
@@ -2544,7 +2545,7 @@ private void handleOwnedManyToMany(PersistentClass collectionEntity, boolean isC
25442545
collector.getLogicalTableName( owner.getTable() ),
25452546
collector.getFromMappedBy( owner.getEntityName(), joinColumns.getPropertyName() )
25462547
);
2547-
if ( isEmpty( tableBinder.getName() ) ) {
2548+
if ( isBlank( tableBinder.getName() ) ) {
25482549
//default value
25492550
tableBinder.setDefaultName(
25502551
owner.getClassName(),
@@ -2581,7 +2582,7 @@ private void handleCheckConstraints(Table collectionTable) {
25812582
private static void addCheckToCollection(Table collectionTable, Check check) {
25822583
final String name = check.name();
25832584
final String constraint = check.constraints();
2584-
collectionTable.addCheck( name.isEmpty()
2585+
collectionTable.addCheck( name.isBlank()
25852586
? new CheckConstraint( constraint )
25862587
: new CheckConstraint( name, constraint ) );
25872588
}
@@ -2742,7 +2743,7 @@ private static String extractHqlOrderBy(OrderBy jpaOrderBy) {
27422743

27432744
private static void checkFilterConditions(Collection collection) {
27442745
//for now it can't happen, but sometime soon...
2745-
if ( ( !collection.getFilters().isEmpty() || isNotEmpty( collection.getWhere() ) )
2746+
if ( ( !collection.getFilters().isEmpty() || isNotBlank( collection.getWhere() ) )
27462747
&& collection.getFetchMode() == FetchMode.JOIN
27472748
&& !( collection.getElement() instanceof SimpleValue ) //SimpleValue (CollectionOfElements) are always SELECT but it does not matter
27482749
&& collection.getElement().getFetchMode() != FetchMode.JOIN ) {

hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,12 @@
155155
import static org.hibernate.engine.OptimisticLockStyle.fromLockType;
156156
import static org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle.fromResultCheckStyle;
157157
import static org.hibernate.internal.util.ReflectHelper.getDefaultSupplier;
158+
import static org.hibernate.internal.util.StringHelper.isBlank;
158159
import static org.hibernate.internal.util.StringHelper.isEmpty;
159-
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
160+
import static org.hibernate.internal.util.StringHelper.isNotBlank;
160161
import static org.hibernate.internal.util.StringHelper.nullIfEmpty;
161162
import static org.hibernate.internal.util.StringHelper.unqualify;
163+
import static org.hibernate.internal.util.collections.CollectionHelper.isEmpty;
162164
import static org.hibernate.internal.util.collections.CollectionHelper.isNotEmpty;
163165

164166

@@ -367,7 +369,7 @@ private void handleCheckConstraints() {
367369
private void addCheckToEntity(Check check) {
368370
final String name = check.name();
369371
final String constraint = check.constraints();
370-
persistentClass.addCheckConstraint( name.isEmpty()
372+
persistentClass.addCheckConstraint( name.isBlank()
371373
? new CheckConstraint( constraint )
372374
: new CheckConstraint( name, constraint ) );
373375
}
@@ -1300,7 +1302,7 @@ private void bindEntityAnnotation() {
13001302
throw new AssertionFailure( "@Entity should never be missing" );
13011303
}
13021304
final String entityName = entity.name();
1303-
name = entityName.isEmpty() ? unqualify( annotatedClass.getName() ) : entityName;
1305+
name = entityName.isBlank() ? unqualify( annotatedClass.getName() ) : entityName;
13041306
}
13051307

13061308
public boolean isRootEntity() {
@@ -1351,7 +1353,7 @@ private void checkSubclassEntity() {
13511353
+ "' is annotated '@Immutable' but it is a subclass in an entity inheritance hierarchy"
13521354
+ " (only a root class may declare its mutability)" );
13531355
}
1354-
if ( isNotEmpty( where ) ) {
1356+
if ( isNotBlank( where ) ) {
13551357
throw new AnnotationException( "Entity class '" + annotatedClass.getName()
13561358
+ "' specifies an '@SQLRestriction' but it is a subclass in an entity inheritance hierarchy"
13571359
+ " (only a root class may be specify a restriction)" );
@@ -1386,7 +1388,7 @@ private void registerImportName() {
13861388
private void bindRootEntity() {
13871389
final RootClass rootClass = (RootClass) persistentClass;
13881390
rootClass.setMutable( isMutable() );
1389-
if ( isNotEmpty( where ) ) {
1391+
if ( isNotBlank( where ) ) {
13901392
rootClass.setWhere( where );
13911393
}
13921394
if ( cacheConcurrentStrategy != null ) {
@@ -1498,11 +1500,11 @@ private <A extends Annotation> A resolveCustomSqlAnnotation(
14981500
}
14991501

15001502
final A override = dialectOverride.override();
1501-
if ( isEmpty( tableName )
1503+
if ( isBlank( tableName )
15021504
&& isEmpty( ( (CustomSqlDetails) override ).table() ) ) {
15031505
return override;
15041506
}
1505-
else if ( isNotEmpty( tableName )
1507+
else if ( isNotBlank( tableName )
15061508
&& tableName.equals( ( (CustomSqlDetails) override ).table() ) ) {
15071509
return override;
15081510
}
@@ -1516,7 +1518,7 @@ private void bindFilters() {
15161518
for ( Filter filter : filters ) {
15171519
final String filterName = filter.name();
15181520
String condition = filter.condition();
1519-
if ( condition.isEmpty() ) {
1521+
if ( condition.isBlank() ) {
15201522
condition = getDefaultFilterCondition( filterName );
15211523
}
15221524
persistentClass.addFilter(
@@ -1536,7 +1538,7 @@ private String getDefaultFilterCondition(String filterName) {
15361538
+ "' has a '@Filter' for an undefined filter named '" + filterName + "'" );
15371539
}
15381540
final String condition = definition.getDefaultFilterCondition();
1539-
if ( isEmpty( condition ) ) {
1541+
if ( isBlank( condition ) ) {
15401542
throw new AnnotationException( "Entity '" + name +
15411543
"' has a '@Filter' with no 'condition' and no default condition was given by the '@FilterDef' named '"
15421544
+ filterName + "'" );
@@ -1587,7 +1589,7 @@ public void bindDiscriminatorValue() {
15871589
final String discriminatorValue = discriminatorValueAnn != null
15881590
? discriminatorValueAnn.value()
15891591
: null;
1590-
if ( isEmpty( discriminatorValue ) ) {
1592+
if ( isBlank( discriminatorValue ) ) {
15911593
final Value discriminator = persistentClass.getDiscriminator();
15921594
if ( discriminator == null ) {
15931595
persistentClass.setDiscriminatorValue( name );
@@ -1646,12 +1648,13 @@ private void bindNaturalIdCache() {
16461648
}
16471649

16481650
final String region = naturalIdCacheAnn.region();
1649-
if ( region.isEmpty() ) {
1651+
if ( region.isBlank() ) {
16501652
final Cache explicitCacheAnn = annotatedClass.getAnnotationUsage( Cache.class, getSourceModelContext() );
16511653

1652-
naturalIdCacheRegion = explicitCacheAnn != null && isNotEmpty( explicitCacheAnn.region() )
1653-
? explicitCacheAnn.region() + NATURAL_ID_CACHE_SUFFIX
1654-
: annotatedClass.getName() + NATURAL_ID_CACHE_SUFFIX;
1654+
naturalIdCacheRegion =
1655+
explicitCacheAnn != null && isNotBlank( explicitCacheAnn.region() )
1656+
? explicitCacheAnn.region() + NATURAL_ID_CACHE_SUFFIX
1657+
: annotatedClass.getName() + NATURAL_ID_CACHE_SUFFIX;
16551658
}
16561659
else {
16571660
naturalIdCacheRegion = naturalIdCacheAnn.region();
@@ -1853,7 +1856,7 @@ public void bindTable(
18531856
final EntityTableNamingStrategyHelper namingStrategyHelper =
18541857
new EntityTableNamingStrategyHelper( persistentClass.getClassName(), entityName, name );
18551858
final Identifier logicalName =
1856-
isNotEmpty( tableName )
1859+
isNotBlank( tableName )
18571860
? namingStrategyHelper.handleExplicitName( tableName, context )
18581861
: namingStrategyHelper.determineImplicitName( context );
18591862

@@ -1917,7 +1920,7 @@ private void createPrimaryColumnsToSecondaryTable(
19171920
final Annotation[] joinColumnSource = (Annotation[]) incoming;
19181921
final AnnotatedJoinColumns annotatedJoinColumns;
19191922

1920-
if ( CollectionHelper.isEmpty( joinColumnSource ) ) {
1923+
if ( isEmpty( joinColumnSource ) ) {
19211924
annotatedJoinColumns = createDefaultJoinColumn( propertyHolder );
19221925
}
19231926
else {
@@ -2047,7 +2050,7 @@ private SecondaryTable findMatchingSecondaryTable(Join join) {
20472050

20482051
private SecondaryRow findMatchingSecondaryRowAnnotation(String tableName) {
20492052
final SecondaryRow row = annotatedClass.getDirectAnnotationUsage( SecondaryRow.class );
2050-
if ( row != null && ( row.table().isEmpty() || tableName.equals( row.table() ) ) ) {
2053+
if ( row != null && ( row.table().isBlank() || tableName.equals( row.table() ) ) ) {
20512054
return row;
20522055
}
20532056
else {

0 commit comments

Comments
 (0)