From 4eaa4ddb81ba0cfd73b51d978c637fc16c009c59 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Wed, 6 Nov 2024 16:25:42 +0530 Subject: [PATCH] #813 - fixed equals method --- .../org/avni/server/domain/CHSBaseEntity.java | 23 +++++++++++++------ .../java/org/avni/server/domain/User.java | 16 +++---------- .../server/domain/framework/IdHolder.java | 6 +++++ .../avni/server/domain/CHSBaseEntityTest.java | 23 +++++++++++++++++++ 4 files changed, 48 insertions(+), 20 deletions(-) create mode 100644 avni-server-api/src/main/java/org/avni/server/domain/framework/IdHolder.java create mode 100644 avni-server-api/src/test/java/org/avni/server/domain/CHSBaseEntityTest.java diff --git a/avni-server-api/src/main/java/org/avni/server/domain/CHSBaseEntity.java b/avni-server-api/src/main/java/org/avni/server/domain/CHSBaseEntity.java index 9e924cd43..36b34e9a7 100644 --- a/avni-server-api/src/main/java/org/avni/server/domain/CHSBaseEntity.java +++ b/avni-server-api/src/main/java/org/avni/server/domain/CHSBaseEntity.java @@ -1,6 +1,7 @@ package org.avni.server.domain; import jakarta.validation.constraints.NotNull; +import org.avni.server.domain.framework.IdHolder; import org.hibernate.proxy.HibernateProxy; import org.hibernate.proxy.LazyInitializer; @@ -8,7 +9,7 @@ import java.util.UUID; @MappedSuperclass -public class CHSBaseEntity { +public class CHSBaseEntity implements IdHolder { @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", updatable = false, nullable = false) @Id @@ -59,16 +60,23 @@ public void assignUUIDIfRequired() { @Override public boolean equals(Object o) { - if (this == o) return true; + return CHSBaseEntity.equals(this, o); + } + + public static boolean equals(IdHolder a, Object o) { + if (a == o) return true; - LazyInitializer lazyInitializer1 = HibernateProxy.extractLazyInitializer(this); LazyInitializer lazyInitializer2 = HibernateProxy.extractLazyInitializer(o); - if (o == null || lazyInitializer2 == null || lazyInitializer1 == null || lazyInitializer1.getImplementationClass() != lazyInitializer2.getImplementationClass()) return false; - CHSBaseEntity that = (CHSBaseEntity) o; + if (o == null) return false; + if (lazyInitializer2 != null && a.getClass() != lazyInitializer2.getImplementationClass()) return false; + + IdHolder that = lazyInitializer2 == null ? (IdHolder) o : (IdHolder) lazyInitializer2.getImplementation(); - if (getId() != null ? !getId().equals(that.getId()) : that.getId() != null) return false; - return getUuid() != null ? getUuid().equals(that.getUuid()) : that.getUuid() == null; + if (that.getId() == null && a.getId() == null) return that == a; + + if (a.getId() != null ? !a.getId().equals(that.getId()) : that.getId() != null) return false; + return a.getUuid() != null ? a.getUuid().equals(that.getUuid()) : that.getUuid() == null; } @Override @@ -77,4 +85,5 @@ public int hashCode() { result = 31 * result + (uuid != null ? uuid.hashCode() : 0); return result; } + } diff --git a/avni-server-api/src/main/java/org/avni/server/domain/User.java b/avni-server-api/src/main/java/org/avni/server/domain/User.java index 687d82279..426802148 100644 --- a/avni-server-api/src/main/java/org/avni/server/domain/User.java +++ b/avni-server-api/src/main/java/org/avni/server/domain/User.java @@ -5,6 +5,7 @@ import jakarta.persistence.*; import jakarta.validation.constraints.NotNull; import org.apache.commons.validator.routines.EmailValidator; +import org.avni.server.domain.framework.IdHolder; import org.avni.server.framework.hibernate.JSONObjectUserType; import org.avni.server.util.DateTimeUtil; import org.avni.server.util.ObjectMapperSingleton; @@ -14,8 +15,6 @@ import org.hibernate.annotations.BatchSize; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.Type; -import org.hibernate.proxy.HibernateProxy; -import org.hibernate.proxy.LazyInitializer; import org.joda.time.DateTime; import java.time.Instant; @@ -27,7 +26,7 @@ @BatchSize(size = 100) @Cacheable @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE) -public class User { +public class User implements IdHolder { public static final String DEFAULT_SUPER_ADMIN = "5fed2907-df3a-4867-aef5-c87f4c78a31a"; @Column @@ -238,16 +237,7 @@ public void setUserGroups(List userGroups) { @Override public boolean equals(Object o) { - if (this == o) return true; - - LazyInitializer lazyInitializer1 = HibernateProxy.extractLazyInitializer(this); - LazyInitializer lazyInitializer2 = HibernateProxy.extractLazyInitializer(o); - if (o == null || lazyInitializer2 == null || lazyInitializer1 == null || lazyInitializer1.getImplementationClass() != lazyInitializer2.getImplementationClass()) return false; - - User that = (User) o; - - if (getId() != null ? !getId().equals(that.getId()) : that.getId() != null) return false; - return getUuid() != null ? getUuid().equals(that.getUuid()) : that.getUuid() == null; + return CHSBaseEntity.equals(this, o); } @Override diff --git a/avni-server-api/src/main/java/org/avni/server/domain/framework/IdHolder.java b/avni-server-api/src/main/java/org/avni/server/domain/framework/IdHolder.java new file mode 100644 index 000000000..0f343cd76 --- /dev/null +++ b/avni-server-api/src/main/java/org/avni/server/domain/framework/IdHolder.java @@ -0,0 +1,6 @@ +package org.avni.server.domain.framework; + +public interface IdHolder { + Long getId(); + String getUuid(); +} diff --git a/avni-server-api/src/test/java/org/avni/server/domain/CHSBaseEntityTest.java b/avni-server-api/src/test/java/org/avni/server/domain/CHSBaseEntityTest.java new file mode 100644 index 000000000..bcb31b147 --- /dev/null +++ b/avni-server-api/src/test/java/org/avni/server/domain/CHSBaseEntityTest.java @@ -0,0 +1,23 @@ +package org.avni.server.domain; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +public class CHSBaseEntityTest { + @Test + public void equals() { + assertEquals(entity(1l), entity(1l)); + assertNotEquals(entity(1l), entity(2l)); + assertNotEquals(new CHSBaseEntity(), new CHSBaseEntity()); + assertNotEquals(entity(1l), new CHSBaseEntity()); + assertNotEquals(new CHSBaseEntity(), entity(1l)); + } + + private static CHSBaseEntity entity(long id) { + CHSBaseEntity chsBaseEntity = new CHSBaseEntity(); + chsBaseEntity.setId(id); + return chsBaseEntity; + } +}