Skip to content

Commit 04b8d80

Browse files
authored
HHH-18492 add Hibernate.isEmpty() for pure convenience
HHH-18492 add Hibernate.isEmpty() for pure convenience Signed-off-by: Gavin King <[email protected]>
1 parent 349b209 commit 04b8d80

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

hibernate-core/src/main/java/org/hibernate/Hibernate.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,21 @@ public static int size(Collection<?> collection) {
190190
: collection.size();
191191
}
192192

193+
/**
194+
* Determine is the given persistent collection is {@linkplain Collection#isEmpty() empty},
195+
* without fetching its state from the database.
196+
*
197+
* @param collection a persistent collection associated with an open session
198+
* @return {@code true} if the collection is empty
199+
*
200+
* @since 7.0
201+
*/
202+
public static boolean isEmpty(Collection<?> collection) {
203+
return collection instanceof PersistentCollection
204+
? ((PersistentCollection<?>) collection).getSize() == 0
205+
: collection.isEmpty();
206+
}
207+
193208
/**
194209
* Determine if the given persistent collection {@linkplain Collection#contains(Object) contains}
195210
* the given element, without fetching its state from the database.

hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,22 +175,24 @@ protected boolean readSize() {
175175
return false;
176176
}
177177

178-
public int getSize() {
178+
public int getSize() {
179179
if ( cachedSize>=0 ) {
180180
return cachedSize;
181181
}
182-
final CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry( this );
183-
if ( entry == null ) {
184-
throwLazyInitializationExceptionIfNotConnected();
185-
throwLazyInitializationException("collection not associated with session");
186-
throw new AssertionFailure("impossible");
187-
}
188182
else {
189-
if ( hasQueuedOperations() ) {
190-
session.flush();
183+
final CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry( this );
184+
if ( entry == null ) {
185+
throwLazyInitializationExceptionIfNotConnected();
186+
throwLazyInitializationException("collection not associated with session");
187+
throw new AssertionFailure("impossible");
188+
}
189+
else {
190+
if ( hasQueuedOperations() ) {
191+
session.flush();
192+
}
193+
cachedSize = entry.getLoadedPersister().getSize( entry.getLoadedKey(), session );
194+
return cachedSize;
191195
}
192-
cachedSize = entry.getLoadedPersister().getSize( entry.getLoadedKey(), session );
193-
return cachedSize;
194196
}
195197
}
196198

hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,9 +1383,7 @@ private void initCollectionPropertyMap(String aliasName, Type type, String[] col
13831383
public int getSize(Object key, SharedSessionContractImplementor session) {
13841384
try {
13851385
final JdbcCoordinator jdbcCoordinator = session.getJdbcCoordinator();
1386-
PreparedStatement st = jdbcCoordinator
1387-
.getStatementPreparer()
1388-
.prepareStatement( sqlSelectSizeString );
1386+
final PreparedStatement st = jdbcCoordinator.getStatementPreparer().prepareStatement( sqlSelectSizeString );
13891387
try {
13901388
getKeyType().nullSafeSet( st, key, 1, session );
13911389
ResultSet rs = jdbcCoordinator.getResultSetReturn().extract( st, sqlSelectSizeString );

0 commit comments

Comments
 (0)