title: Java HashMap not finding key, but it should
body: <p>I have a strange issue occuring in my application, I will quickly explain global architecture and then my problem in depth.</p>
  
  <p>I use a service to populate a <code>HashMap&lt;DomainObject,Boolean&gt;</code> coming from my database (JPA driven) which is in turn returned to my view, via an EJB remote method call (using Apache Wicket). In this part, I add a new <code>DomainObject</code> to the map returned in order to store any new value from my end user.</p>
  
  <p>The problem occurs when the user hit the "add" button in its browser, I try to retrieve the newly created item in my map, but it fails. By playing with the debugger I face the following things.</p>
  
  <p>Assuming <code>HashMap&lt;DomainObject, Boolean&gt; map</code> and <code>DomainObject do</code> are the two variables interesting I have the following results in the debugger</p>
  
  <p><code>map.keySet();</code> gives me an object corresponding to <code>do</code> (even the @whatever simili-reference is identical), <code>hashcode()</code> on both objects returns similar value and <code>equals()</code> between the two returns <code>true</code></p>
  
  <p><code>map.containsKey(do);</code> returns <code>false</code></p>
  
  <p><code>map.get(do)</code>; returns <code>null</code>, weird because my key seems to be in the <code>map</code>.</p>
  
  <p>Assuming my newly created item is the first key enumerated by <code>keySet()</code>, I do the following :
  <code>map.get(new ArrayList(map.keySet()).get(0))</code>, and it returns null.</p>
  
  <p>If it can help, by attaching breakpoints to my <code>DomainObject.equals()</code> and <code>DomainObject.hashcode()</code> methods I found that <code>map.get()</code> is only calling <code>hashcode()</code> and not <code>equals()</code>.</p>
  
  <p>The only workaround I found is to recreate a new map on top of the existing one <code>new HashMap(map)</code>, in this new map, I have no problem at all looking up an object by its key.</p>
  
  <p>I hope someone here can give my a pointer on what happens, thanks.</p>
  
  <blockquote>
    <p>Environment used :</p>
    
    <ul>
    <li>Sun Java 1.6.0_26 x64 under OS X 10.7.1</li>
    <li>OpenJDK 1.6.0_18 x64 under Debian 6.0.2 (2.6.32)</li>
    <li>Apache Wicket 1.4.17</li>
    <li>Oracle Glassfish 3.1.1</li>
    <li>JBoss Hibernate 3.6.5</li>
    </ul>
  </blockquote>
  
  <p><code>DomainObject</code> code : </p>
  
  <pre><code>public class AssetComponentDetailTemplate extends BaseEntite&lt;Long&gt; {
  public enum DataType {
      TXT,
      DATE,
      INT,
      JOIN,
      LIST,
      COULEURS,
      REFERENCE
  }
  
  public enum Tab {
      IDENTITE,
      LOCALISATION,
      CYCLE_DE_VIE,
      FINANCE,
      RESEAU,
      DETAIL
  }
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  @Column(nullable = false)
  private String name;
  @Column(nullable = false)
  @Enumerated(EnumType.STRING)
  private DataType dataType;
  private Integer classNameId;
  private Long orderId;
  private Long nextAssetComponentDetailTemplateId;
  private String unit;
  @Enumerated(EnumType.STRING)
  private Tab tab;
  
  @Column(nullable = false)
  private Long uniqueOrganizationId;
  
  @OneToMany(fetch = FetchType.LAZY)
  @JoinColumn(name = "idAssetComponentDetailTemplate", insertable = false, updatable = false)
  private List&lt;AssetComponentDetailJoin&gt; assetComponentDetailJoins;
  
  private Boolean mandatory = false;
  
  public AssetComponentDetailTemplate() {
  }
  
  public Long getId() {
      return id;
  }
  
  public void setId(final Long id) {
      this.id = id;
  }
  
  public String getName() {
      return name;
  }
  
  public void setName(final String name) {
      this.name = name;
  }
  
  public DataType getDataType() {
      return dataType;
  }
  
  public void setDataType(final DataType dataType) {
      this.dataType = dataType;
  }
  
  public Integer getClassNameId() {
      return classNameId;
  }
  
  public void setClassNameId(final Integer classNameId) {
      this.classNameId = classNameId;
  }
  
  public Long getUniqueOrganizationId() {
      return uniqueOrganizationId;
  }
  
  public void setUniqueOrganizationId(final Long uniqueOrganizationId) {
      this.uniqueOrganizationId = uniqueOrganizationId;
  }
  
  public Long getNextAssetComponentDetailTemplateId() {
      return nextAssetComponentDetailTemplateId;
  }
  
  public void setNextAssetComponentDetailTemplateId(final Long nextAssetComponentDetailTemplateId) {
      this.nextAssetComponentDetailTemplateId = nextAssetComponentDetailTemplateId;
  }
  
  public String getUnit() {
      return unit;
  }
  
  public void setUnit(final String unit) {
      this.unit = unit;
  }
  
  public Tab getTab() {
      return tab;
  }
  
  public void setTab(final Tab tab) {
      this.tab = tab;
  }
  
  public Long getOrder() {
      return orderId;
  }
  
  public void setOrder(final Long order) {
      this.orderId = order;
  }
  
  public Boolean isMandatory() {
      return mandatory;
  }
  
  @Override
  public String toString() {
      return name;
  }
  
  @Override
  public boolean equals(final Object o) {
      if (this == o) {
          return true;
      }
      if (o == null || getClass() != o.getClass()) {
          return false;
      }
  
      final AssetComponentDetailTemplate that = (AssetComponentDetailTemplate) o;
  
      if (classNameId != null ? !classNameId.equals(that.classNameId) : that.classNameId != null) {
          return false;
      }
      if (dataType != that.dataType) {
          return false;
      }
      if (id != null ? !id.equals(that.id) : that.id != null) {
          return false;
      }
      if (name != null ? !name.equals(that.name) : that.name != null) {
          return false;
      }
      if (nextAssetComponentDetailTemplateId != null ?
          !nextAssetComponentDetailTemplateId.equals(that.nextAssetComponentDetailTemplateId) :
          that.nextAssetComponentDetailTemplateId != null) {
          return false;
      }
      if (orderId != null ? !orderId.equals(that.orderId) : that.orderId != null) {
          return false;
      }
      if (tab != that.tab) {
          return false;
      }
      if (uniqueOrganizationId != null ? !uniqueOrganizationId.equals(that.uniqueOrganizationId) :
          that.uniqueOrganizationId != null) {
          return false;
      }
      if (unit != null ? !unit.equals(that.unit) : that.unit != null) {
          return false;
      }
  
      return true;
  }
  
  @Override
  public int hashCode() {
      int result = id != null ? id.hashCode() : 0;
      result = 31 * result + (name != null ? name.hashCode() : 0);
      result = 31 * result + (dataType != null ? dataType.hashCode() : 0);
      result = 31 * result + (classNameId != null ? classNameId.hashCode() : 0);
      result = 31 * result + (orderId != null ? orderId.hashCode() : 0);
      result = 31 * result +
               (nextAssetComponentDetailTemplateId != null ? nextAssetComponentDetailTemplateId.hashCode() : 0);
      result = 31 * result + (unit != null ? unit.hashCode() : 0);
      result = 31 * result + (tab != null ? tab.hashCode() : 0);
      result = 31 * result + (uniqueOrganizationId != null ? uniqueOrganizationId.hashCode() : 0);
      return result;
  }
  </code></pre>