Mapping a many-to-many relationship
A student often attends many classes and hopefully every class is attended by more than one student. This type of relationship is called many-to-many and in a relational database, an intermediate table usually represents it, with at least two columns referencing the keys of the participating entities.
NHibernate supports many-to-many relationships and does so without having to expose the intermediate table to the code.
Getting ready
Complete the Getting ready instructions at the beginning of this chapter.
How to do it…
- Create a new folder named
ManyToMany
in theMappingRecipes
project. - Add a new class
Student
to the folder:public class Student { public virtual Guid Id { get; protected set; } public virtual string Name { get; set; } }
- Create an embedded resource mapping named
Student.hbm.xml
(in the same folder) with the following XML:<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate...