Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cc455f2

Browse files
committed
5 2 fix hibernate issue
1 parent eb128e1 commit cc455f2

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

src/main/java/ru/javawebinar/topjava/model/BaseEntity.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package ru.javawebinar.topjava.model;
22

3+
import org.hibernate.Hibernate;
4+
35
import javax.persistence.*;
46

7+
/**
8+
* Do not manipulate new (transient) entries in HashSet/HashMap without overriding hashCode
9+
* http://stackoverflow.com/questions/5031614
10+
*/
511
@MappedSuperclass
612

713
// http://stackoverflow.com/questions/594597/hibernate-annotations-which-is-better-field-or-property-access
@@ -12,7 +18,9 @@ public class BaseEntity {
1218
@Id
1319
@SequenceGenerator(name = "global_seq", sequenceName = "global_seq", allocationSize = 1, initialValue = START_SEQ)
1420
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "global_seq")
15-
protected Integer id;
21+
// PROPERTY access for id due to bug: https://hibernate.atlassian.net/browse/HHH-3718
22+
@Access(value = AccessType.PROPERTY)
23+
private Integer id;
1624

1725
protected BaseEntity() {
1826
}
@@ -30,28 +38,28 @@ public Integer getId() {
3038
}
3139

3240
public boolean isNew() {
33-
return (this.id == null);
41+
return (getId() == null);
3442
}
3543

3644
@Override
3745
public boolean equals(Object o) {
3846
if (this == o) {
3947
return true;
4048
}
41-
if (o == null || getClass() != o.getClass()) {
49+
if (o == null || !getClass().equals(Hibernate.getClass(o))) {
4250
return false;
4351
}
4452
BaseEntity that = (BaseEntity) o;
45-
return id != null && id.equals(that.id);
53+
return getId() != null && getId().equals(that.getId());
4654
}
4755

4856
@Override
4957
public int hashCode() {
50-
return (id == null) ? 0 : id;
58+
return (getId() == null) ? 0 : getId();
5159
}
5260

5361
@Override
5462
public String toString() {
55-
return String.format("Entity %s (%s)", getClass().getName(), id);
63+
return String.format("Entity %s (%s)", getClass().getName(), getId());
5664
}
5765
}

src/main/java/ru/javawebinar/topjava/model/Meal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void setUser(User user) {
9999
@Override
100100
public String toString() {
101101
return "Meal{" +
102-
"id=" + id +
102+
"id=" + getId() +
103103
", dateTime=" + dateTime +
104104
", description='" + description + '\'' +
105105
", calories=" + calories +

src/main/java/ru/javawebinar/topjava/model/NamedEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ public String getName() {
3131

3232
@Override
3333
public String toString() {
34-
return String.format("Entity %s (%s, '%s')", getClass().getName(), id, name);
34+
return String.format("Entity %s (%s, '%s')", getClass().getName(), getId(), name);
3535
}
3636
}

src/main/java/ru/javawebinar/topjava/model/User.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public String getPassword() {
118118
@Override
119119
public String toString() {
120120
return "User{" +
121-
"id=" + id +
121+
"id=" + getId() +
122122
", email=" + email +
123123
", name=" + name +
124124
", enabled=" + enabled +

0 commit comments

Comments
 (0)