简体   繁体   English

NHibernate双向多对多映射列表/包

[英]NHibernate Bidirectional Many-to-Many Mapping List / Bag

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 计划A.

  1. John 约翰
  2. Sally 出击
  3. Seth 赛斯

Program B 方案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 约翰*计划A *计划B.

Sally 出击

  • Program A 计划A.

Seth 赛斯

  • Program A 计划A.
  • Program B 方案B.

Alex 亚历克斯

  • Program B 方案B.

Amy 艾米

  • Program B 方案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. 我一直得到PK违规错误。

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. PK是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. 但是,我真的希望能够通过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. 我已经尝试了许多映射的变体,包括在程序列表上设置inverse = true,尝试不同的级联选项等等。似乎没有什么工作。

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) 问题可能隐藏在这样的事实中: <list>映射支持一个实例的多个赋值(因此可能会有学生Seth分配两次,具有不同的SequenceIndex - 违反PK错误)

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 . 请参阅NHibernate文档: 第24章最佳实践 Quick summary: 快速摘要:

Don't use exotic association mappings. 不要使用异国情调的关联映射。

Good usecases for a real many-to-many associations are rare. 真正many-to-many关联的良好用途很少见。 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 ... 我会说,这是(可能)的情况,因为你真的需要订单 ......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM