-->
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: Multilevel class hierarchy
PostPosted: Tue Aug 01, 2006 11:57 am 
Newbie

Joined: Tue Aug 01, 2006 11:47 am
Posts: 2
I' using Hibernate 2 and I' m trying to map a multilevel class hierarchy.
I used a table per subclass mapping.
A leaf class has a many-to-one association.
When I try to save an object of this leaf class, the object is saved,
but doesn' t the collection associated to the object.
Does anyone know how can I make a correct multilevel class hierarchy mapping?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 02, 2006 12:44 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You'll have to be more explicit, as what you've given is a bit confusing. There's a many-to-one association, and a collection, in the same subclass? And you want a save to be cascade to the collection? I'm confused.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 02, 2006 4:28 am 
Newbie

Joined: Tue Aug 01, 2006 11:47 am
Posts: 2
Excuse me for my imprecision.
This is an example of my mapping:
Code:
    <class name="ClassA" table="CLASS_A">
        <id name="id" type="long" column="ID" unsaved-value="null">
            <generator class="hilo">
                <param name="table">CLASA_HI_VALUE</param>
                <param name="column">NEXT_VALUE</param>
                <param name="max_lo">100</param>
            </generator>
        </id>
        <property name="name" type="string" column="NAME" not-null="true" />
        <property name="description" type="string" column="DESCRIPTION" />
        <set name="messages" table="MESSAGES_CLASSA" >
            <key column="CLASSA_ID" />
            <many-to-many class="Message" column="MESSAGE_ID" />
        </set>                 
        <joined-subclass name="ClassB" table="CLASS_B">
            <key column="ID" />
            <property name="property1" type="string" column="PROPERTY_1" />
            <property name="property2" type="int" column="PROPERTY_2" />
        </joined-subclass>
        <joined-subclass name="ClassC" table="CLASS_C">
            <key column="ID" />
            <property name="property3" type="string" column="PROPERTY_3" />
            <property name="property4" type="int" column="PROPERTY_4" />
            <property name="property5" type="int" column="PROPERTY_5" />
            <set name="objectSet" inverse="true" cascade="all-delete-orphan" >
           <key column="CLASSC_REF" />
           <one-to-many class="MyClass" />
            </set>
            <joined-subclass name="ClassD" table="CLASS_D">
           <key column="ID" />
                <property name="property6" type="int" column="PROPERTY_6" />
            </joined-subclass>
            <joined-subclass name="ClassE" table="CLASS_E">
           <key column="ID" />
                <property name="property7" type="int" column="PROPERTY_7" />
            </joined-subclass>
        </joined-subclass>
    </class>

    <class name="MyClass" table="MYCLASS">
        <id name="id" type="long" column="ID" unsaved-value="null">
            <generator class="hilo">
                <param name="table">MYCLASS_ID_HI_VALUE</param>
                <param name="column">NEXT_VALUE</param>
                <param name="max_lo">100</param>
            </generator>
        </id>
        <many-to-one name="objectClassC" class="ClassC" column="CLASSC_REF" cascade="all" />
        <property name="myproperty" type="int" column="MYPROPERTY" />
    </class>


As you can see, ClassC has 2 subclasses: ClassD and ClassE.
Moreover, ClassC has a Set of MyClass objects.
This is the code I use to create a ClassD object with a new set of
MyClass objects:

Code:
    ClassD obj = new ClassD();
    ....
    ....
    MyClass myobj = new MyClass();
    ....
    myobj.setObjectClassC(obj);
    obj.getObjectSet().add(myobj);
    ....
    tx = sess.beginTransaction();
    session.save(obj);
    tx.commit();

obj is saved, but doesn' t myobj.
Therefore, in the database obj has an empty set.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 02, 2006 5:18 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You have inverse="true" on the objectSet <set>. This tells hibernate that it is allowed put existing items into the set, and remove items from the set, but it's not allowed to create or delete items. Remove the inverse="true" to make it work, or explicitly save myobj before saving obj.

_________________
Code tags are your friend. Know them and use them.


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.