-->
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: Parent/Child Mapping Questions
PostPosted: Fri Oct 21, 2005 11:55 pm 
Newbie

Joined: Fri Oct 21, 2005 10:45 pm
Posts: 2
I have a pretty lazy requiremet from the users to save/get/delete the parent object, but it also requires to save/get/delete all of the objects inside the parent object.

Put it simple, let's say we have three classes:
Class Parent {
int parent_id;
Set children; //hold the Child objects
String pname;
}

Class Child{
int child_id;
Set grandsons; //hold the Grandson objects
String cname;
}

Class Grandchild{
int grandchild_id;
String gname;
}


My task is to create a simple API to wrap the Hibernate API and let the users to do three things:

1. void save (Parent p);
//save all of the Child and Grandchild to database

2. Parent get(Parent.class, Serilizable parent_id);
//get all of the Child objects and Grandchild objects held by this Parent object with the specified id, which was persisted to the database previously by calling save(Parent p) method.

3. void delete(Parent.class, Serializable parent_id);
//delete all of the Child and Grandchild objects held by this Parent object with the given parent_id which was persisted to the database previously by calling the save(Parent p) method.


The implementation of these three functions seems to be pretty straight forward, but I have the problem to create the mapping file. Since there are more properties related to Parent, Child, Grandchild objects, I would like to create at least one table in database for each entity (of course, we might have also association tables). That means, the composite mapping might not be an option dictated by the user here.

Can someone give me some directions? If this requirement could cause some issues or not legitimate, please also comment.

By the way, I am using v2.1.8.



Thanks much!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 22, 2005 12:07 am 
Regular
Regular

Joined: Tue Mar 01, 2005 2:35 pm
Posts: 60
I would have a Person super class, and then have Parent, Child, and Grandchild each extend Person. Hibernate is very good at mapping this sort of relationship to a database:

http://www.hibernate.org/hib_docs/v3/re ... tance.html

_________________
I always mark helpful posts as such.


Top
 Profile  
 
 Post subject: Re: Parent/Child Mapping Questions
PostPosted: Sat Oct 22, 2005 12:32 am 
Beginner
Beginner

Joined: Tue Oct 18, 2005 3:57 pm
Posts: 48
Location: Los Angeles, CA
ywang1000 wrote:
I have a pretty lazy requiremet from the users to save/get/delete the parent object, but it also requires to save/get/delete all of the objects inside the parent object.

Is this something the users actually demanded, or something the analysts thought up? :) Before you go any further, you should clarify this requirement a bit, i.e. when you load the parent, do you also need to load all children and grandchildren? This can have an impact later on when more and more children/grandchildren are added to your tree(s).

Now, assuming everything you've posted is *the* requirement, you just need some one-to-many mappings. For example:
Code:
<set name="children">
  <key column="PARENT_ID"/>
  <one-to-many column="CHILD_ID" class="Child"/>
</set>

Of course, you can also specify lazy="true" to lazy load children/grandchildren as needed, but that really depends on your requirement...

jd
____________________
Don't forget to rate!


Top
 Profile  
 
 Post subject: Re: Parent/Child Mapping Questions
PostPosted: Sun Oct 23, 2005 10:36 am 
Newbie

Joined: Fri Oct 21, 2005 10:45 pm
Posts: 2
jd001982 wrote:
ywang1000 wrote:
I have a pretty lazy requiremet from the users to save/get/delete the parent object, but it also requires to save/get/delete all of the objects inside the parent object.

Is this something the users actually demanded, or something the analysts thought up? :) Before you go any further, you should clarify this requirement a bit, i.e. when you load the parent, do you also need to load all children and grandchildren? This can have an impact later on when more and more children/grandchildren are added to your tree(s).

Now, assuming everything you've posted is *the* requirement, you just need some one-to-many mappings. For example:
Code:
<set name="children">
  <key column="PARENT_ID"/>
  <one-to-many column="CHILD_ID" class="Child"/>
</set>

Of course, you can also specify lazy="true" to lazy load children/grandchildren as needed, but that really depends on your requirement...

jd
____________________
Don't forget to rate!


jd, thanks. They are absolutely the "requirements" needed by the users (for get/save/delete operations)...

Is this OK to specify table attribute such as table="parent_child" for the set children? Do I need specify cascade="all" for the set children? By the way, "column" is an invalid attribute for the one-to-many assoication.

Again, thanks.


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.