@Test public void cannotBeComparedWithNonEntity() { TestEntity entity = new TestEntity(1L); Object nonEntity = new Object(); assertThat(entity.equals(nonEntity)).isFalse(); assertThat(nonEntity.equals(entity)).isFalse(); }
@Override public boolean equals(Object other) { if (other == this) { return true; } if (other == null) { return false; } Class<? extends BaseEntity> thisClass = getClass(); Class<?> otherClass = other.getClass(); if (!(other instanceof Entity) || (!thisClass.isAssignableFrom(otherClass) && !otherClass.isAssignableFrom( thisClass))) { return false; } return Objects.equals(getId(), ((Entity<?>) other).getId()); }
BaseEntity implements Entity<I> { @Override public boolean equals(Object other) { if (other == this) { return true; } if (other == null) { return false; } Class<? extends BaseEntity> thisClass = getClass(); Class<?> otherClass = other.getClass(); if (!(other instanceof Entity) || (!thisClass.isAssignableFrom(otherClass) && !otherClass.isAssignableFrom( thisClass))) { return false; } return Objects.equals(getId(), ((Entity<?>) other).getId()); } }
BaseEntity implements Entity<I> { @Override public boolean equals(Object other) { if (other == this) { return true; } if (other == null) { return false; } Class<? extends BaseEntity> thisClass = getClass(); Class<?> otherClass = other.getClass(); if (!(other instanceof Entity) || (!thisClass.isAssignableFrom(otherClass) && !otherClass.isAssignableFrom( thisClass))) { return false; } return Objects.equals(getId(), ((Entity<?>) other).getId()); }  }
BaseEntity implements Entity<I> { @Override public boolean equals(Object other) { if (other == this) { return true; } if (other == null) { return false; } Class<? extends BaseEntity> thisClass = getClass(); Class<?> otherClass = other.getClass(); if (!(other instanceof Entity) || (!thisClass.isAssignableFrom(otherClass) && !otherClass.isAssignableFrom( thisClass))) { return false; } return Objects.equals(getId(), ((Entity<?>) other).getId()); }  @Override @SuppressWarnings("unchecked") I getId(); @Override int hashCode(); @Override boolean equals(Object other); @Override String toString(); }
BaseEntity implements Entity<I> { @Override public boolean equals(Object other) { if (other == this) { return true; } if (other == null) { return false; } Class<? extends BaseEntity> thisClass = getClass(); Class<?> otherClass = other.getClass(); if (!(other instanceof Entity) || (!thisClass.isAssignableFrom(otherClass) && !otherClass.isAssignableFrom( thisClass))) { return false; } return Objects.equals(getId(), ((Entity<?>) other).getId()); }  @Override @SuppressWarnings("unchecked") I getId(); @Override int hashCode(); @Override boolean equals(Object other); @Override String toString();  }