0

I have found solution for my task here: mixing-joined-and-single-table-inheritance. Question contains example where mixed two inheritance strategies: joined with single table. But, is it possible to convert this annotation mapping to XML mapping?

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "animals")
public abstract class Animal { ...

@MappedSuperclass
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
public abstract class Mammal extends Animal { ...

@Entity
@Table(name = "mammals")
@PrimaryKeyJoinColumn(name = "mammal_id")
@DiscriminatorValue(value = "dog")
public class Dog extends Mammal { ...

@Entity
@Table(name = "mammals")
@PrimaryKeyJoinColumn(name = "mammal_id")
@DiscriminatorValue(value = "cat")
public class Cat extends Mammal { ...
Community
  • 1
  • 1
user and
  • 550
  • 4
  • 9

1 Answers1

1

sorry yesterday I just posted some code excerpt conceptualized from one-level inheritance. The problem you encountered is caused by

    <discriminator column="type" type="string"/>

cannot be put under <subclass> and <joint-subclass> is not able to be nested. I try to run a similar code today to achieve the functions you may need. I tried to put <join> under <subclass> to simulate a <joint-subclass>, but <discriminator> will not work in this way.

So the only thing I can come up with is to make a one-to-one association between Animal and Mammal. Here is the code that I tried, it may look awkward for the concept.

    <hibernate-mapping>
<class name="bean.Animal" table="animal">
    <id name="id" type="java.lang.Integer">
        <column name="id" length="50" scale="0" />
    </id>
     <many-to-one name="mammal" 
    column="mammal_id"
    not-null="true"/>
</class>

<class name="bean.Mammal" table="mammal">
    <id name="mammal_id" type="java.lang.Integer">
        <column name="mammal_id" length="50" scale="0" />
    </id>
    <discriminator column="type" type="string"/>

     <subclass name="bean.Dog" extends="bean.Mammal" discriminator-value="dog">
        </subclass>
        <subclass name="bean.Cat" extends="bean.Mammal" discriminator-value="cat">
        </subclass>
</class>

And the result table is:


+------------------------------+
|Tables                        |
+------------------------------+
|animal                        |
|mammal                        |
+------------------------------+

And the schemas in those tables are

+------------------------------+
|animal                        |
+------------------------------+
|id                            |
|mammal_id                     |
+------------------------------+

+------------------------------+
|mammal                        |
+------------------------------+
|mammal_id                     |
|type                          |
+------------------------------+

Hope this will help. BTW, I am still wondering what is the reason that you don't use annotation to solve this problem since you have the solution, just curious:).

Shali Liu
  • 1,174
  • 13
  • 23
  • Did you try run similar mapping? My output stop on `ERROR ErrorLogger:57 - Error parsing XML (215) : The content of element type "joined-subclass" must match "(meta*,subselect?`... – user and Nov 07 '12 at 09:10