title: Hibernate @ElementCollection Map with @Audited create duplicate key violation on update
body: <p>I have a problem with hibernate mappings in conjunction with auditing.
  My java class with mapping:</p>
  
  <pre><code>@Entiy
  @Audited
  @Table(name = "farm_detail")
  public class FarmDetail {
     @Id
     private Long id;
  
      @ElementCollection
      @MapKeyColumn(name = "field_id")
      @Column(name = "remark")
      @CollectionTable(name = "farm_detail_remark", joinColumns =    @JoinColumn(name = "farm_detail_id"))
      private Map&lt;String, String&gt; fieldRemarks = new HashMap&lt;&gt;();
     // Getter, Setter, etc
  }
  </code></pre>
  
  <p>The sql definition of the <code>farm_detail_remark</code> and <code>farm_detial_remark_aud</code> tables:</p>
  
  <pre><code>db=# \d farm_detail_remark
            Table "public.farm_detail_remark"
       Column     |          Type          | Modifiers 
  ----------------+------------------------+-----------
   farm_detail_id | bigint                 | not null
   field_id       | character varying(255) | not null
   remark         | character varying      | not null
  Indexes:
      "farm_detail_remark_pkey" PRIMARY KEY, btree (farm_detail_id, field_id)
  Foreign-key constraints:
      "farm_detail_remark_farm_detail_id_fkey" FOREIGN KEY (farm_detail_id) REFERENCES farm_detail(id)
  
  db=# \d farm_detail_remark_aud
          Table "public.farm_detail_remark_aud"
       Column     |          Type          | Modifiers 
  ----------------+------------------------+-----------
   rev            | integer                | not null
   revtype        | smallint               | 
   farm_detail_id | bigint                 | not null
   field_id       | character varying(255) | not null
   remark         | character varying      | 
  Indexes:
      "farm_detail_remark_aud_pkey" PRIMARY KEY, btree (rev, farm_detail_id, field_id)
  Foreign-key constraints:
      "farm_detail_remark_aud_rev_fkey" FOREIGN KEY (rev) REFERENCES revinfo(rev)
  </code></pre>
  
  <p>If I want to update any entry of the <code>fieldRemarks</code> set I get a duplicate key error on the <code>farm_detail_remark_aud</code> table. By turning on the sql logging I found out that hibernate tries to insert two entries in the aud table:</p>
  
  <pre><code>Hibernate: 
      /* insert farm_detail_remark_AUD
          */ insert 
          into
              farm_detail_remark_aud
              (revtype, rev, farm_detail_id, remark, field_id) 
          values
              (?, ?, ?, ?, ?)
  21:20:58.188 [main] TRACE o.h.t.d.s.BasicBinder - binding parameter [1] as [INTEGER] - [0]
  21:20:58.188 [main] TRACE o.h.t.d.s.BasicBinder - binding parameter [2] as [INTEGER] - [1057]
  21:20:58.188 [main] TRACE o.h.t.d.s.BasicBinder - binding parameter [3] as [BIGINT] - [110]
  21:20:58.188 [main] TRACE o.h.t.d.s.BasicBinder - binding parameter [4] as [VARCHAR] - [Old remark]
  21:20:58.188 [main] TRACE o.h.t.d.s.BasicBinder - binding parameter [5] as [VARCHAR] - [152]
  </code></pre>
  
  <p>and (only param binding here):</p>
  
  <pre><code>21:20:58.193 [main] TRACE o.h.t.d.s.BasicBinder - binding parameter [1] as [INTEGER] - [2]
  21:20:58.194 [main] TRACE o.h.t.d.s.BasicBinder - binding parameter [2] as [INTEGER] - [1057]
  21:20:58.194 [main] TRACE o.h.t.d.s.BasicBinder - binding parameter [3] as [BIGINT] - [110]
  21:20:58.194 [main] TRACE o.h.t.d.s.BasicBinder - binding parameter [4] as [VARCHAR] - [new remark]
  21:20:58.194 [main] TRACE o.h.t.d.s.BasicBinder - binding parameter [5] as [VARCHAR] - [152]
  </code></pre>
  
  <p>Is there a way to solve this, without creating an extra entity for the remarks?</p>
  
  <p>I'm using hibernate 5.0.12 shipped with spring boot 1.5.7. The database is postgresql 9.5.6.</p>