-->
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-one from parent
PostPosted: Tue Aug 10, 2004 6:58 am 
Newbie

Joined: Mon Apr 19, 2004 6:48 am
Posts: 3
[b]Hibernate version: 2.1[/b]

I need to have a many-to-one relationship between Company and Person.
(Don't ask why, I need to display the two records in specific places on the view, and many other reasons).

Now, i need to do something like this with my bean:

[Class Company]
private Person priPerson;
private Person secPerson;
private String priPid;
private String secPid;

... getter & setters ...

where priPid & secPid keeps the relationship for the two Person's related to Company.

The only (relevant) values stored in the Company record is the 2xId's of the Person record.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 10, 2004 7:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-declaration-manytoone


Top
 Profile  
 
 Post subject: many-to-one from parent (little more complex)
PostPosted: Wed Aug 11, 2004 4:46 am 
Newbie

Joined: Mon Apr 19, 2004 6:48 am
Posts: 3
I've read the document many times, and the one reffered to from the Forum page on Parent/Child relationships. I'm afraid they don't give a solution to my perticular problem.

The documents suggests a many-to-one defenition from the parent point of view and a one-to-many relationship from the childs point of view... not true.

Company one has [b]TWO[/b] child entries for People.
One People can be the child of multiple Company entries.

So I'm not storing the Company Key in the People record, I'm storing [b]TWO[/b] People Key's in the Company record.
Company bean:
[code]
private Person priPerson;
private Person secPerson;
private String priPid;
private String secPid;
//(priPerson & secPerson is not stored in Company table, only their key's priPid & secPid)
[/code]

I can see this moving to a many-to-many direction, but that's unnecessary overhead (another table to maintain) and I will need overhead code in my bean just to put the People entry in the correct variable, since I need primary and secondary to be distinct. (Pri can't be the first record found according to the key's, it has to be in the possition according to the key for Pri).[/b][/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 3:18 pm 
Beginner
Beginner

Joined: Mon Aug 09, 2004 12:31 pm
Posts: 47
Location: New York, NY, USA
You do need many-to-one, twice, in the parent.

Code:
       <many-to-one name='priPerson' class=Person' column='Pri_Person_id' unique='true'/>
       <many-to-one name='secPerson' class=Person' column='Sec_Person_id' unique='true'/>


Then your Company table has two foreign keys, Pri_Person_id and Sec_person_id, to the primary key of the Person table.

The Person mapping would list relationship to the Company.

I had nearly the same exact situation, and it worked for me.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 3:19 pm 
Beginner
Beginner

Joined: Mon Aug 09, 2004 12:31 pm
Posts: 47
Location: New York, NY, USA
Sorry, I dropped a word there:

Quote:
The Person mapping would list <b>NO</b> relationship to the Company.
[/quote]


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.