2

I have implemented a solution similar to: How to use Hibernate @Any-related annotations?, and it's getting out of hand - I have too many mappings which have similar mapping "scheme":

@Any(metaColumn = @Column(name = "something_type"), fetch = FetchType.LAZY)
@AnyMetaDef(idType = "long", metaType = "string", metaValues = {
    @MetaValue(value = "A", targetEntity = AImpl.class),
    @MetaValue(value = "B", targetEntity = BImpl.class),
    @MetaValue(value = "C", targetEntity = CImpl.class),
    @MetaValue(value = "D", targetEntity = DImpl.class),
    @MetaValue(value = "E", targetEntity = EImpl.class),
    etc..  })
public BaseSomething getRelatedSometing() {
    return relatedSometing;
}

I've tried to keep a value (e.g. "A") that can be computed to a target (e.g. "AImpl"). Is there a way to avoid listing all the implementation by hand, but still keep hibernate happy?

Community
  • 1
  • 1
Asaf
  • 2,480
  • 4
  • 25
  • 33

1 Answers1

3

I asked a similar question, hoping for support for Spring style meta-annotations.

However, In the meantime, the following is working well for me:

class SomeClass {
    public static final String SOMETHING_METADEF_NAME = "somethingMetadefName";


    @Any(metaColumn = @Column(name = "something_type"), fetch = FetchType.LAZY)
    @AnyMetaDef(idType = "long", metaType = "string", name=SomeClass.SOMETHING_METADEF_NAME,
    metaValues = {
            @MetaValue(value = "A", targetEntity = AImpl.class),
            @MetaValue(value = "B", targetEntity = BImpl.class),
            @MetaValue(value = "C", targetEntity = CImpl.class),
            @MetaValue(value = "D", targetEntity = DImpl.class),
            @MetaValue(value = "E", targetEntity = EImpl.class),
            etc..  })
    public BaseSomething getRelatedSometing() {
        return relatedSometing;
    }
}

    // Elsewhere
    @Any(metaColumn = @Column(name = "something_type"), fetch = FetchType.LAZY,
            metaDef=SomeClass.SOMETHING_METADEF_NAME)
    public BaseSomething getAnotherSomething() {
        return relatedSometing;
    }

Specifically, I define the relationship once, and give it a name property in the @AnyMetaDef. Then, I can reference this from a @Any(metaDef=MY_NAME) elsewhere.

Hope this helps

Community
  • 1
  • 1
Marty Pitt
  • 28,822
  • 36
  • 122
  • 195
  • Thanks. I wans't aware of reusing a named metadef. It does saves me a bunch of redundant lines. the pain is still specifying the target classes. – Asaf Nov 20 '12 at 23:45