Skip to content

The Unified and Autonomous Development Platform

Improve AbstractEntity.equals/hash/compareTo to handle null id case.

robin shineb9681b59192b
diff --git a/commons.hibernate/src/main/java/com/pmease/commons/hibernate/AbstractEntity.java b/commons.hibernate/src/main/java/com/pmease/commons/hibernate/AbstractEntity.java
index 3c6296dc70..c2ae2f50bb 100644
--- a/commons.hibernate/src/main/java/com/pmease/commons/hibernate/AbstractEntity.java
+++ b/commons.hibernate/src/main/java/com/pmease/commons/hibernate/AbstractEntity.java
@@ -38,7 +38,7 @@ public abstract class AbstractEntity implements Serializable, Comparable<Abstrac
if (this == other)
return true;
AbstractEntity otherEntity = (AbstractEntity) other;
- if (getId() == null && otherEntity.getId() == null)
+ if (getId() == null || otherEntity.getId() == null)
return super.equals(other);
else
return new EqualsBuilder().append(getId(), otherEntity.getId()).isEquals();
@@ -53,7 +53,16 @@ public abstract class AbstractEntity implements Serializable, Comparable<Abstrac
@Override
public int compareTo(AbstractEntity entity) {
- return getId().compareTo(entity.getId());
+ if (getId() != null) {
+ if (entity.getId() != null)
+ return getId().compareTo(entity.getId());
+ else
+ return -1;
+ } else if (entity.getId() != null) {
+ return 1;
+ } else {
+ return 0;
+ }
}
public boolean isNew() {