Mapping a one to many relationship with a join table

51 views
Skip to first unread message

jksoft

unread,
Mar 14, 2012, 5:04:10 PM3/14/12
to nhu...@googlegroups.com
First off, I am relatively new to NHibernate and so far finding good documentation has been a trial.

I am in the processing of using NH to connect to a legacy application which also includes some new tables we are adding for this project.  The database makes extensive use of join tables to link two entities together.  An example would be

Company --> CompanyToDocument <-- Document
CompanyToDocument
- companyId
- documentId

While this is a traditional way of doing Many to Many relationships, it really is a One to Many as each Document can only be associated with one Company.

I have been racking my brain trying to figure out how to model this in the application and use NH to map appropriately.  I really want my Document class to either have a "companyId" property or a Company property, not a collection of Companies which is the natural mapping for a Many to Many.

One idea I came up with was to put both a Company and a Companies property on my Document class so the NH Many to Many association would work.  The problem with this is I still can't do a query using my companyId property since NH doesn't know anything about it.  One thought I had is to use a Join association to map the companyId property.  I suppose another thought would be to tweak the query to join back to the Join table.

I wanted to seek out some advice as the pieces just don't seem to be fitting in place which makes me think I must be doing something wrong here.

I am open to any and all ideas here as I am stumped on this.  

Thanks for any advice,

J

Surya Pratap

unread,
Mar 14, 2012, 5:30:34 PM3/14/12
to nhu...@googlegroups.com
Hi,

One way for doing this is to use the documentid as a primary key(or apply a unique constraint) in the CompanyToDocument table so that it shares a one-to-one relationship with Document table.

I think the OR Map too is clear in this case.

Regards
Surya

--
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/-/x8eTnz-LzOUJ.
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.

Oskar Berggren

unread,
Mar 15, 2012, 5:34:42 AM3/15/12
to nhu...@googlegroups.com
The sum it up, the problem is that you have a many-to-many relation on
one side, but want it to be a one-to-many on another side.

Using the <join> element to treat CompanyToDocument and Document as
both being part of the Document entity sounds like a way forward.

/Oskar


2012/3/14 jksoft <jer...@digiwebstudios.com>:

Ricardo Peres

unread,
Mar 15, 2012, 7:42:13 AM3/15/12
to nhu...@googlegroups.com
But are updates to the <join>ed properties persisted?

> nhusers+unsubscribe@googlegroups.com.

jksoft

unread,
Mar 15, 2012, 9:53:07 AM3/15/12
to nhu...@googlegroups.com
I understand adding the unique constraint  to CompanyToDocument which makes the database schema consistent with how we are treating the data.  With that being said, the OR Map may be clear for you but it is not for me.  Could you tell me how you see the map looking in this case?

J


On Wednesday, March 14, 2012 5:30:34 PM UTC-4, Surya Pratap wrote:
Hi,

One way for doing this is to use the documentid as a primary key(or apply a unique constraint) in the CompanyToDocument table so that it shares a one-to-one relationship with Document table.

I think the OR Map too is clear in this case.

Regards
Surya

On Thu, Mar 15, 2012 at 2:34 AM, jksoft wrote:
First off, I am relatively new to NHibernate and so far finding good documentation has been a trial.

I am in the processing of using NH to connect to a legacy application which also includes some new tables we are adding for this project.  The database makes extensive use of join tables to link two entities together.  An example would be

Company --> CompanyToDocument <-- Document
CompanyToDocument
- companyId
- documentId

While this is a traditional way of doing Many to Many relationships, it really is a One to Many as each Document can only be associated with one Company.

I have been racking my brain trying to figure out how to model this in the application and use NH to map appropriately.  I really want my Document class to either have a "companyId" property or a Company property, not a collection of Companies which is the natural mapping for a Many to Many.

One idea I came up with was to put both a Company and a Companies property on my Document class so the NH Many to Many association would work.  The problem with this is I still can't do a query using my companyId property since NH doesn't know anything about it.  One thought I had is to use a Join association to map the companyId property.  I suppose another thought would be to tweak the query to join back to the Join table.

I wanted to seek out some advice as the pieces just don't seem to be fitting in place which makes me think I must be doing something wrong here.

I am open to any and all ideas here as I am stumped on this.  

Thanks for any advice,

J

--
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/-/x8eTnz-LzOUJ.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+unsubscribe@googlegroups.com.

jksoft

unread,
Mar 15, 2012, 9:54:54 AM3/15/12
to nhu...@googlegroups.com
Can I have a join on the Document side and a Many to Many on the Company side?  Without the Many to Many, I don't know how else to get NH to utilize the join table.  Perhaps I am missing something.

J


On Thursday, March 15, 2012 5:34:42 AM UTC-4, Oskar Berggren wrote:
The sum it up, the problem is that you have a many-to-many relation on
one side, but want it to be a one-to-many on another side.

Using the <join> element to treat CompanyToDocument and Document as
both being part of the Document entity sounds like a way forward.

/Oskar


2012/3/14 jksoft :

Reply all
Reply to author
Forward
0 new messages