From b3ea1d67c4ac01ed50096be28552d9d2ae8e9ad5 Mon Sep 17 00:00:00 2001 From: Chris Cranford Date: Wed, 15 Jan 2020 14:40:11 -0500 Subject: [PATCH] HHH-13760 Code formatting changes & suggested logic simplification --- .../mapper/relation/ToOneIdMapper.java | 22 ++++++------------- .../manytoone/lazy/BaseDomainEntity.java | 6 ++--- .../integration/manytoone/lazy/Shipment.java | 2 +- 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/ToOneIdMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/ToOneIdMapper.java index 2d8753375c..8246d3aab7 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/ToOneIdMapper.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/ToOneIdMapper.java @@ -54,23 +54,15 @@ public class ToOneIdMapper extends AbstractToOneMapper { Object oldObj) { final HashMap newData = new HashMap<>(); - Object oldObject = oldObj; - Object newObject = newObj; - if ( lazyMapping ) { - if ( nonInsertableFake && oldObject instanceof HibernateProxy ) { - oldObject = ( (HibernateProxy) oldObject ).getHibernateLazyInitializer().getImplementation(); - } - - - if ( !nonInsertableFake && newObject instanceof HibernateProxy ) { - newObject = ( (HibernateProxy) newObject ).getHibernateLazyInitializer().getImplementation(); - } + // If this property is originally non-insertable, but made insertable because it is in a many-to-one "fake" + // bi-directional relation, we always store the "old", unchanged data, to prevent storing changes made + // to this field. It is the responsibility of the collection to properly update it if it really changed. + Object entity = nonInsertableFake ? oldObj : newObj; + if ( lazyMapping && entity instanceof HibernateProxy ) { + entity = ( (HibernateProxy) entity ).getHibernateLazyInitializer().getImplementation(); } - // If this property is originally non-insertable, but made insertable because it is in a many-to-one "fake" - // bi-directional relation, we always store the "old", unchaged data, to prevent storing changes made - // to this field. It is the responsibility of the collection to properly update it if it really changed. - delegate.mapToMapFromEntity( newData, nonInsertableFake ? oldObject : newObject ); + delegate.mapToMapFromEntity( newData, entity ); for ( Map.Entry entry : newData.entrySet() ) { data.put( entry.getKey(), entry.getValue() ); diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytoone/lazy/BaseDomainEntity.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytoone/lazy/BaseDomainEntity.java index 1d4534836d..61f99b06dc 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytoone/lazy/BaseDomainEntity.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytoone/lazy/BaseDomainEntity.java @@ -39,10 +39,10 @@ public abstract class BaseDomainEntity extends BaseDomainEntityMetadata { @Override public boolean equals(Object o) { - if (this == o) { + if ( this == o ) { return true; } - if (o == null || getClass() != o.getClass()) { + if ( o == null || getClass() != o.getClass() ) { return false; } BaseDomainEntity that = (BaseDomainEntity) o; @@ -51,6 +51,6 @@ public abstract class BaseDomainEntity extends BaseDomainEntityMetadata { @Override public int hashCode() { - return Objects.hash(id); + return Objects.hash( id ); } } diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytoone/lazy/Shipment.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytoone/lazy/Shipment.java index 9344c49bec..2dae856930 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytoone/lazy/Shipment.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/manytoone/lazy/Shipment.java @@ -68,7 +68,7 @@ public class Shipment extends BaseDomainEntity { Shipment(Instant when, String who, Instant dueDate, String identifier, AddressVersion origin, AddressVersion dest) { super( when, who ); this.dueDate = dueDate; - this.identifier = Objects.requireNonNull(identifier ); + this.identifier = Objects.requireNonNull( identifier ); this.origin = origin; this.destination = dest; }