Many-to-many collection mapping in NHibernate?

Its a classic Many-to-Many where a Podcast can have many subscribers and a subscriber can subscribe to many podcasts. You map it like this.

Its a classic Many-to-Many where a Podcast can have many subscribers and a subscriber can subscribe to many podcasts. You map it like this: If you really want to store the index in the database rather than having NH order the results (I have never had to do this as its better to order by a column and have NH provide your indexes). Then add To the mapping.

Thanks for the more detailed answer; unfortunately I'm still waiting for a spare moment to test this out. – alastairs Nov 11 '09 at 15:12 A couple of questions: 1. Why a set over a bag?2.

Should the class name be the same in both the many-to-many tags? Not one Podcasts, one Users?(I'm not sure which way around it should be, if so). – alastairs Nov 11 '09 at 22:08 There is an error in the above mapping, the second many-to-many shoud be class Users - sorry about that I will update the answer.

– reach4thelasers Nov 12 '09 at 14:47 Re:Set/bag....In the . NET collections hierarchy there isn't a set implementation, so . NET users tend not to use it.

Nbernate comes with an implementation of a Set from Iesi.Collections. The key thing to know about a Set is that each object can only exist once, however in a bag, e.g. IList the same object can exist more than once. For a more detailed description see: blogs.

Com/nhibernate/archive/2008/06/12/… and you'll see that in theoretical terms, a set is actually the correct Collection type to use in this instance. – reach4thelasers Nov 12 '09 at 14:48 In practical terms though, . Net users tend to overuse bags/Lists for everything and, as long as you check the collection beforehand to ensure that the item doesn't actually exist its ok.

With a Set, you can call Add() without checking and the object will be added if it doesn't exist and will be added if it does. – reach4thelasers Nov 12 '09 at 14:48.

You don't need the lookup table. The answer to this question provides the details on how to perform a many to many in Nbernate.

Thanks for your answer. The answer you link to implies I don't need a class for my lookup table (which I don't have, and wasn't expecting to need). However, the example mapping in the other answer (from the OP) still uses the lookup table.Is that right?

– alastairs Nov 2 '09 at 0:22 Correct, you don't need to define the class, but you still need to map the relationship through the table in the database – lomaxx Nov 2 '09 at 2:32.

You can put the many-to-many relation to either class, or even to both. This is up to your domain model. If you map it to both, one of them is inverse.

You can put the many-to-many relation to either class, or even to both. This is up to your domain model. If you map it to both, one of them is inverse.

Class Person { // id ... IList Competencies { get; private set; } // you domain model is responsible to manage bidirectional dependencies. // of course this is not a complete implementation public void AddCompetency(Competency competency) { Competencies. Add(competency); competency.

AddPerson(this); } } class Competency { // id ... IList Persons { get; private set; } } Mapping: Only make it bidirectional if you really need it. By the way: it is much better to write the classes first and create the database design afterwards. The database can be exported from the mapping files.

This is very useful.

I guess I don't really understand why I would, or wouldn't, need bidirectional and what impact it haves if used when not really needed. – Chris Stewart Apr 27 '10 at 17:23 It depends on which property paths you need to "navigate" in your model. Do you regularly want to see the competencies of a person?

Most probably you will, so it will be a good idea to have the competencies available in the person. Do you frequently need to know all the persons which have a certain competency? I guess this is an exception and could also be found by a query.

– Stefan Steinegger Apr 28 '10 at 7:13 Shouldn't the name of the bag in the second class mapping be "Persons" and not "Competencies"? – Mateo Feb 23 at 4:19 @Mateo: yes, I think you are right. I'll fix it.

– Stefan Steinegger Feb 23 at 12:07.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions