Fluent NHibernate Many-to-Many

后端 未结 4 1351
暗喜
暗喜 2020-12-14 01:56

I am using Fluent NHibernate and having some issues getting a many to many relationship setup with one of my classes. It\'s probably a stupid mistake but I\'ve been stuck fo

相关标签:
4条回答
  • 2020-12-14 02:27

    The fact that it is adding two records to the table looks like you are missing an inverse attribute. Since both the person and the group are being changed, NHibernate is persisting the relation twice (once for each object). The inverse attribute is specifically for avoiding this.

    I'm not sure about how to add it in mapping in code, but the link shows how to do it in XML.

    0 讨论(0)
  • 2020-12-14 02:35

    Are you making sure to add the Person to the Groups.Admin? You have to make both links.

    0 讨论(0)
  • 2020-12-14 02:36

    @Santiago I think you're right.

    The answer might just be that you need to remove one of your ManyToMany declarations, looking more at Fluent it looks like it might be smart enough to just do it for you.

    0 讨论(0)
  • 2020-12-14 02:40

    You have three tables right?

    People, Groups, and GroupAdministrators

    when you add to both sides you get

    People (with an id of p1) Groups (with an id of g1)

    and in GroupAdministrators you have two columns and a table that has

    (p1,g1)

    (p1,g1)

    and your unit test code looks like the following.

    Context hibContext //Built here
    Transaction hibTrans //build and start the transaction.
    
    Person p1 = new Person()
    Groups g1 = new Groups()
    
    p1.getGroupsOwned().add(g1)
    g1.getAdmins().add(p1)
    
    hibTrans.commit();
    hibContext.close();
    

    And then in your test you make a new context, and test to see what's in the context, and you get back the right thing, but your tables are all mucked up?

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