如何通过休眠将单例对象建模到表?

red*_*edi 5 java orm hibernate data-modeling

我的项目中有一个全局配置对象,并且我想在数据库中保留该类的 0 或 1 个实例。做这个的最好方式是什么 ?我在这里知道的一个技巧是映射一个“常量”字段,并在其上设置唯一的约束,是否还有其他这样的方法,因为这看起来有点hacky?

这是我尝试过的:-

    @Entity
    public class DTLdapConfig implements Serializable {

        @GeneratedValue(strategy=GenerationType.TABLE)
        @Id
        private int id;

        @Column(unique=true)
        private boolean singletonGuard;

 // no public setter getter for singletonGuard

 // other code below

}
Run Code Online (Sandbox Code Playgroud)