-->
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.  [ 5 posts ] 
Author Message
 Post subject: Foreign key not being set when saving Object child arrays
PostPosted: Fri Feb 13, 2004 10:20 pm 
Regular
Regular

Joined: Fri Feb 13, 2004 10:02 pm
Posts: 90
This is my hibernate file that contains all of the mappings for my classes.
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>

    <!-- org.teamcharlie.modal.Trigger root -->
    <class name="org.teamcharlie.modal.Trigger" table="Trigger">
        <id name="id" type="int" column="id" unsaved-value="0">
            <generator class="native"/>
        </id>
        <property name="name" column="name" type="string"/>
        <property name="state" column="state" type="string"/>
    </class>

    <!-- org.teamcharlie.modal.Instance root -->
    <class name="org.teamcharlie.modal.Instance" table="Instance">
        <id name="id" type="int" column="id" unsaved-value="0">
            <generator class="native"/>
        </id>
        <property name="name" column="name" type="string"/>
    </class>

    <!-- org.teamcharlie.modal.CIMMethod root -->
    <class name="org.teamcharlie.modal.CIMMethod" table="Method">
        <id name="id" type="int" column="id" unsaved-value="0">
            <generator class="native"/>
        </id>
        <many-to-one name="overRiddenMethod" column="overRiddenMethod" class="org.teamcharlie.modal.CIMMethod"/>
        <property name="methodName" column="methodName" type="string"/>
        <property name="returnType" column="returnType" type="string"/>
        <array name="triggers" table="Triggers" cascade="all">
            <key column="uid_triggers"/>
            <index column="idx_triggers"/>
            <one-to-many class="org.teamcharlie.modal.Trigger"/>
        </array>
        <array name="parameters" table="Parameters" cascade="all">
            <key column="uid_parameters"/>
            <index column="idx_parameters"/>
            <element column="elm" type="string"/>
        </array>
    </class>
    <!-- org.teamcharlie.modal.NamedElement root -->
    <class name="org.teamcharlie.modal.NamedElement" table="NamedElement">
        <id name="id" type="int" column="id" unsaved-value="0">
            <generator class="native"/>
        </id>
        <property name="name" column="name" type="string"/>
        <array name="qualifiers" table="Qualifier" cascade="all">
            <key column="uid_qualifiers"/>
            <index column="idx_qualifiers"/>
            <one-to-many class="org.teamcharlie.modal.Qualifier"/>
        </array>
    </class>

    <!-- org.teamcharlie.modal.Property root -->
    <class name="org.teamcharlie.modal.Property" table="Property">
        <id name="id" type="int" column="id" unsaved-value="0">
            <generator class="native"/>
        </id>
        <property name="name" column="name" type="string"/>
        <many-to-one name="overridenProperty" column="overridenProperty" class="org.teamcharlie.modal.Property"/>
        <array name="triggers" table="Trigger" cascade="all">
            <key column="uid_triggers"/>
            <index column="idx_triggers"/>
            <one-to-many class="org.teamcharlie.modal.Trigger"/>
        </array>
        <property name="type" column="type" type="string"/>
        <array name="qualifiers" table="qualifiers" cascade="all">
            <key column="uid_qualifiers"/>
            <index column="idx_qualifiers"/>
            <one-to-many class="org.teamcharlie.modal.Qualifier"/>
        </array>
        <property name="value" column="value" type="string"/>
    </class>

    <!-- org.teamcharlie.modal.Qualifier root -->
    <class name="org.teamcharlie.modal.Qualifier" table="Qualifier">
        <id name="id" type="int" column="id" unsaved-value="0">
            <generator class="native"/>
        </id>
        <property name="defaultValue" column="defaultValue" type="string"/>
        <property name="flavor" column="flavor" type="string"/>
        <property name="name" column="name" type="string"/>
        <property name="scope" column="scope" type="string"/>
        <many-to-one name="type" class="org.teamcharlie.modal.QualifierType"/>
        <property name="value" column="value" type="string"/>
    </class>

    <!-- org.teamcharlie.modal.QualifierType root -->
    <class name="org.teamcharlie.modal.QualifierType" table="QualifierType">
        <id name="id" type="int" column="id" unsaved-value="0">
            <generator class="native"/>
        </id>
        <property name="type" column="type" type="string"/>
    </class>

    <!-- org.teamcharlie.modal.Reference root -->
    <class name="org.teamcharlie.modal.Reference" table="Reference">
        <id name="id" type="int" column="id" unsaved-value="0">
            <generator class="native"/>
        </id>
        <property name="name" column="name" type="string"/>
        <many-to-one name="overridenProperty" column="overridenProperty" class="org.teamcharlie.modal.Property"/>
        <array name="triggers" table="Trigger" cascade="all">
            <key column="uid_triggers"/>
            <index column="idx_triggers"/>
            <one-to-many class="org.teamcharlie.modal.Trigger"/>
        </array>
        <property name="type" column="type" type="string"/>
        <array name="qualifiers" table="qualifiers" cascade="all">
            <key column="uid_qualifiers"/>
            <index column="idx_qualifiers"/>
            <one-to-many class="org.teamcharlie.modal.Qualifier"/>
        </array>
        <property name="value" column="value" type="string"/>
        <many-to-one name="referencedClass" column="referencedClass" class="org.teamcharlie.modal.CIMClass"/>
    </class>

    <!-- org.teamcharlie.modal.CIMClass root -->
    <class name="org.teamcharlie.modal.CIMClass" table="Class">
        <id name="id" type="int" column="id" unsaved-value="0">
            <generator class="native"/>
        </id>
        <property name="version" column="version" type="string"/>
        <array name="methods" table="Methods" cascade="all" inverse="true">
            <key column="uid_methods"/>
            <index column="idx_methods"/>
            <one-to-many class="org.teamcharlie.modal.CIMMethod"/>
        </array>
        <property name="name" column="name" type="string"/>
        <array name="properties" table="Property" cascade="all" inverse="true">
            <key column="uid_properties"/>
            <index column="idx_properties"/>
            <one-to-many class="org.teamcharlie.modal.Property"/>
        </array>
        <array name="triggers" table="Triggers" cascade="all" inverse="true">
            <key column="uid_triggers"/>
            <index column="idx_triggers"/>
            <one-to-many class="org.teamcharlie.modal.Trigger"/>
        </array>
        <many-to-one name="parentClass" column="parentClass" class="org.teamcharlie.modal.CIMClass"/>
        <array name="instances" table="Instances" cascade="all" inverse="true">
            <key column="uid_instances"/>
            <index column="idx_instances"/>
            <one-to-many class="org.teamcharlie.modal.Instance"/>
        </array>
        <joined-subclass name="org.teamcharlie.modal.Association" table="Association">
            <key column="class_id"/>
            <array name="references" table="Reference" cascade="all" inverse="true">
                <key column="uid_references"/>
                <index column="idx_references"/>
                <one-to-many class="org.teamcharlie.modal.Reference"/>
            </array>
        </joined-subclass>
        <joined-subclass name="org.teamcharlie.modal.Indication" table="Indication">
            <key column="class_id"/>
        </joined-subclass>
    </class>
    <!-- org.teamcharlie.modal.Schema root -->
    <class name="org.teamcharlie.modal.Schema" table="Schema">
        <id name="id" type="int" column="id" unsaved-value="0">
            <generator class="native"/>
        </id>
        <property name="version" column="version" type="string"/>
        <property name="name" column="name" type="string"/>
        <array name="classes" table="classes" cascade="all">
            <key column="uid"/>
            <index column="idx"/>
            <one-to-many class="org.teamcharlie.modal.CIMClass"/>
        </array>
    </class>


