-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Table per Subclass - Child added in Parent Table?!
PostPosted: Mon Mar 08, 2010 6:04 am 
Beginner
Beginner

Joined: Thu Nov 26, 2009 8:20 am
Posts: 23
Hello,
I have a simple structure:
XYDiagram extends Diagram extends DiagramBase.
I want Hibernate to use the Table-per-Subclass inheritance strategy.
Everything is working fine so far but hibernate persists the child objects in the parent table.

Example:

I have an object of the type XYDiagram. I want to persist it. Hibernate inserts the object in the Table DiagramBase and not in the table XYDiagram (which exists!!).

My mapping files are:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <subclass
        name="...Diagram"
        extends="...DiagramBase">
       
    </subclass>

</hibernate-mapping>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class
        name="DiagramBase"
        table="DIAGRAMBASE">
       
        <id
            name="id"
            column="DIAGRAMBASE_ID">
            <generator class="native"/>
        </id>

      <discriminator column="DTYPE"/>

        <property
            name="name"
            column="DIAGRAM_NAME"/>
       
        <property
            name="lockUser"
            column="DIAGRAM_LOCK_USER"/>
       
        <property
            name="lockTimeStamp"
            column="DIAGRAM_LOCK_TIMESTAMP"/>
           
    </class>

</hibernate-mapping>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <joined-subclass
           name="XYDiagram"
        extends="Diagram"
        table="xydiagram">
       
        <key column="DIAGRAM_ID" foreign-key="DIAGRAMBASE_ID"></key>
       
    </joined-subclass>

</hibernate-mapping>


I would appreciate any help!

Thanks in advance & best regards
Alex


Top
 Profile  
 
 Post subject: Re: Table per Subclass - Child added in Parent Table?!
PostPosted: Mon Mar 08, 2010 6:23 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Actually with joined-subclass (Table per Subclass) strategy there should as well an insert in the
parent class (the common attributes) as too in the child-table (the child-specific attributes).
If you want to have just one insert in the apposite child-table,
then you must opt for union-subclass strategy indeed.
BTW: In your example you are mixing 'subclass' with 'joined-subclass' strategies within the same class hierarchy.
Be aware that not all kind of strategy mixtures are always guaranteed to be working.


Top
 Profile  
 
 Post subject: Re: Table per Subclass - Child added in Parent Table?!
PostPosted: Mon Mar 08, 2010 7:12 am 
Beginner
Beginner

Joined: Thu Nov 26, 2009 8:20 am
Posts: 23
First, thanks for your answer!

I tried to define the subclasses only in my parent classes xml-file, but it doesn't work.
Hibernate is inserting all XYDiagrams in the DiagramBase table.
Do I need some special tags for inserting the childs in another table?

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class
        name="DiagramBase"
        table="DIAGRAMBASE">
       
        <id
            name="id"
            column="DIAGRAMBASE_ID">
            <generator class="native"/>
        </id>

      <discriminator column="DTYPE"/>

        <property
            name="name"
            column="DIAGRAM_NAME"/>
       
        <property
            name="lockUser"
            column="DIAGRAM_LOCK_USER"/>
       
        <property
            name="lockTimeStamp"
            column="DIAGRAM_LOCK_TIMESTAMP"/>
           
         <set name="diagramElements" cascade="all-delete-orphan" lazy="false">
           <key><column name="DIRECT_DIAGRAM_PARENT_FK_ID"></column></key>
           <one-to-many class="DiagramElement"/>
         </set>
         
         <subclass name="XYDiagram" discriminator-value="XYDiagram">
             <join table="xydiagram">
                <key column="DIAGRAM_ID"/>
            </join>
        </subclass>
         
       <subclass
              name="Diagram"
              extends="DiagramBase">
       
       </subclass>
            
    </class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Table per Subclass - Child added in Parent Table?!
PostPosted: Mon Mar 08, 2010 8:12 am 
Beginner
Beginner

Joined: Thu Nov 26, 2009 8:20 am
Posts: 23
Hello again,
sorry I misunderstood your post first.
I solved the problem by defining explicit sql-insert statements in the subclasses...

Thanks for your advice :)
Best regards, Alex


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.