NHibernate many-to-one with a composite-id entity

603 views
Skip to first unread message

sternr

unread,
Nov 27, 2011, 4:34:06 AM11/27/11
to nhu...@googlegroups.com

How can I map a many-to-one relationship where the "one" entity has a composite-id? For example:

public class SingleEntity  
{  
 
public virtual int FirstId{get;set;}  
 
public virtual int SecondId{get;set;}  
 
public virtual string SomeData{get;set;}  
}  

The entity ManyEntity has (in addition to a primaryKey) columns for both SingleEntity's FirstId and SecondId, so I want to be able to mape the relationship so that ManyEntity would look like that:

public class ManyEntity  
{  
public virtual int Id{get;set;}  
public virtual SingleEntity Single{get;set;}  
public virtual string Name{get;set;}  
}  

How can I do it using NHibernate?

John Davidson

unread,
Nov 27, 2011, 6:45:13 AM11/27/11
to nhu...@googlegroups.com
See http://nhforge.org/blogs/nhibernate/archive/2010/07/01/nhibernate-and-composite-keys.aspx

which should give you a good start

John Davidson

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/nhusers/-/wgT99kI1eZgJ.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.

sternr

unread,
Nov 27, 2011, 7:20:32 AM11/27/11
to nhu...@googlegroups.com
Thanks for the reply John,

The link describes mapping "simple" composite-id scenarios, but what I require (due to my legacy DB's schema) is even more twisted - 
I need to be able to map a ManyToOne (not the other way around) relation using 2 columns as reference keys

John Davidson

unread,
Nov 27, 2011, 8:54:39 AM11/27/11
to nhu...@googlegroups.com
You need to define the composite key in Single as a Class as described in the reference article. Then that key class becomes a single property in the many-to-one mapping.

John Davidson

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/nhusers/-/BxsUfEfLbHIJ.

mynkow

unread,
Nov 28, 2011, 12:57:14 PM11/28/11
to nhu...@googlegroups.com

sternr

unread,
Dec 1, 2011, 10:55:05 AM12/1/11
to nhu...@googlegroups.com
Could you link to the part in the reference article or supply an example?
Thanks!
Reply all
Reply to author
Forward
0 new messages