Nhibernate many to relationship example

Fluent NHibernate: One-To-Many Mapping - SIRHOE CODE

NHibernateComplex relationships. time to read 3 min We then map the Person, using standard many-to-many mapping for the addresses. The user of course means adding columns to the "junction" table used to store the many-to-many relationship, and being able to populate them. I don't have to create a UserRole class. NHibernate ensures the associative table . Models public class User { public User() { Roles = new.

The association class is the "Ticket" which breaks this many-to-many association and binds a particular instance of a passenger to a particular instance of a train. Also a train now has many tickets one-to-many and a passenger could have many tickets to many trains one-to-many.

nhibernate many to relationship example

Every developer knows this scenario because it is the most commonly used scenario. Our topic of discussion is to reveal how to use NHibernate to map a many-to-many entity association.

  • nHibernate many-to-many collections. (OR mapping is not one table one class)
  • Ayende @ Rahien

Our sample scenario is a console application to persist the many-to-many association in an online train ticket reservation. We will model the scenario slightly differently because in normal practice, it is not necessary the passenger himself must book his ticket in advance and also a railway ticket which is booked in advance could have names for more than one passenger if listed so in the booking form family bookings in one ticket with entire family names.

So in our online ticket reservation, a registered user is allowed to book a ticket for many trains. A train will have tickets booked by many registered users.

NHibernate Relationships

Hence the association between a registered user wishing to book the ticket in many trains and a train getting booked by many registered users is a many-to-many entity association.

The user submits a form that contains the list of passengers travelling and a particular train he needs to book the ticket for.

nhibernate many to relationship example

The association class is the online Ticket which will associate a particular user booking the ticket and the particular train for which the ticket is booked with the list of passengers for that ticket as enlisted by the registered user in the booking form.

The tickets are issued by the class TicketCounter which is the reservation system issuing the ticket. We will ignore the date and time of the train and differentiate a train only by name as our interest is limited to many-to-many entity association and using NHibernate for persisting it. The advantage of mapping the many-to-many entity class association between the registered user and the trains is, the user instance will have the collection of tickets booked by that particular registered user and the train will have the collection of tickets booked for that particular train, and the ticket will have a list of passengers and other information pertaining to a passenger all persisted by NHibernate.

Now refer to Figure 2. The many-to-many entity association between Train and User Registered User of Reservation system is split to two one-to-many associations by the association class Ticket and this is shown by the orange arrow.

nhibernate many to relationship example

Also note that as expected, now neither Train nor User is referencing each other though they have a many-to-many association between them in the domain. Figure 2 The C code snippet below shows the Ticket.

nhibernate many to relationship example

Note that the ticket class uses a composite key. All along we have advocated the policy of using surrogate keys with generators and never composite keys. But a many-to-many association is an exception to this practice because a composite key fits in correctly here.

This composite key class has been defined by us separately. In the downloadable project, you will see this CompositeKey class in the Ticket.

nHibernate many-to-many collections. (OR mapping is not one table one class) | Peter van Ooijen

In the constructor of Ticket, observe that this composite key is set to the primary key values of User buying the Ticket and Train for which the ticket is bought. Therefore the only way NHibernate can update the database to reflect the changes is to delete all of the children of that parent, and then re-insert all of the children one by one.

This is a significant drawback in terms or performance, and therefore NHibernate came up with a special type of collection called idBag. Note that this column is not associated with a property on the class! This is how it looks like: Note that for components, this happens automatically, but often you want this to be the case even if the children are entities.

Fluent NHibernate: One-To-Many Mapping

Recall the definition of an Entity: Of course that you can call ISession. SaveOrUpdate for example NHibernate works as follows: The diagram above is intentionally left incomplete without the datatype and other contraints so that we can focus on the relationship.

nhibernate many to relationship example

In order to represent such relationship in C FluentNHibernate code, we first have to create the entities classes, Post and Category. Even if you are not using Fluent or NHibernate you would still create such classes to store the data you fetch from the database. This entity classes are required no matter which ORM you use or even if you do not use one. After that, we will create the mapping classes required by FluentNHibernate to represent the relationship in the database.

NHibernate - Relationships

There will be two mapping classes, one for earch entity class. ClassMap it in the Contructor As you can see from the PostMap class above, all the mappings must be in the Contrsuctor.

Table Post Primary key, also known as Id, is mapped to the Id method.

Best Practices for Many-To-One and One-To-Many Association Mappings