Hibernate - pattern for default object mapping?

Is there a slick way to map the default database object in Hibernate?

For example, in this class, Foo should always return Bar, whether it has been configured for Foo, or whether it comes from a default that is stored somewhere in the database. Foo users should be able to customize a custom Bar object in it, but they don't need write access to Bar - hence reduced visibility.

public Class Foo {
    public Bar Bar {
        get {
            return CustomBar ?? DefaultBar;
        }
    }

    public Bar CustomBar { get; set; }
    protected Bar DefaultBar { get; private set; }
}

      

What I'm interested in is hibernation for the DefaultFoo property mapping. All Bar classes must be able to reach this single DefaultFoo object. The only way I know how to do this in Hibernate is with a one-to-many mapping in Foo. (Foo containing from one to many bars) ... this seems a little silly since there is always at most one bar. Is there a better pattern for this behavior? TIA.

+1


source to share


1 answer


This is also called the NullObject or DefaultObject pattern. This has been covered on the Hibernate mailing lists. But I haven't seen anything about how you actually store it in the database. If you want a real NullObject external from the database, then you can implement the default Singleton object and implement the ILifecycle interface (I think) and veto the OnSave event to prevent it from being saved.



0


source







All Articles