Hibernate: many-to-any mapping

Why

Convenient for a collection of objects implementing the same Java interface.

Code

Robot has a set of Animals. Animal is just an interface.

Robot.hbm.xml

    <set name="animals" table="robot_animals">
      <key column="robot_id"/>
      <many-to-any id-type="long">
        <column name="animal_class" not-null="true"/>
        <column name="animal_id" not-null="true"/>
      </many-to-any>
    </set>

Note: not-null=“true” makes that the primary key is a composite key of the three columns.

Reference

Hibernate Forums: many-to-any and schema export

Feedback

as
sa
Nov 9, 2012
#1