Lets say I have the following code:
Code:
@Entity
public class Person {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public boolean equals(Object other) {
if(other instanceof Person) {
return id.equals(((Person) other).getId());
} else {
return false;
}
}
public static void main(String[] args) {
Person p1 = new Person();
Person p2 = new Person();
System.out.println(p1.equals(p2)); //this throws a null pointer exception
}
}
I was wondering when hibernate assigns the ID field to the Person object, because I'd like to use the @Id as the primary field for the equals method but as the code is right now, I get a null pointer exception. Any insights would be helpful. Thanks!