I've got a Hibernate class that I do not implement an equals method on.
My service layer has many methods which return this class.
For example, one of my service layer methods is:
public List<User> getUsers() {
return this.userRepository.findAll();
}
where User
is simply
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "email", nullable = false)
private String email;
}
In my test code, I would like to assert equality between the objects the service methods return and the objects I expect them to return.
However, it makes no sense to implement an equals method on the Hibernate class which checks that all fields are equal (as discussed in The JPA hashCode() / equals() dilemma).
At the moment to solve this I'm converting my Hibernate class into a class which does have an equals method which checks each field is equal. It works, but it's not too elegant.
Is there a better way to do this?