简体   繁体   English

Nhibernate,集合和Compositeid

[英]Nhibernate , collections and compositeid

I have the tables below: 我有下表:

Bucket(
 bucketId smallint (PK)
 name varchar(50)
)

BucketUser(
 UserId varchar(10) (PK) 
 bucketId smallint (PK)
)

The composite key is not the problem thats ok I know how to get around this but I want my bucket class to contanin a IList of BucketUser. 复合键不是可以解决的问题,但是我希望我的存储桶类包含一个BucketUser的IList。 I read the online reference and thought that I had cracked it but havent. 我阅读了在线参考资料,并认为自己已经破解了,但是还没有。 The two mappings are below 这两个映射如下

-- bucket -- -斗-

<class name="Bucket,Impact.Dice.Core" table="Bucket">
  <id name="BucketId" column="BucketId" type="Int16" unsaved-value="0">
    <generator class="native"/>
  </id>

  <property column="BucketName" type="String" name="BucketName"/>

  <bag name="Users" table="BucketUser" inverse="true" generic="true" lazy="true">
    <key>
      <column name="BucketId" sql-type="smallint"/>
      <column name="UserId" sql-type="varchar"/>
    </key>
    <one-to-many class="Bucket,Impact.Dice.Core" not-found="ignore"/>
  </bag>
</class>

-- bucketUser -- -bucketUser-

<class name="BucketUser,Impact.Dice.Core" table="BucketUser">
  <composite-id>
    <key-many-to-one name="BucketUser" class="Bucket,Impact.Dice.Core" column="BucketId"/>
    <key-property name="UserId" column="UserId" type="string"></key-property>
  </composite-id>
</class>

The key is the foreign key to the containing entity, not the primary key. 密钥是包含实体的外键,而不是主键。

You have two options: 您有两种选择:

  • the class represents an independent entity, having an own id. 该类表示一个独立的实体,具有自己的ID。 It could be referenced from other classes, is always in the same table and could be loaded independently. 它可以从其他类中引用,始终在同一表中,并且可以独立加载。
  • or it is a part of another entity with no independent identity. 或者它是另一个没有独立身份的实体的一部分。 If referenced by other classes it is always in a separate table. 如果被其他类引用,则它始终在单独的表中。 It could not (easily) loaded independently from its parent entity. 它不能(轻松)独立于其父实体加载。

Bucketuser is an idependent entity . Bucketuser是一个独立的实体 It has its own mapping definition and you reference it using one-to-many. 它具有自己的映射定义,您可以使用一对多引用。 You get a composite key in your case, but I would avoid this. 在您的情况下,您会得到一个复合键,但是我会避免这种情况。

<!-- reference to BucketUser. There is not table attribute needed. -->
<bag name="Users" inverse="true" generic="true" lazy="true">
  <key>
    <!-- foreign key -->
    <column name="BucketId" sql-type="smallint" />
  </key>
  <one-to-many class="Bucket,Impact.Dice.Core" not-found="ignore"/>
</bag>

<!-- BucketUser mapped as an independent entity -->
<class name="BucketUser" ... >
  <!-- here is the composite id, try to avoid this -->
  <composite-id>
    <key-property name="BucketId">
    <key-property name="UserId">
  </composite-id>
</bag>

Bucketuser is a dependent part of Bucket. Bucketuser是Bucket的从属部分。 The foreign key to the Bucket is the primary key at the same time: 桶的外键同时是主键:

<!-- The table is defined on the fly by the table attribute -->
<bag name="Users" table="BucketUser" inverse="true" generic="true" lazy="true">
  <key>
    <column name="BucketId" sql-type="smallint" />
  </key>
  <!-- use composite-element to define the contents of the table -->
  <composite-element>
    <!-- define the contents of the BucketUser here -->
    <property name="UserId" sql-type="varchar"/>
  </composite-element>
</bag>

It depends on your case which strategy is appropriate. 这取决于您的情况,哪种策略合适。

What error are you getting? 你遇到了什么错误? If you really cut-and-pasted that mapping then the solution may be as simple as replacing the comma in 如果您确实剪切并粘贴了该映射,那么解决方案可能就像替换逗号一样简单。

<one-to-many class="Bucket,Impact.Dice.Core" not-found="ignore"/>

with a period. 有一段时间。

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

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