many-to-many mapping in NHibernate

后端 未结 1 1303
北海茫月
北海茫月 2020-12-29 11:26

I\'m looking to create a many to many relationship using NHibernate. I\'m not sure how to map these in the XML files. I have not created the classes yet, but they will jus

1条回答
  •  隐瞒了意图╮
    2020-12-29 12:15

    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.

    0 讨论(0)
提交回复
热议问题