Skip to content

Commit dfbf6df

Browse files
committed
very minor code improvements in ForeignKeys + NonNullableTransientDependencies
1 parent 306fda3 commit dfbf6df

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
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 & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1414

15+
import static java.util.Collections.emptyList;
16+
1517
/**
1618
* Tracks non-nullable transient entities that would cause a particular entity insert to fail.
1719
*
@@ -23,6 +25,10 @@ public final class NonNullableTransientDependencies {
2325
private Map<Object,Set<String>> propertyPathsByTransientEntity; // lazily initialized
2426

2527
public void add(String propertyName, Object transientEntity) {
28+
getPropertyPaths( transientEntity ).add( propertyName );
29+
}
30+
31+
private Set<String> getPropertyPaths(Object transientEntity) {
2632
if ( propertyPathsByTransientEntity == null ) {
2733
propertyPathsByTransientEntity = new IdentityHashMap<>();
2834
}
@@ -31,16 +37,13 @@ public void add(String propertyName, Object transientEntity) {
3137
propertyPaths = new HashSet<>();
3238
propertyPathsByTransientEntity.put( transientEntity, propertyPaths );
3339
}
34-
propertyPaths.add( propertyName );
40+
return propertyPaths;
3541
}
3642

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

4649
/**
@@ -51,12 +54,9 @@ public Iterable<Object> getNonNullableTransientEntities() {
5154
* @return The property paths
5255
*/
5356
public Iterable<String> getNonNullableTransientPropertyPaths(final Object entity) {
54-
if ( propertyPathsByTransientEntity == null ) {
55-
return Collections.emptyList();
56-
}
57-
else {
58-
return propertyPathsByTransientEntity.get( entity );
59-
}
57+
return propertyPathsByTransientEntity == null
58+
? emptyList()
59+
: propertyPathsByTransientEntity.get( entity );
6060
}
6161

6262
/**
@@ -77,8 +77,9 @@ public boolean isEmpty() {
7777
* @throws IllegalStateException If the entity had tracked paths
7878
*/
7979
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." );
80+
if ( propertyPathsByTransientEntity != null
81+
&& propertyPathsByTransientEntity.remove( entity ) == null ) {
82+
throw new IllegalStateException( "Attempt to resolve a non-nullable, transient entity that is not a dependency" );
8283
}
8384
}
8485

@@ -90,14 +91,17 @@ public void resolveNonNullableTransientEntity(Object entity) {
9091
* @return The loggable representation
9192
*/
9293
public String toLoggableString(SharedSessionContractImplementor session) {
93-
final StringBuilder sb = new StringBuilder( getClass().getSimpleName() ).append( '[' );
94+
final StringBuilder result =
95+
new StringBuilder( getClass().getSimpleName() ).append( '[' );
9496
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() );
97+
for ( var entry : propertyPathsByTransientEntity.entrySet() ) {
98+
result.append( "transientEntityName=" )
99+
.append( session.bestGuessEntityName( entry.getKey() ) );
100+
result.append( " requiredBy=" )
101+
.append( entry.getValue() );
98102
}
99103
}
100-
sb.append( ']' );
101-
return sb.toString();
104+
result.append( ']' );
105+
return result.toString();
102106
}
103107
}

0 commit comments

Comments
 (0)