</hibernate-mapping>


In a JUnit test, using cactus I run the following setup to add 3 temporary objects to my database.
Code:

       super.setUp();

        //this sesion of code cleans up the database
        Session session = HibernateSession.getInstance().getSession();

        //Add A, B, and C to the database

        CIMClass A = new CIMClass();
        A.setName("A");
        A.setVersion("1");
        A.setProperties(
                new Property[]{
                    new Property(0, "A prop1", "String", "", null, null, null),
                    new Property(0, "A prop2", "String", "", null, null, null)
                }
        );

        A.setMethods(
                new CIMMethod[]{
                    new CIMMethod(0, "A method1", "void", new String[]{"param1", "param2"}, null, null),
                    new CIMMethod(0, "A method2", "void", new String[]{"param1", "param2"}, null, null)
                }
        );

        A.setInstances(new Instance[]{new Instance(0, "A instance")});

        CIMClass B = new CIMClass();

        B.setName("B");
        B.setVersion("1");

        //set the parent class to A
        B.setParentClass(A);

        B.setProperties(
                new Property[]{
                    new Property(0, "B prop1", "String", "", null, null, null),
                    new Property(0, "B prop2", "String", "", null, null, null)
                }
        );

        B.setMethods(
                new CIMMethod[]{
                    new CIMMethod(0, "B method1", "void", new String[]{"param1", "param2"}, null, null),
                    new CIMMethod(0, "B method2", "void", new String[]{"param1", "param2"}, null, null)
                }
        );

        B.setInstances(new Instance[]{new Instance(0, "A instance")});

        CIMClass C = new CIMClass();


        C.setName("C");
        C.setVersion("1");
        //Set parent Class
        C.setParentClass(A);

        C.setProperties(
                new Property[]{
                    new Property(0, "C prop1", "String", "", null, null, null),
                    new Property(0, "C prop2", "String", "", null, null, null)
                }
        );

        C.setMethods(
                new CIMMethod[]{
                    new CIMMethod(0, "C method1", "void", new String[]{"param1", "param2"}, null, null),
                    new CIMMethod(0, "C method2", "void", new String[]{"param1", "param2"}, null, null)
                }
        );

        C.setInstances(
                new Instance[]{
                    new Instance(0, "C instance 1"),
                    new Instance(0, "C instance 1")
                });

        //save them in the databse
        session.save(A);
        session.save(B);
        session.save(C);
        session.flush();
        session.close();


