Solved

Map Reference Table with NHibernate

Posted on 2009-05-05
4
1,010 Views
Last Modified: 2012-05-06
I'm trying to map out some classes to use with NHibernate.  I have most things correct, however, I'm trying to map a reference table for Directors for movies.  

We have a few tables in the database, one being a Movies table that holds details about a particular movie.  Another table a Directors table that holds details pertaining to movie directors.  The problem is many directors can direct one movie, and one director can direct multiple movies.  So we created a reference table to show this:

MovieId | DirectorId
----------------------
      1             2
      2             2
      3             2
      1             4
      5             7

This way we can pull up all movies directed by a director and also list all directors for a particular movie.

How can I map this in NHibernate so that when I reference my Movie object I have a collection of directors for that movie, if any, and when I access my Director object, I have a collection of movies directed by him/her, if any?

Such as:

Movie movie = new Movie();
movie.Directors (type of IList<Directors>)  or something to that extent.

Director director = new Director();
director.Movies  (same as above IList<Movie>)

Thanks.
0
Comment
Question by:Phreak3eb
4 Comments
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24310151
Create an association from Movie Table  ID to ReferenceTable.MovieID
Create an association from Director Table ID to Reference Table DirectorID

This is how you do it using dbml and Linq not sure if NHibernate is the same.

When the association is done you should be able to access Movie.MovieDirectors

... Does NHibernate support 'Associations'?  (Right click)
0
 
LVL 9

Expert Comment

by:tculler
ID: 24310498
I cannot offer a solution to your problem because I'm not familiar with NHibernate, but I do find it necessary to say that the table you are working with here is not a "reference" or "lookup" table. An example of one of those two would be a list of countries or states. This situation is what we call a bridge table, because you are bridging the two relationships and breaking a "many-to-many" relationship, which is rather impossible to visualize.

Sorry I can't help with your problem, but I felt that that was necessary information nonetheless, maybe to prevent some future confusion of some kind :)
0
 
LVL 3

Accepted Solution

by:
steeza earned 500 total points
ID: 24312679
0
 

Author Closing Comment

by:Phreak3eb
ID: 31578263
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now