Thanks for the quick reply rajasaur.
I had looked into that before, but it doesn't seem to help in my case. I believe that is because the object I'm referring to already exists in a database.
To use the .equals() or .hash(), hibernate would have to load in the entire Authority table, and for each of its rows, compare against my new insertion.
Maybe I'm missing something however, so let me include my annotated POJOs I use in my example.
Code:
@Entity
@Table(name = "users")
public class User
{
private long id;
private Authority authority;
private Date registered;
private String username;
private String password;
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
private String address;
private String city;
private String state;
private String postalCode;
private String country;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@ManyToOne(cascade = CascadeType.ALL)
public Authority getAuthority() {
return authority;
}
public void setAuthority(Authority authority) {
this.authority = authority;
}
@Temporal(TemporalType.TIMESTAMP)
public Date getRegistered() {
return registered;
}
public void setRegistered(Date registered) {
this.registered = registered;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
// for brevity I'm omitting the rest of the getters and setters...
}
Code:
@Entity
@Table(name = "authority")
public class Authority implements Serializable
{
private long id;
private String role;
public Authority() {
}
public Authority(String role) {
this.role = role;
}
public boolean equals(Object obj) {
if (this == obj) return true;
if (! (obj instanceof Authority)) return false;
final Authority other = (Authority) obj;
return role.equals(other.getRole());
}
public int hashCode() {
return role.hashCode();
}
public String toString() {
return role;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "role")
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}