I need something like a partial contraint for one of my entities.
@Entity
public class MyEntity
{
@NotNull
private String name;
@ManyToOne @NotNull
private Type type;
}
Only for a sinlge type
I need the name
to be unique.
Is this possible with a @UniqueConstraint
or do I need to implement this with a @PrePersist
and @PreUpdate
listener? So far I haven't implemented such a listener, but even if I check the contraint in this listener, does it guarantee to prevent a duplicate entry?
Update
Let's assume the constraint should only be active for type=special
- Allowed
{id=1,type=normal,name=Test},{id=2,type=normal,name=Test}
- Not allowed:
{id=3,type=special,name=Test},{id=4,type=special,name=Test}