Solved

Map Reference Table with NHibernate

Posted on 2009-05-05
4
1,044 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Ever wonder how to "do" object oriented programming (OOP)?
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

726 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