title: How to test Hibernate object equality in test code?
body: <p>I've got a Hibernate class that I do not implement an equals method on.</p>
  <p>My service layer has many methods which return this class.</p>
  <p>For example, one of my service layer methods is:</p>
  <pre><code>public List&lt;User&gt; getUsers() {
      return this.userRepository.findAll();
  }
  </code></pre>
  <p>where <code>User</code> is simply</p>
  <pre><code>@Entity
  public class User {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Column(name = &quot;name&quot;, nullable = false)
      private String name;
  
      @Column(name = &quot;email&quot;, nullable = false)
      private String email;
  }
  </code></pre>
  <p>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.</p>
  <p>However, it makes no sense to implement an equals method on the Hibernate class which checks that all fields are equal (as discussed in <a href="https://stackoverflow.com/questions/5031614/the-jpa-hashcode-equals-dilemma">The JPA hashCode() / equals() dilemma</a>).</p>
  <p>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.</p>
  <p>Is there a better way to do this?</p>