title: Conditional unique constraint using annotations
body: <p>I have two related entities with two different types(GENERAL and CUSTOM) and I save it in the same table. Entity with type GENERAL should have unique values of field name and CUSTOM can have duplicates for different users and not duplicate GENERAL name.
  I'm looking for a way to create conditional unique constraint in order to check next cases:</p>
  
  <ul>
  <li>if entity has type GENERAL, name should be unique</li>
  <li><p>if entity has type CUSTOM, name can be duplicated in the table but can't duplicate GENERAL items and should be unique for specific user(by user id)</p>
  
  <p><pre><code>@Entity
  @Table(name = "Purpose", uniqueConstraints = @UniqueConstraint(columnNames = {"purposeId"}))
  @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
  public class GeneralPurpose {</p>
  
  <p>@Id
  @GeneratedValue(strategy=GenerationType.AUTO, generator="purpose_seq_gen")
  @SequenceGenerator(name="purpose_seq_gen", sequenceName="PURPOSE_SEQ")
  @Column(name = "purposeId", nullable = false)
  private long purposeId;</p>
  
  <p>@Column(name = "type", nullable = false)
  @Enumerated(EnumType.STRING)
  private PurposeType type;</p>
  
  <p>@Column(name = "name", nullable = false)
  private String name;</pre></code></p>
  
  <p><pre><code>@Entity
  @Table(name = "Purpose")
  public class CustomPurpose extends GeneralPurpose {</p>
  
  <p>@ManyToOne()
  @JoinColumn(name="id")
  @JsonIgnore
  private User user;</pre></code></p>
  
  <p><pre><code>public enum PurposeType {
      GENERAL, CUSTOM
  }</pre></code></p></li>
  </ul>