Skip to content

Commit 0d6a883

Browse files
committed
very minor code improvements in ForeignKeys + NonNullableTransientDependencies
1 parent e320299 commit 0d6a883

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

hibernate-core/src/main/java/org/hibernate/engine/internal/ForeignKeys.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void nullifyTransientReferences(final Object[] values) {
8989
* @return {@code null} if the argument is an unsaved entity; otherwise return the argument.
9090
*/
9191
private Object nullifyTransientReferences(final Object value, final String propertyName, final Type type) {
92-
final Object returnedValue = nullifyTransient(value, propertyName, type);
92+
final Object returnedValue = nullifyTransient( value, propertyName, type );
9393
// value != returnedValue if either:
9494
// 1) returnedValue was nullified (set to null);
9595
// or 2) returnedValue was initialized, but not nullified.
@@ -149,15 +149,15 @@ private Object nullifyEntityType(Object value, String propertyName, EntityType e
149149
else {
150150
// If value is lazy, it may need to be initialized to
151151
// determine if the value is nullifiable.
152-
final Object possiblyInitializedValue = initializeIfNecessary(value, propertyName, entityType );
152+
final Object possiblyInitializedValue = initializeIfNecessary( value, propertyName, entityType );
153153
if ( possiblyInitializedValue == null ) {
154154
// The uninitialized value was initialized to null
155155
return null;
156156
}
157157
else {
158158
// If the value is not nullifiable, make sure that the
159159
// possibly initialized value is returned.
160-
return isNullifiable(entityType.getAssociatedEntityName(), possiblyInitializedValue)
160+
return isNullifiable( entityType.getAssociatedEntityName(), possiblyInitializedValue )
161161
? null
162162
: possiblyInitializedValue;
163163
}
@@ -199,15 +199,15 @@ private boolean initializationIsNecessary(Object value, Type type) {
199199

200200
/**
201201
* Determine if the object already exists in the database,
202-
* using a "best guess"
202+
* using heuristics.
203203
*
204204
* @param entityName The name of the entity
205205
* @param object The entity instance
206206
*/
207207
private boolean isNullifiable(final String entityName, Object object)
208208
throws HibernateException {
209209
if ( object == UNFETCHED_PROPERTY ) {
210-
// this is the best we can do...
210+
// this is the best we can do
211211
return false;
212212
}
213213

@@ -244,10 +244,10 @@ private boolean isNullifiable(final String entityName, Object object)
244244
|| isDelete && hasSelfReferentialForeignKeyBug();
245245
}
246246

247-
// See if the entity is already bound to this session, if not look at the
248-
// entity identifier and assume that the entity is persistent if the
249-
// id is not "unsaved" (that is, we rely on foreign keys to keep
250-
// database integrity)
247+
// See if the entity is already bound to this session;
248+
// if it's not, look at the entity identifier and assume
249+
// that the entity is persistent if the id is not "unsaved"
250+
// (that is, we rely on foreign keys to keep database integrity)
251251

252252
final EntityEntry entityEntry = persistenceContext.getEntry( object );
253253
return entityEntry == null
@@ -416,11 +416,11 @@ public static NonNullableTransientDependencies findNonNullableTransientEntities(
416416
boolean isEarlyInsert,
417417
SharedSessionContractImplementor session) {
418418
final EntityPersister persister = session.getEntityPersister( entityName, entity );
419-
final Nullifier nullifier = new Nullifier( entity, false, isEarlyInsert, session, persister );
419+
final var nullifier = new Nullifier( entity, false, isEarlyInsert, session, persister );
420420
final String[] propertyNames = persister.getPropertyNames();
421421
final Type[] types = persister.getPropertyTypes();
422422
final boolean[] nullability = persister.getPropertyNullability();
423-
final NonNullableTransientDependencies nonNullableTransientEntities = new NonNullableTransientDependencies();
423+
final var nonNullableTransientEntities = new NonNullableTransientDependencies();
424424
for ( int i = 0; i < types.length; i++ ) {
425425
collectNonNullableTransientEntities(
426426
nullifier,

hibernate-core/src/main/java/org/hibernate/engine/internal/NonNullableTransientDependencies.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
*/
55
package org.hibernate.engine.internal;
66

7-
import java.util.Collections;
87
import java.util.HashSet;
98
import java.util.IdentityHashMap;
109
import java.util.Map;
1110
import java.util.Set;
1211

1312
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1413

14+
import static java.util.Collections.emptyList;
15+
1516
/**
1617
* Tracks non-nullable transient entities that would cause a particular entity insert to fail.
1718
*
@@ -23,6 +24,10 @@ public final class NonNullableTransientDependencies {
2324
private Map<Object,Set<String>> propertyPathsByTransientEntity; // lazily initialized
2425

2526
public void add(String propertyName, Object transientEntity) {
27+
getPropertyPaths( transientEntity ).add( propertyName );
28+
}
29+
30+
private Set<String> getPropertyPaths(Object transientEntity) {
2631
if ( propertyPathsByTransientEntity == null ) {
2732
propertyPathsByTransientEntity = new IdentityHashMap<>();
2833
}
@@ -31,16 +36,13 @@ public void add(String propertyName, Object transientEntity) {
3136
propertyPaths = new HashSet<>();
3237
propertyPathsByTransientEntity.put( transientEntity, propertyPaths );
3338
}
34-
propertyPaths.add( propertyName );
39+
return propertyPaths;
3540
}
3641

3742
public Iterable<Object> getNonNullableTransientEntities() {
38-
if ( propertyPathsByTransientEntity == null ) {
39-
return Collections.emptyList();
40-
}
41-
else {
42-
return propertyPathsByTransientEntity.keySet();
43-
}
43+
return propertyPathsByTransientEntity == null
44+
? emptyList()
45+
: propertyPathsByTransientEntity.keySet();
4446
}
4547

4648
/**
@@ -51,12 +53,9 @@ public Iterable<Object> getNonNullableTransientEntities() {
5153
* @return The property paths
5254
*/
5355
public Iterable<String> getNonNullableTransientPropertyPaths(final Object entity) {
54-
if ( propertyPathsByTransientEntity == null ) {
55-
return Collections.emptyList();
56-
}
57-
else {
58-
return propertyPathsByTransientEntity.get( entity );
59-
}
56+
return propertyPathsByTransientEntity == null
57+
? emptyList()
58+
: propertyPathsByTransientEntity.get( entity );
6059
}
6160

6261
/**
@@ -77,8 +76,9 @@ public boolean isEmpty() {
7776
* @throws IllegalStateException If the entity had tracked paths
7877
*/
7978
public void resolveNonNullableTransientEntity(Object entity) {
80-
if ( propertyPathsByTransientEntity != null && propertyPathsByTransientEntity.remove( entity ) == null ) {
81-
throw new IllegalStateException( "Attempt to resolve a non-nullable, transient entity that is not a dependency." );
79+
if ( propertyPathsByTransientEntity != null
80+
&& propertyPathsByTransientEntity.remove( entity ) == null ) {
81+
throw new IllegalStateException( "Attempt to resolve a non-nullable, transient entity that is not a dependency" );
8282
}
8383
}
8484

@@ -90,14 +90,17 @@ public void resolveNonNullableTransientEntity(Object entity) {
9090
* @return The loggable representation
9191
*/
9292
public String toLoggableString(SharedSessionContractImplementor session) {
93-
final StringBuilder sb = new StringBuilder( getClass().getSimpleName() ).append( '[' );
93+
final StringBuilder result =
94+
new StringBuilder( getClass().getSimpleName() ).append( '[' );
9495
if ( propertyPathsByTransientEntity != null ) {
95-
for ( Map.Entry<Object,Set<String>> entry : propertyPathsByTransientEntity.entrySet() ) {
96-
sb.append( "transientEntityName=" ).append( session.bestGuessEntityName( entry.getKey() ) );
97-
sb.append( " requiredBy=" ).append( entry.getValue() );
96+
for ( var entry : propertyPathsByTransientEntity.entrySet() ) {
97+
result.append( "transientEntityName=" )
98+
.append( session.bestGuessEntityName( entry.getKey() ) );
99+
result.append( " requiredBy=" )
100+
.append( entry.getValue() );
98101
}
99102
}
100-
sb.append( ']' );
101-
return sb.toString();
103+
result.append( ']' );
104+
return result.toString();
102105
}
103106
}

0 commit comments

Comments
 (0)