Hibernate/JPA: only one entry can have specific field value <p>I need something that seems not so specific but anyway I was unable to come up with nice and sophisticated solution.</p> <p>Say I have very simple hibernate/jpa entity:</p> <pre><code>@Entity(name="entity") public class Type { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; @Column(unique = true, nullable = false) private String name; @Column(unique = false, nullable = false) private boolean defaultType; } </code></pre> <p>What i need is to somehow annotate <em>defaultType</em> field so only (and exactly) one persisted entity have this value as true. When new entity get persisted with this <em>defaultType</em> as true, the old one (with <em>defaultType</em>=true) entity has to be altered and its <em>defaultType</em> value changed to false. Also if any entity get changed (its <em>defaultType</em> got changed to true), same rule should apply.</p> <p>As far I know this can be achieved inside business logic (e.g. in DAO layer), with DB trigger or with hibernates interceptor or event (If there is another way, please let me know). I tried with DAO solution but it's kind of bad solution because it can be bypassed and it is really clumsy for such simple operation. DB triggers can not be added with hibernate/jpa annotations (if I am not mistaken) and i am not sure how to make this functionality with hibernate interceptors/events.</p> <p>So, what is best solution for this problem?</p>