Solved

Map Reference Table with NHibernate

Posted on 2009-05-05
4
1,029 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

733 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