JPA hibernate insert row cause ConstraintViolationException
<p>i have simple entity class:</p>

<pre><code>@Entity
@Table(name = "NH_ENTITIES")
@Getter
@Setter
@ToString
public class Entita {
  /**
   * Primary key.
   */
  @Id
  @Column(name = "PK", nullable = false, length = NHConstants.DB_STANDARD_LENGTH)
  @GeneratedValue(strategy = GenerationType.AUTO, generator = "nh_entities_gen")
  @SequenceGenerator(name = "nh_entities_gen", sequenceName = "NH_ENTITIES_SEQ", initialValue = 1, allocationSize = 1)
  private int pk;

  /**
   * Entity name.
   */
  @NotEmpty(message = "Entita je povinná")
  @Size(max = NHConstants.DB_MAX_LENGTH, message = "Maximální počet znaků: " + NHConstants.DB_MAX_LENGTH)
  @Column(name = "ENTITY", unique = true, nullable = false, length = NHConstants.DB_MAX_LENGTH)
  private String entity = "";

  /**
   * For Hibernate only.
   */
  public Entita() {
  }
}
</code></pre>

<p>and my service method:</p>

<pre><code>  @Override
  @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
  public void saveEntity(Entita entity) {
    log.debug("Start transaction saveEntity");
    this.entityRepo.save(entity);
    log.debug("Transaction saveEntity finished");
  }
</code></pre>

<p>when i <strong>manually</strong> insert row/rows to this entity by some sql client and afterthat execute saveEntity programatically im getting the error
<em>could not execute statement; SQL [n/a]; constraint [C##NH.SYS_C0013920]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement</em></p>

<p>This error im getting just once....it depends how many rows i inserted before manually....</p>

<p>Is there another way how to handle this issue except execute findlast before save?</p>

<p>Thank you for your suggestion.</p>