Skip to content

Commit 56cb8bf

Browse files
committed
Added equals and hashcode methods in BaseEntity
1 parent 2f0d9e0 commit 56cb8bf

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class Gender extends BaseEntity {
5959
```java
6060
@TableName("person")
6161
public class Person extends BaseEntity {
62-
//BaseEntity gives us the id field.
62+
// BaseEntity gives us the id field.
6363
// We need to add the rest.
6464

6565
private String name;
@@ -80,6 +80,7 @@ public class Person extends BaseEntity {
8080
```
8181

8282
Every POJO *must* extend `BaseEntity` and have an empty or default constructor.\
83+
The class `BaseEntity` overrides the `equals` and `hashCode` methods using the `id`. Override them in your entity classes if you need different behavior.\
8384
If you want to have a field in your POJO that should be ignored by Java2DB, you can apply the `Ignore` attribute to the specific field.
8485

8586
Then we can go ahead and create the service classes:

src/main/java/com/github/collinalpert/java2db/entities/BaseEntity.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,33 @@ public long getId() {
2020
*
2121
* @param id The id of the entity.
2222
*/
23+
@Deprecated
2324
public void setId(long id) {
2425
this.id = id;
2526
}
2627

2728
@Override
2829
public String toString() {
29-
return "Id: " + id;
30+
return "Id: " + this.id;
31+
}
32+
33+
@Override
34+
public boolean equals(Object o) {
35+
if (this == o) {
36+
return true;
37+
}
38+
39+
if (!(o instanceof BaseEntity)) {
40+
return false;
41+
}
42+
43+
BaseEntity that = (BaseEntity) o;
44+
45+
return this.id == that.id;
46+
}
47+
48+
@Override
49+
public int hashCode() {
50+
return (int) (this.id ^ (this.id >>> 32));
3051
}
3152
}

0 commit comments

Comments
 (0)