title: Spring JPA/HIbernate: duplicate key value violates unique constraint
body: <p>Searching amongst existing answers gave no valid results. So here is the error I can't find the reason for.
  I have an entity class <code>MeteoRecord</code> and <code>DayDegree</code>defined as follows:</p>
  <pre class="lang-java prettyprint-override"><code>@Getter
  @Setter
  @Entity
  public class MeteoRecord extends AbstractEntity {
  
      @Temporal(TemporalType.DATE)
      Date date;
  ...
  //other attributes
  }
  
  @Entity
  @Getter
  @Setter
  public class DayDegree extends AbstractEntity {
  
      @ManyToOne
      @JoinColumn(name = &quot;meteo_record_id&quot;)
      private MeteoRecord meteoRecord;
  ...
  //other attributes
  }
  </code></pre>
  <p>All the entities extend <code>AbstractEntity</code> class:</p>
  <pre class="lang-java prettyprint-override"><code>@MappedSuperclass
  @Getter
  @Setter
  public abstract class AbstractEntity implements Serializable {
  
      @Access(AccessType.PROPERTY)
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      @CreationTimestamp
      @Temporal(TemporalType.TIMESTAMP)
      @Column(name = &quot;DATCRE&quot;)
      public Date datcre;
  
      @UpdateTimestamp
      @Temporal(TemporalType.TIMESTAMP)
      @Column(name = &quot;DATMOD&quot;)
      public Date datmod;
  }
  </code></pre>
  <p>When saving a new <code>MeteoRecord</code> as follows:</p>
  <pre class="lang-java prettyprint-override"><code>Optional&lt;MeteoRecord&gt; meteoByDate = meteoRecordRepository.findByDate(dateAsDate);
    MeteoRecord meteoRecord;
    if (meteoByDate.isPresent()) {
        meteoRecord = meteoByDate.get();
    } else {
        meteoRecord = new MeteoRecord();
        meteoRecord.setDate(dateAsDate);
    }
  MeteoRecord savedMeteoRecord = meteoRecordRepository.save(meteoRecord);
  ...
  </code></pre>
  <p>it raises the error:</p>
  <pre><code>rg.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint &quot;meteo_record_pkey&quot;
    Detail: Key (id)=(1680) already exists.
  </code></pre>
  <p>The `MeteoRepositiry just extends `` JPARepository:</p>
  <pre class="lang-java prettyprint-override"><code>@Repository
  public interface MeteoRecordRepository extends JpaRepository&lt;MeteoRecord, Long&gt; {
  ...
  Optional&lt;MeteoRecord&gt; findByDate(Date date);
  }
  </code></pre>
  <p>I activated more logs to see the SQL queries but see no additional queries creating a new <code>MeteoRecord</code> nor inserting a <code>DayDegree</code> record elsewhere:</p>
  <pre><code>11:33:55.664 [http-nio-8080-exec-1] DEBUG org.hibernate.SQL - 
      select
          meteorecor0_.id as id1_33_,
          meteorecor0_.datcre as datcre2_33_,
          meteorecor0_.datmod as datmod3_33_,
          meteorecor0_.date as date4_33_,
          meteorecor0_.degree_day_15 as degree_d5_33_,
          meteorecor0_.degree_day_15_eq as degree_d6_33_,
          meteorecor0_.station as station7_33_,
          meteorecor0_.temp_avg as temp_avg8_33_,
          meteorecor0_.temp_avg_eq as temp_avg9_33_ 
      from
          meteo_record meteorecor0_ 
      where
          meteorecor0_.date=?
  Hibernate: 
      select
          meteorecor0_.id as id1_33_,
          meteorecor0_.datcre as datcre2_33_,
          meteorecor0_.datmod as datmod3_33_,
          meteorecor0_.date as date4_33_,
          meteorecor0_.degree_day_15 as degree_d5_33_,
          meteorecor0_.degree_day_15_eq as degree_d6_33_,
          meteorecor0_.station as station7_33_,
          meteorecor0_.temp_avg as temp_avg8_33_,
          meteorecor0_.temp_avg_eq as temp_avg9_33_ 
      from
          meteo_record meteorecor0_ 
      where
          meteorecor0_.date=?
  11:33:55.677 [http-nio-8080-exec-1] DEBUG o.s.d.r.c.s.TransactionalRepositoryProxyPostProcessor$CustomAnnotationTransactionAttributeSource - Adding transactional method 'save' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
  11:33:55.679 [http-nio-8080-exec-1] DEBUG o.s.orm.jpa.JpaTransactionManager - Found thread-bound EntityManager [SessionImpl(1454087429&lt;open&gt;)] for JPA transaction
  11:33:55.680 [http-nio-8080-exec-1] DEBUG o.s.orm.jpa.JpaTransactionManager - Creating new transaction with name [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
  11:33:55.680 [http-nio-8080-exec-1] DEBUG o.h.e.t.internal.TransactionImpl - On TransactionImpl creation, JpaCompliance#isJpaTransactionComplianceEnabled == false
  11:33:55.680 [http-nio-8080-exec-1] DEBUG o.h.e.t.internal.TransactionImpl - begin
  11:33:55.681 [http-nio-8080-exec-1] DEBUG o.s.orm.jpa.JpaTransactionManager - Exposing JPA transaction as JDBC [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@27864a10]
  11:33:55.696 [http-nio-8080-exec-1] DEBUG org.hibernate.engine.spi.ActionQueue - Executing identity-insert immediately
  11:33:55.703 [http-nio-8080-exec-1] DEBUG org.hibernate.SQL - 
      insert 
      into
          meteo_record
          (datcre, datmod, date, degree_day_15, degree_day_15_eq, station, temp_avg, temp_avg_eq) 
      values
          (?, ?, ?, ?, ?, ?, ?, ?)
  Hibernate: 
      insert 
      into
          meteo_record
          (datcre, datmod, date, degree_day_15, degree_day_15_eq, station, temp_avg, temp_avg_eq) 
      values
          (?, ?, ?, ?, ?, ?, ?, ?)
  11:33:55.732 [http-nio-8080-exec-1] DEBUG o.h.e.jdbc.spi.SqlExceptionHelper - could not execute statement [n/a]
  org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint &quot;meteo_record_pkey&quot;
    Detail: Key (id)=(1681) already exists.
  </code></pre>
  <p>What am I missing? I have an impression that it is because of a relation defined in <code>DayDegree</code> entity but see no queries nor use of <code>DayDegree</code> entity.</p>
  <p>Thank you.</p>