-->
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.  [ 14 posts ] 
Author Message
 Post subject: How to map more parents to the same child?
PostPosted: Tue Aug 02, 2005 10:25 pm 
Beginner
Beginner

Joined: Fri Jan 07, 2005 11:07 am
Posts: 30
hi,

i have a design question regarding how to map more than one parent
to the same child
.

the relationship between a parent and the child is one to many.

the parent objects' id needs to be a number (Long).

the business background is that a few objects need
to have dated notes regarding when an object was changed, why, etc.
i want these objects to use the same Note class/table. i am building
a new application.

i searched this forum and found a few related posts but not helpful to me on this. i myself tried different mappings, but none of them work or Hibernate does not allow me in terms of syntax.

could somebody shed some light on this? how to write mappings to
meet the above requirement? in Java, such relationships are so easy
to express.

thanks you so much!

pete


Top
 Profile  
 
 Post subject: Re: How to map more parents to the same child?
PostPosted: Tue Aug 02, 2005 11:33 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
pjydc wrote:
hi,

i have a design question regarding how to map more than one parent
to the same child
.

the relationship between a parent and the child is one to many.

the parent objects' id needs to be a number (Long).

the business background is that a few objects need
to have dated notes regarding when an object was changed, why, etc.
i want these objects to use the same Note class/table. i am building
a new application.

i searched this forum and found a few related posts but not helpful to me on this. i myself tried different mappings, but none of them work or Hibernate does not allow me in terms of syntax.

could somebody shed some light on this? how to write mappings to
meet the above requirement? in Java, such relationships are so easy
to express.

thanks you so much!

pete


Isn't more than one child with the same parent just a many-to-many relationship ?

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 12:03 am 
Beginner
Beginner

Joined: Fri Jan 07, 2005 11:07 am
Posts: 30
Quote:
Isn't more than one child with the same parent just a many-to-many relationship ?


thanks for your input. maybe i was unclear in my above post. a child object can only belong to one parent. so it is not many-to-many relationship. or i missing something.

i made many attempts but simply was unable to write correct mapping by using composite key, one-to-many relationship, etc...

