Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is (almost) correct for "technical equality", but not for "natural equality". To achieve top technical equality, you should also test the reflexive <code>o == this</code>. It may happen that the object isn't persisted in DB yet and thus doesn't have a technical ID yet. E.g.</p> <pre><code>public class User { private Long id; @Override public boolean equals(Object object) { return (object instanceof User) &amp;&amp; (id != null) ? id.equals(((User) object).id) : (object == this); } @Override public int hashCode() { return (id != null) ? (User.class.hashCode() + id.hashCode()) : super.hashCode(); } } </code></pre> <p>For "natural equality" you should rather compare all non-technical properties. For "real world entities" this is after all more robust (but also more expensive) than technical equality.</p> <pre><code>public class User { private String name; private Date birth; private int housenumber; private long phonenumber; @Override public boolean equals(Object object) { // Basic checks. if (object == this) return true; if (!(object instanceof User)) return false; // Property checks. User other = (User) object; return Objects.equals(name, other.name) &amp;&amp; Objects.equals(birth, other.birth) &amp;&amp; (housenumber == other.housenumber) &amp;&amp; (phonenumber == other.phonenumber); } @Override public int hashCode() { return Objects.hash(name, birth, housenumber, phonenumber); } } </code></pre> <p>True, that's lot of code when there are a lot of properties. A bit decent IDE (Eclipse, Netbeans, etc) can just autogenerate <code>equals()</code>, <code>hashCode()</code> (and also <code>toString()</code>, getters and setters) for you. Take benefit of it. In Eclipse, rightclick code and peek the <em>Source</em> (Alt+Shift+S) menu option.</p> <h3>See also:</h3> <ul> <li><a href="https://developer.jboss.org/wiki/EqualsandHashCode" rel="nofollow noreferrer">JBoss: Equals and HashCode (in view of <em>persistence</em>)</a></li> <li><a href="https://docs.jboss.org/hibernate/stable/core.old/reference/en/html/persistent-classes-equalshashcode.html" rel="nofollow noreferrer">Hibernate: Persistent Classes - implementing equals() and hashCode()</a></li> <li><a href="https://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java">Related SO question: Overriding equals and hashCode in Java</a> </li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload