How to simulate a singleton object for a table through hibernation?

I have a global config object in my project and there may be 0 or 1 instance of this class that I want to store in the db. What's the best way to do this? One trick I know here is to have a "constant" field mapped to a unique constraint set on it, are there any other ways this looks a little hacky?

Here is what I tried: -

    @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

}

      

+3


source to share


1 answer


This is what I would like to do and then pass the instance as needed.

From Wikipedia: http://en.wikipedia.org/wiki/Singleton_pattern



public class Singleton {
     private static final Singleton instance = new Singleton();

     private Singleton() {}

     public static Singleton getInstance() {
        return instance;
     }
}

      

-1


source







All Articles