5 Replies Latest reply on Nov 7, 2007 8:37 AM by porcherg

    How to mix inheritence hierarchies with Hibernate ?

    porcherg

      To map the NodeBehaviour, I want to use a table per process language.

      To do this, each process language need to extend NodeBehaviour with a strategy one table per class (union-subclass) or one table per subclass (joined-subclass).
      Then I want each process language behaviour to be mapped in the same table. We can do this by using a single table strategy (subclass).
      So I need a mixed strategy (maybe this is not the best way to map NodeBehaviour, it's just how the problem appeared).

      In Hibernate documentation, it is said (http://www.hibernate.org/hib_docs/reference/en/html/inheritance.html) :

      It is possible to use different mapping strategies for different branches of the same
      inheritance hierarchy, and then make use of implicit polymorphism to achieve polymorphism
       across the whole hierarchy.


      To use implicit polymorphism in this case, we need to specify a "any" relationship in the NodeImpl mapping. This "any" element contains all the known subclass of NodeBehaviour.

      I don't think this is a good option because :
      - there can't be any foreign key constraint with "any" relationships, and that can be a problem (see http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-types-anymapping)
      - each extension needs to modify the mapping of NodeImpl to add their own NodeBehaviour implementations.

      However, Hibernate does not support mixing <subclass>, and
      <joined-subclass> and <union-subclass> mappings under the same root <class> element.


      This is what I would have called mixing inheritance strategies...

      It is possible to mix together the table per hierarchy and table per subclass strategies, under the the same <class> element, by combining the <subclass> and <join> elements (see below)


      This can be a solution, but the join element only allows property, many-to-one, component, dynamic-component and any relationships. So it's not possible to map a collection in that case.

      Is there another way to mix inheritance strategies with Hibernate ?
      I took the example of NodeBehaviour, but there might be other classes where a mixed strategy is needed.

      regards,
      Guillaume