the tough thing is that the parent id (a number, i need to use a number and lets's accept it) and the child id together cannot uniquely identify a child because different parents can have same IDs. obviously, we cannot have a CHILD_ID column in parent tables because a parent object can have many children. we cannot have a PARENT_ID in the child table either because different parents can have the same id.

i was thinking using the parents' class name as an additional element in uniquely identifying a child. however, i am simply unable to write correct mappings.

thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 12:15 am 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
Quote:
different parents can have same IDs

So how do you identify a specific parent? Put another way, what is the primary key of a parent? If different parents can have the same ID, you have bigger problems, because you can never retrieve a particular parent.


Top
 Profile  
 
 Post subject: Does it realli a child?
PostPosted: Wed Aug 03, 2005 3:26 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
IMO the Note object should not be a child, but needs to be maintained separately, although that might happens quite transparently, for example:
Your parents would implement
Public interface Stamped{
List getNotes();
Void setNotes();
Serializable getId();
String getType();
}


Then in your business methods (DAO ) you could have something like:

Object read( id, Clazz ){
Object o = session.load( id, clazz);
If( o instanceof Stamped){
Stamped s = (Stamped) o;
s.setNotes( findNotesFor( s ) );
}
}


void saveOrUpdate( o ){
session. saveOrUpdate ( o);
If( o instanceof Stamped){
Stamped s = (Stamped) o;
s.setNotes( saveNotes( s ) );
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 5:40 am 
Newbie

Joined: Sat Jul 23, 2005 8:31 am
Posts: 18
if you take instead of a "one to many" a "one to one to may" with a table inbetween it works well... at least for me and if i understood you correctly.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 1:58 pm 
Beginner
Beginner

Joined: Fri Jan 07, 2005 11:07 am
Posts: 30
eagle79 wrote:
Quote:
different parents can have same IDs

So how do you identify a specific parent? Put another way, what is the primary key of a parent? If different parents can have the same ID, you have bigger problems, because you can never retrieve a particular parent.


Each parent has its own table. Each parent's ID is generated as follows:

<id name="id" type="long" column="ID">
<generator class="native"/>
</id>

So it is apparent that different parents can have same ID numbers because they come from differnent tables/classes. There is no problem for using ID for each parent individually. The problem is how to identify the parent for a child when multiple parents share the same Child table.
That is why I am thinking about using each parent's class name as an additional element.

If each parent used "uuid.hex" instead of "native", I would have no problem at all in writing Hibernate mapping file. Now each parent uses a number.

If "uuid.hex" is the only option, it seems to me that Hibernate misses something or not so powerful in mapping. Maybe I am wrong.


Top
 Profile  
 
 Post subject: mapping
PostPosted: Wed Aug 03, 2005 2:17 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Lets consider FK, we could not create FOREIGN KEY that will check integrity of reference from child to multip[le parents, but we can create ON UPDATE trigger that will do the job.
FK is analogous to mapping - very straightforward and efficient in vast majority of cases, but sometimes we have to resort to code: triggers of application level logic.


Top
 Profile  
 
 Post subject: Re: mapping
PostPosted: Wed Aug 03, 2005 2:58 pm 
Beginner
Beginner

Joined: Fri Jan 07, 2005 11:07 am
Posts: 30
kgignatyev wrote:
Lets consider FK, we could not create FOREIGN KEY that will check integrity of reference from child to multip[le parents, but we can create ON UPDATE trigger that will do the job.
FK is analogous to mapping - very straightforward and efficient in vast majority of cases, but sometimes we have to resort to code: triggers of application level logic.


The trigger approach sounds interesting to me. I have never done things like that. Do you have any pointers so that I can pursue?
Do you know whether this trigger approach will work in retrieving children by a parent?

I am not sure how this post is related to your above post.

Regards.


Top
 Profile  
 
 Post subject: triggers
PostPosted: Wed Aug 03, 2005 3:25 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Quote:
The trigger approach sounds interesting to me. I have never done things like that. Do you have any pointers so that I can pursue?

http://www-db.stanford.edu/~ullman/fcdb ... ggers.html
see Aborting Triggers with Error.

Quote:
Do you know whether this trigger approach will work in retrieving children by a parent?

It will not.

I used FK vs Triggers analogy to illustrate that not everything could be done via mapping, sometimes we need to code. I do not thing that Hibernate should support multitable references, IMO that belongs to application logic.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 4:56 pm 
Beginner
Beginner

Joined: Fri Jan 07, 2005 11:07 am
Posts: 30
kgignatyev,

thank you for your input.

Quote:
I do not think that Hibernate should support multitable references


Hibernate actually supports the type of problem I am facing. If I used
"uuid.hex" approach to generate parent IDs, there would be no problem
at all. However, since I am using number for parent ID, I have this
mapping problem. Simply failed to find a right way to write mappings.

As you understand, I would like to avoid to other approaches such as triggers as much as possible, unless Hibernate alone really cannot solve
my design issue.

Regards.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 5:06 pm 
Beginner
Beginner

Joined: Fri Jan 07, 2005 11:07 am
Posts: 30
chrigi wrote:
if you take instead of a "one to many" a "one to one to may" with a table inbetween it works well... at least for me and if i understood you correctly.


chrigi,

thanks for your input. i am not sure what was your thinking.
i thought about this approach before, but it looks to me not working.
could you please elaborate a bit on your recommendation?

thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 04, 2005 4:29 am 
Newbie

Joined: Sat Jul 23, 2005 8:31 am
Posts: 18
as much as i understood you have one sort/class of children and more then one sort/class of parents. so for example P1 and P2 as parents and C as child. now each child instance can have one parent and each parent can have multiple children.
the problem you now have is to know to which parent one child belongs. what you are thinking about is, is to add a property which identifies the parenttype by classname.
another solution is to add two references to C, one to P1 and one to P2. for two different parent types this is no problem, but for 10 it gets messy. the solution for this is to use a table in between with just an ID in it. so the parents of the children are actually of the same type and they get referenced by the real type. this works only if it's an unidirectional mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 04, 2005 10:06 am 
Beginner
Beginner

Joined: Fri Jan 07, 2005 11:07 am
Posts: 30
Quote:
now each child instance can have one parent and each parent can have multiple children.


Exactly!

Quote:
use a table in between with just an ID in it. so the parents of the children are actually of the same type and they get referenced by the real type. this works only if it's an unidirectional mapping.


i was thinking about this in-between table approach too, but after many attempts eventually it looked to me not working. could you please elaborate the structure of this table? can you write workable Hibernate mapping? what is the "unidirectional mapping"? the unidirection in my case is from parent to child.

regards.


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