-->
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.  [ 12 posts ] 
Author Message
 Post subject: another Parent/Child Problem (sorry)
PostPosted: Wed Dec 03, 2003 5:22 am 
Newbie

Joined: Wed Sep 03, 2003 4:59 am
Posts: 12
Hy,

I'm trying to persist 2 classes Parent and Child:
class Parent{
private String id;
private Child myPreferedChild;
private String name;
//getters and setters
}

class Child{
private String id;
private Parent myOnlyParent;
private String name;
//geters and setters
}

For the moment, there is no one-to-many relation between parent and childrens (the parent only knows its prefered child).

In the model, there is one table for each class.
A parent HAS obligatory a prefered child (the column is not nullable).
The child may not have a Parent (the column is nullable)

I used two relation "many-to-one" with cascade="all".
I used not-null="true" on the parent->child relationship and not-null="false" on the child->parent relationship.

A use case is to create a child that have a Parent that is not already in the database.
So the code is like :
Child myNewChild = new Child();
myNewChild.setName("newChild");
Parent myParent = new Parent();
myParent.setName("new parent");
myParent.setPreferedChild(myNewChild);
myNewChild.setParent(myParent);

//persist the child
session.save(myNewChild);

When I do this, the error is that hibernate is trying to save the Parent first and I get an exception saying that the PREFERED_CHILD_ID cannot be NULL.

Is there anyone who could help me for this ?

Thanks
joe


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2003 6:01 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
What is value fro "inverse" attribute for both mappings?

Or just show the mapping...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2003 6:38 am 
Newbie

Joined: Wed Sep 03, 2003 4:59 am
Posts: 12
Here is the mapping :

Code:
    <class
        name="Child"
        table="CHILD"
        dynamic-update="false"
        mutable="true"
    >

        <id
            name="id"
            column="ID"
            type="java.lang.Long"
            unsaved-value="null"
        >
            <generator class="sequence">
                <param name="sequence">CHILD_SEQ</param>
            </generator>
        </id>

        <many-to-one
            name="parent"
            class="Parent"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="PARENT_ID"
            not-null="false"
        />

        <property
            name="activityCode"
            type="java.lang.String"
            update="true"
            insert="true"
            column="NAME"
        />
</class>


    <class
        name="Parent"
        table="Parent"
        dynamic-update="true"
        mutable="true"
    >

        <id
            name="id"
            column="ID"
            type="java.lang.Long"
            unsaved-value="null"
        >
            <generator class="sequence">
                <param name="sequence">PARENT_SEQ</param>
            </generator>
        </id>

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="NAME"
        />

        <many-to-one
            name="preferedChild"
            class="Child"
            cascade="all"
            outer-join="auto"
            update="true"
            insert="true"
            column="PREFERED_CHILD_ID"
            not-null="true"
        />

    </class>
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2003 6:40 am 
Newbie

Joined: Wed Sep 03, 2003 4:59 am
Posts: 12
I am actually trying some combinations for the attributes "cascade" to call explicitly save() on the child, then on the parent...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2003 6:46 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Since cascade="none" for the Child's parent property, when you save child, Hibernate will not save its parent automatically.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2003 6:59 am 
Newbie

Joined: Wed Sep 03, 2003 4:59 am
Posts: 12
Yeah, I know, but as I was saying, I'm currently modifying it.
For the moment, I resolve It by saying that the preferedChild is just a Long object that I set myself.

Thanks
joe


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2003 7:10 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
I think you need to mark one of the ends of your bi-directional relationship as "inverse" end.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2003 7:21 am 
Newbie

Joined: Wed Sep 03, 2003 4:59 am
Posts: 12
I don't think so, because, here it's not real Bi-directionnal relations.
It is just 2 many-to-one relations, on which you cannot put the attribute inverse="true".

Anyway, I obtain something working, so it's OK for me.
I'm still working on it....

Thanks for your responses

Joe


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2003 7:25 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Oh sorry, you are right - "inverse" is not available for many-to-one.

Don't you think one-to-one is better suited for your needs?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2003 7:30 am 
Newbie

Joined: Wed Sep 03, 2003 4:59 am
Posts: 12
I don't think so, There is no link between the parent id and the child id...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2003 9:19 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
In hibernate 2.1 you can use the property-ref attribute in one-to-one.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2003 9:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
correct, as Emmanuel says, use a <one-to-one> with a property-ref.


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