NHibernate双向多对多映射列表/包 [英] NHibernate Bidirectional Many-to-Many Mapping List / Bag

查看:153
本文介绍了NHibernate双向多对多映射列表/包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有两个类别,一个是双向多对多映射,其中一个是重要的顺序,另一个则不重要。 :程序和学生。

一个程序有很多学生,顺序是重要的。

程序A


  1. John

  2. Sally

  3. Seth

程序B


  1. Alex

  2. Seth

  3. Amy

  4. 约翰





John
*程式A
*程式B

>

Sally


  • 计划A



Seth


  • 程式A

  • 程式B li>


Alex


  • 计划B



  • Amy




    • 计划B



    所以,它附上ars,我会在程序和学生之间建立一个双向的多对多关系,我可以做这样的事情......

      var john = GetJohn(); 
    var programCount = john.Programs.Count; // 2

    var programB = GetProgramB();
    var studentCount = programB.Students.Count; // 4
    var secondStudent = programB.Students [1]; // Seth

    我无法弄清楚如何让映射工作。



    程序制图



    程序制图

     <?xml version =1.0encoding =utf-8?> 
    < hibernate-mapping xmlns =urn:nhibernate-mapping-2.2>
    < class name =Programtable =Programslazy =false>
    < id name =IDcolumn =IDtype =Int32unsaved-value =0>
    < generator class =identity/>
    < / id>
    < property name =Titlecolumn =Titletype =Stringlength =200/>
    < list name =Studentstable =ProgramAssignmentslazy =truecascade =all>
    < key column =ProgramID/>
    < index column =SequenceIndextype =Int32/>
    <多对多列=StudentIDclass =Student/>
    < / list>
    < / class>
    < / hibernate-mapping>

    学生映射

     < hibernate-mapping xmlns =urn:nhibernate-mapping-2.2> 
    < class name =Studenttable =Studentslazy =false>
    < id name =IDcolumn =IDtype =Int32unsaved-value =0>
    < generator class =identity/>
    < / id>
    < property name =Titlecolumn =Titletype =Stringlength =200/>
    < bag name =Programstable =ProgramAssignmentslazy =true>
    < key column =StudentID/>
    < / bag>
    < / class>
    < / hibernate-mapping>

    我的关联表非常简单...

      ProgramID int 
    StudentID int
    SequenceIndex int

    PK是ProgramID + StudentID。



    我真的希望从程序端管理这个关联,因为每个程序中的学生顺序是重要。但是,我真的希望能够通过mystudent.Programs访问特定学生的课程。我已经尝试了许多变化的映射,包括在程序列表中设置inverse = true,尝试不同的级联选项等。似乎没有什么可行的。



    帮助!感谢!

    解决方案

    这个问题可能隐藏在< list> mapping支持一个实例的多重赋值(所以可能会有学生 Seth 分配两次,使用不同的 SequenceIndex - 违反PK错误)



    但我想给你一个提示。尝试改变方法并引入中间对象。查看NHibernate文档:第24章最佳实践。快速总结:


    不要使用外来的关联映射。



    真正的多对多关联的好用例很少见。
    大部分时间您需要存储在链接表中的附加信息。
    在这种情况下,使用两个一对多关联到
    中间链接类更好。事实上,我们认为大多数协会
    是一对多和多对一的,您在使用任何
    其他协会风格时应该小心,并问自己是否真的需要。


    我想说,这是(可能)的情况,因为您确实需要订单。 ..


    I am struggling with a bi-directional many-to-many mapping where the order is important on one side, but not the other.

    I have two classes: Program and Student.

    A program has many students and the sequential order is important.

    Program A

    1. John
    2. Sally
    3. Seth

    Program B

    1. Alex
    2. Seth
    3. Amy
    4. John

    A student has many programs, but the order is not important here.

    John * Program A * Program B

    Sally

    • Program A

    Seth

    • Program A
    • Program B

    Alex

    • Program B

    Amy

    • Program B

    So, it appears that I would have a bidirectional many-to-many association between programs and students where I could do things like this...

    var john = GetJohn();
    var programCount = john.Programs.Count; // 2
    
    var programB = GetProgramB();
    var studentCount = programB.Students.Count; // 4
    var secondStudent = programB.Students[1]; // Seth
    

    I cannot figure out how to get the mapping to work. I keep getting PK violation errors.

    I have tried the following...

    PROGRAM MAPPING

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
        <class name="Program" table="Programs" lazy="false">
            <id name="ID" column="ID" type="Int32" unsaved-value="0">
                <generator class="identity" />
            </id>
            <property name="Title" column="Title" type="String" length="200" />
            <list name="Students" table="ProgramAssignments" lazy="true" cascade="all">
                <key column="ProgramID" />
                <index column="SequenceIndex" type="Int32" />
                <many-to-many column="StudentID" class="Student" />
            </list>
        </class>
    </hibernate-mapping>
    

    STUDENT MAPPING

    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
        <class name="Student" table="Students" lazy="false">
            <id name="ID" column="ID" type="Int32" unsaved-value="0">
                <generator class="identity" />
            </id>
            <property name="Title" column="Title" type="String" length="200" />
            <bag name="Programs" table="ProgramAssignments" lazy="true">
                <key column="StudentID" />
                <many-to-many column="ProgramID" class="Program" />
            </bag>
        </class>
    </hibernate-mapping>
    

    My association table is very simple...

    ProgramID int
    StudentID int
    SequenceIndex int
    

    The PK is the ProgramID + StudentID.

    I really want the association to be managed from the program side because the order of the students in each program is important. But, I really would like to be able to access the programs for a specific student via mystudent.Programs. I have tried many variations of the mapping, including setting inverse=true on the program list, trying different cascade options, etc. Nothing seems to work.

    Help! Thanks!

    解决方案

    The issue could be hidden in the fact, that the <list> mapping supports multiple assignment of one instance (so there could be student Seth assigned twice, with different SequenceIndex - violating the PK error)

    But I would like to give you a hint. Try to change the approach and introduce the intermediate object. See the NHibernate documentation: Chapter 24. Best Practices. Quick summary:

    Don't use exotic association mappings.

    Good usecases for a real many-to-many associations are rare. Most of the time you need additional information stored in the "link table". In this case, it is much better to use two one-to-many associations to an intermediate link class. In fact, we think that most associations are one-to-many and many-to-one, you should be careful when using any other association style and ask yourself if it is really neccessary.

    And I would say, that this is (could be) the case, because you really need the Order ...

    这篇关于NHibernate双向多对多映射列表/包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