Skip to content

Commit

Permalink
#813 - fixed equals method
Browse files Browse the repository at this point in the history
  • Loading branch information
petmongrels committed Nov 6, 2024
1 parent 49cc333 commit 4eaa4dd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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;

import jakarta.persistence.*;
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
Expand Down Expand Up @@ -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
Expand All @@ -77,4 +85,5 @@ public int hashCode() {
result = 31 * result + (uuid != null ? uuid.hashCode() : 0);
return result;
}

}
16 changes: 3 additions & 13 deletions avni-server-api/src/main/java/org/avni/server/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -238,16 +237,7 @@ public void setUserGroups(List<UserGroup> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.avni.server.domain.framework;

public interface IdHolder {
Long getId();
String getUuid();
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 4eaa4dd

Please sign in to comment.