0

Suppose we have class A which has a list of class B objects (List<B> listOfBs). In database design this means that table representing class B should have a foreign key referring to rows of table representing class A. Now, class B is a superclass of class C, D and E. My hibernate mapping file of class A is the following and works fine for storing class A entries to the database and also cascading at the same time multiple entries of class B that are contained in the list of class A.

Class A Hibernate Mapping File:

<hibernate-mapping package="..."> 
    <class name="A" table="table_of_A"> 
        <id name="key" column="A_ID"> 
            <generator class="native"/> 
        </id> 
        <property name="...." type="long"/>
        <property name="...." type="string"/>
        <list name="listOfBs" access="field" cascade="all">
               <key column="A_ID" not-null="true" />
               <list-index column="idx"/>
               <one-to-many class="B"/>
        </list>
    </class> 
</hibernate-mapping>

I am trying to figure out what I should include to the hibernate file shown above so I can store in the database the listOfBs which might containts objects of class C, D and E and therefore each of the object on the listOfBs should go to the appropriate table in the database. Note that I am using the table per concrete class with implicit polymorphism strategy for mapping inheritance on hibernate.

Any help would really appreciated. Thanks in advance.

Soc
  • 283
  • 3
  • 17
  • I did a more detailed description on the following link: [Detailed Description Link](http://stackoverflow.com/questions/17325742/hibernate-mapping-with-one-to-many-polymorphic-relationship) – Soc Jun 26 '13 at 16:43

1 Answers1

0

So here you are using table per concrete class and being B as a super-class u can use Generalization functionality provided by the hibernate.

In hibernate there are three different ways by which inheritance can be represented.

1. Table per class hierarchy

2. Table per subclass

3. Table per concrete class

Since you are using table per concrete class you will need to change class B mapping as per below

<class name="B">
<id name="id" column="ID">
    <generator class="sequence"/>
</id>
<property name="name" column="NAME"/>
<union-subclass name="C" table="C">
    <property name="${properties}" column="collumns"/>
</union-subclass>
<union-subclass name="D" table="D">
    <property name="${properties}" column="collumns"/>
</union-subclass>
<union-subclass name="E" table="E">
    <property name="${properties}" column="collumns"/>
</union-subclass>
</class>

This is the reference link,

Kartik73
  • 503
  • 6
  • 19
  • I tried that myself too and the `` gives me "org.hibernate.exception.SQLGrammarException: could not get next sequence value" error. I actually used ` B_id_seq `. I am using HSQL DB if that helps. – Soc Jun 25 '13 at 14:30
  • try using 'native' or 'increment'. – Kartik73 Jun 25 '13 at 14:47
  • 'native' does not work with `union-subclass`. Increment gives me `org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator`. – Soc Jun 25 '13 at 15:10