-->
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: Many-to-Many question.
PostPosted: Fri Aug 06, 2004 1:29 pm 
Newbie

Joined: Mon Feb 09, 2004 11:14 am
Posts: 16
I have a Many-to-Many relationship set up using the Composite element model which seems to be working.

Parent
Child
ParentChildAssociation

I can insert a Parent without creating a ParentChildRelationship (and I am fine with that). But I can also insert a Child without creating a ParentChildRelationship. That seems like an odd thing.

Question. Is it possible to force the ParentChildRelationship to be created when the Child is inserted?


Top
 Profile  
 
 Post subject: Re: Many-to-Many question.
PostPosted: Fri Aug 06, 2004 1:38 pm 
Newbie

Joined: Tue Jun 29, 2004 10:14 am
Posts: 11
taa_sarge wrote:
I have a Many-to-Many relationship set up using the Composite element model which seems to be working.

Parent
Child
ParentChildAssociation

I can insert a Parent without creating a ParentChildRelationship (and I am fine with that). But I can also insert a Child without creating a ParentChildRelationship. That seems like an odd thing.

Question. Is it possible to force the ParentChildRelationship to be created when the Child is inserted?


You have defined a many to many relationship, not a parent-child relationship. Many to many does not have parent-child sematics. If you want to enforce parent child through the data model, you will need to redesign the data model. Hibernate is not involved.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 06, 2004 1:47 pm 
Newbie

Joined: Mon Feb 09, 2004 11:14 am
Posts: 16
Well maybe a bad example. In my example, a parent can have many children (2 kids in the family) . And a child can have many parents (mother and father).

I am asserting that a child cannot exist without first having a mother and father.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 06, 2004 2:00 pm 
Newbie

Joined: Tue Jun 29, 2004 10:14 am
Posts: 11
taa_sarge wrote:
Well maybe a bad example. In my example, a parent can have many children (2 kids in the family) . And a child can have many parents (mother and father).

I am asserting that a child cannot exist without first having a mother and father.


That's OK, but that is not what the data model will enforce as it stands.

You could perhaps use Hibernate to enforce it (because the database won't without changing the model) by intercepting Save for the Child table and validating that they have a parent. See 9.9. Interceptors in the documentation.


Top
 Profile  
 
 Post subject: Re: Many-to-Many question.
PostPosted: Fri Aug 06, 2004 2:00 pm 
Newbie

Joined: Fri Aug 06, 2004 1:54 pm
Posts: 1
taa_sarge wrote:
Well maybe a bad example. In my example, a parent can have many children (2 kids in the family) . And a child can have many parents (mother and father).

I am asserting that a child cannot exist without first having a mother and father.


If you want to enforce this rule, you can use 2 separate one-to-many relationships.

This will also eliminate the need to have a separate many-to-many mapping table, as your child objects can contain 2 parent references (father/mother). See below:

father(1) -> children(many)
mother(1) -> children(many)

child.getMother()
child.getFather()


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.