Hoever, when I look in the database, the key column is null for all of the child object arrays of the CIMClass. For instance, the Class table has the row.
Code:
'id','version','name','parentClass','uid','idx'
'16','1','A','[NULL]','[NULL]','[NULL]'


And these are two rows in the method table
Code:
'id','overRiddenMethod','methodName','returnType','uid_methods','idx_methods'
'31','[NULL]','A method1','void','[NULL]','[NULL]'
'32','[NULL]','A method2','void','[NULL]','[NULL]'


As you can see, the uid_methods property never gets set to the value of class.id. However when I delete the objects, all child objects in the array are deleted as well. Does anyone have any clue as to why this is happening or how to fix it?

Thanks,
Todd.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 14, 2004 5:36 pm 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
Probably because you have a single-directional relationship between Class and Method and in the one-to-many section in Class for the Methods inverse is set to true. This means setting the relationship in this direction ( adding to the methods array ) has no effect on the db. Try removing the inverse='true' in the CIMClass methods property. You should see the relationships getting updated then.

Check the generated SQL before and after to see what it is doing wrong now and what it does with the change.

Cheers.

Myk.


Top
 Profile  
 
 Post subject: Thanks for the help
PostPosted: Sat Feb 14, 2004 9:11 pm 
Regular
Regular

Joined: Fri Feb 13, 2004 10:02 pm
Posts: 90
Myk,
Thanks for the help, I removed the inverse="true", and now after flush is called the foreign keys are there. You're a lifesafer! We start implementation of our business logic on Monday, and this needed corrected by then.

Thanks again!
Todd


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 4:32 am 
Beginner
Beginner

Joined: Tue Nov 11, 2003 4:49 am
Posts: 47
Location: Florence, Italy
Making it inverse="true" is REALLY a good idea, cause can improve performance of your application.
The fact is that when you mantain relationship with inverse="true", you must mantain the relationship on the otherside. I mean that when you add Method to Class, you must also set Class into Method, that's what create the fk.
Also, remember that cascade="all" with inverse="true" is not possible.
You can read all I said on the documentation.
Ciao.

_________________
Ciao.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 5:39 am 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
inverse='true' is used for bidirectional relationships. This relationship only goes from Class to Method, there is no link from Method to class so inverse='true' shouldn't be used and it is not possible to set the Method's Class, this happens implicitly by adding to the Method array in Class.

Cheers.

Myk.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.