Ok so I'll answer my own question.
The answer is mapping the ManyToOne with an any in XML configuration. After I went I home I reread Hibernate in Action, and discovered the exact discussion I needed in there.
However, there is no equivalent to <any> element in annotations. However, it's in the works to add a @ManyToAny annotation as well as a @Any annotation in v 3.3.1. I have a work around which I think will perform better than the discriminator, type that ManyToAny relationship would use, but not as clean.
However, this is probably the weakest part of ORM tools while hibernate allows you to do this. It doesn't recommend it which is bogus because no matter how well you organize your mapping. That doesn't guarentee that there will only one interface through which polymorphism is provided. This isn't some silly work around for legacy issues. You have the exact same issue that TPC produces when you add interfaces. The STI and Joined subclass mapping strategy only allows ONE polymorphic base. However, Java objects can have any number of interfaces. But, these prescribed mapping strategies only provide ONE to us.
I don't see this issue is completely contained to hibernate per se. I think Ruby's ActiveRecord essentially does the same thing, however it's much easier to use, but not as optimized. It's really a problem with what relational DB's provide us. There's just no easy way to solve it perfectly given the relational DB toolset which is absolutely ridiculous given 80%+ of projects are using OO languages. I wonder if we could extend relational DBs to provide a better facility for this.
|