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.  [ 9 posts ] 
Author Message
 Post subject: How to create a simple foreign keys?
PostPosted: Sun Oct 04, 2009 5:39 am 
Newbie

Joined: Sun Oct 04, 2009 5:36 am
Posts: 8
Problem1:
I have a project where NHib creates the database from mapping file. The problem is that I want to add a ForeignKey without defining any set, map, list or bag. Only foreign key without any mapping.

For example: I have [Warehouse] table and [Transfer] table. I transfer products from one warehouse to another. In [Transfer] I have two columns call [Warehouse_From] and [Warehouse_To]. How can I define in mapping file two ForeignKeys to the parent table [Warehouse]?

Problem2:
Is it possible this kind of relation and how to define it in NHib. I have [Parent] table and [Child] table. I need a foreign key and in the child class I have IList for example.


Top
 Profile  
 
 Post subject: Re: How to create a simple foreign keys?
PostPosted: Sun Oct 04, 2009 8:35 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi mynkow,

have two fields in your Transfer class and map them both <many-to-one> to the Warehouse class.
Code:
<class name="Transfer">
  ...
  <many-to-one name="..." column="Warehouse_From" class="Warehouse" .../>
  <many-to-one name="..." column="Warehouse_To" class="Warehouse" .../>
  ...
</class>

There is no need that the Warehouse class has any knowledge about that.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: How to create a simple foreign keys?
PostPosted: Sun Oct 04, 2009 9:06 am 
Newbie

Joined: Sun Oct 04, 2009 5:36 am
Posts: 8
Will this mapping create a foreign key constraint in the database?

PS: I am not able to test it today.


Top
 Profile  
 
 Post subject: Re: How to create a simple foreign keys?
PostPosted: Sun Oct 04, 2009 9:16 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi mynkow,

mynkow wrote:
Will this mapping create a foreign key constraint in the database?


Two, actually. (If you use a DB that supports FKs.)

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: How to create a simple foreign keys?
PostPosted: Sun Oct 04, 2009 11:02 am 
Newbie

Joined: Sun Oct 04, 2009 5:36 am
Posts: 8
How can I specify the foreign key name?


Top
 Profile  
 
 Post subject: Re: How to create a simple foreign keys?
PostPosted: Sun Oct 04, 2009 11:43 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi mynkow,

RTFM: Many-to-one.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: How to create a simple foreign keys?
PostPosted: Sun Oct 04, 2009 11:46 am 
Newbie

Joined: Sun Oct 04, 2009 5:36 am
Posts: 8
Hi Froestel,

I want to avoid mapping with bag, list, set or any of those. I want to create a mapping file and define a foreign key constraint within that file for some columns without creating any collections in my classes.


Top
 Profile  
 
 Post subject: Re: How to create a simple foreign keys?
PostPosted: Sun Oct 04, 2009 12:05 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi mynkow,

so just do what I posted above. There's no collection involved. This simply associates the two fields of type Warehouse in your Transfer class with the Warehouse class for Hibernate. This way Hibernate can tell which items are related to a certain Transfer instance. Hibernate will do that by creating two FKs pointing from the table that keeps the Transfer instances to the table that holds the Warehouse instances.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: How to create a simple foreign keys?
PostPosted: Mon Oct 05, 2009 6:19 am 
Newbie

Joined: Mon Oct 05, 2009 4:47 am
Posts: 1
I want to create a mapping file and define a foreign key constraint within that file for some columns without creating any collections.

_________________
registry cleaner
affiliate training online


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