-->
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.  [ 3 posts ] 
Author Message
 Post subject: noobie mapping question
PostPosted: Tue Jan 04, 2005 10:13 am 
Newbie

Joined: Wed Dec 08, 2004 9:44 am
Posts: 2
Apologies if the simplicity of my question offends, but I'm going round in circles, and I'd like to know if what I'm trying to achieve is actually possible.

My domain consists of a "person" object, and a "baggage" object. A person can have zero or more items of baggage, in a set. Baggage can also exist outside of a person.

I wish to create a person with a number of baggage objects, then save the person, which in turn will save all the new baggage objects.

I have played with a number of approaches, where hibernate either complains that it tries to update the nonexistent baggage objects, neglects the foreign keys, or my final problem, where the information appeared to be stored but would then not come back out. All user errors I'm sure.

In short, can I save a parent object, which in turn saves all the children objects. Where the child objects also have unique id's so they can be accessed individually? I'm hoping that the child object doesn't have to contain a reference to the parent. Can anyone point me to an example of this? I have found many examples that don't quite seem to cover my situation.

Hibernate version: 2.1

Mapping documents:
<hibernate-mapping>
<class name="airline.hibernate.Baggage" table="BAGGAGE">

<id name="baggageId" type="java.lang.Integer" column="baggage_id" unsaved-value="null" >
<generator class="sequence">
<param name="sequence">BAGGAGE_ID_SEQ</param>
</generator>
</id>

<many-to-one name="personId" column="person_id" class="airline.hibernate.Person"/>

<property name="description">
<column name="description" sql-type="varchar2(1000)" not-null="true"/>
</property>
</class>
</hibernate-mapping>


<hibernate-mapping>
<class name="airline.hibernate.Person" table="PERSONS">
<id column="PERSON_ID" name="personId" type="java.lang.Integer">
<generator class="sequence">
<param name="sequence">PERSON_ID_SEQ</param>
</generator>
</id>

<property
name="firstName"
type="java.lang.String"
column="FIRST_NAME"
not-null="true">
</property>
<property
name="lastName"
type="java.lang.String"
column="LAST_NAME"
not-null="true">
</property>

<set name="baggage" cascade="all">
<key column="person_id"/>
<one-to-many class="airline.hibernate.Baggage"/>
</set>

</class>
</hibernate-mapping>

Name and version of the database you are using:
Oracle 9i


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 04, 2005 10:41 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Please read http://www.hibernate.org/hib_docs/reference/en/html/example-parentchild.html, parent-child relationships are pretty well explained there.


Top
 Profile  
 
 Post subject: Bidirectional reliance
PostPosted: Tue Jan 04, 2005 12:48 pm 
Newbie

Joined: Wed Dec 08, 2004 9:44 am
Posts: 2
Thanks for the prompt reply.

I have a follow up query.

In the reference you mentioned and all examples I have seen, bidirectional relationships are used. However, I did note that in the hibernate docs covering collections http://www.hibernate.org/hib_docs/reference/en/html/collections.html#collections-onetomany , it was mentioned that if you relax the database constraints you may get away with not using bidirectional relations.

Is this a possibility or will I be severely limiting my options by trying to avoid bidirectional relationships?

I am concerned because ultimately in my scenario, my objects will be marshalled from xml and I may not be able to re-create bidirectional relationships.

Many thanks


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