[英]Nhibernate , collections and compositeid
我有下表:
Bucket(
bucketId smallint (PK)
name varchar(50)
)
BucketUser(
UserId varchar(10) (PK)
bucketId smallint (PK)
)
復合鍵不是可以解決的問題,但是我希望我的存儲桶類包含一個BucketUser的IList。 我閱讀了在線參考資料,並認為自己已經破解了,但是還沒有。 這兩個映射如下
-斗-
<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-
<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>
密鑰是包含實體的外鍵,而不是主鍵。
您有兩種選擇:
Bucketuser是一個獨立的實體 。 它具有自己的映射定義,您可以使用一對多引用。 在您的情況下,您會得到一個復合鍵,但是我會避免這種情況。
<!-- 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是Bucket的從屬部分。 桶的外鍵同時是主鍵:
<!-- 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>
這取決於您的情況,哪種策略合適。
你遇到了什么錯誤? 如果您確實剪切並粘貼了該映射,那么解決方案可能就像替換逗號一樣簡單。
<one-to-many class="Bucket,Impact.Dice.Core" not-found="ignore"/>
有一段時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.