Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion teaql-core/src/main/java/io/teaql/core/BaseEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ public boolean equals(Object pO) {

@Override
public int hashCode() {
return Objects.hash(getId(), getVersion(), typeName());
return Objects.hash(getId(), typeName());
}

public Map<String, Object> getAdditionalInfo() {
Expand Down
45 changes: 45 additions & 0 deletions teaql-core/src/test/java/io/teaql/core/BaseEntityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,49 @@ public void testEntityWithoutIdFallsBackToLocalTracking() {
entity.updateName(null);
assertNull(entity.dirtyFields());
}

static class AnotherTestEntity extends BaseEntity {
@Override
public String typeName() {
return "AnotherTestEntity";
}
}

@Test
public void testEqualsAndHashCodeContract() {
TestEntity e1 = new TestEntity();
e1.updateId(1L);
e1.updateVersion(1L);

TestEntity e2 = new TestEntity();
e2.updateId(1L);
e2.updateVersion(2L); // Different version

// 1. Same instance is equal to itself
assertEquals(e1, e1);
assertEquals(e1.hashCode(), e1.hashCode());

// 2. Same concrete entity type and ID are equal
assertEquals(e1, e2);

// 3. Equal entities have identical hash codes even when versions differ
assertEquals(e1.hashCode(), e2.hashCode());

// 4. HashSet lookup succeeds for an equal entity instance
java.util.HashSet<TestEntity> set = new java.util.HashSet<>();
set.add(e1);
assertTrue(set.contains(e2));

// 5. Different IDs are not equal
TestEntity e3 = new TestEntity();
e3.updateId(2L);
e3.updateVersion(1L);
assertNotEquals(e1, e3);

// 6. Different concrete entity classes are not equal
AnotherTestEntity a1 = new AnotherTestEntity();
a1.updateId(1L);
a1.updateVersion(1L);
assertNotEquals(e1, a1);
}
}
Loading