Nhibernate、收藏和合成
我有以下表格:Nhibernate、收藏和合成,nhibernate,collections,composite,Nhibernate,Collections,Composite,我有以下表格: Bucket( bucketId smallint (PK) name varchar(50) ) BucketUser( UserId varchar(10) (PK) bucketId smallint (PK) ) 复合键不是问题,我知道如何解决这个问题,但我希望我的bucket类包含BucketUser的IList。我读了网上的参考资料,认为我已经破解了,但还没有。下面是两个映射 --水桶-- --巴克图瑟-- 您遇到了什么错误?如果您确实剪切并粘贴了该
Bucket(
bucketId smallint (PK)
name varchar(50)
)
BucketUser(
UserId varchar(10) (PK)
bucketId smallint (PK)
)
复合键不是问题,我知道如何解决这个问题,但我希望我的bucket类包含BucketUser的IList。我读了网上的参考资料,认为我已经破解了,但还没有。下面是两个映射
--水桶--
--巴克图瑟--
您遇到了什么错误?如果您确实剪切并粘贴了该映射,那么解决方案可能很简单,只需在中替换逗号即可
<one-to-many class="Bucket,Impact.Dice.Core" not-found="ignore"/>
带句点。键是包含实体的外键,而不是主键 您有两个选择:
- 该类表示具有自己id的独立实体。它可以从其他类引用,始终位于同一个表中,并且可以独立加载
- 或者它是另一个实体的一部分,没有独立的身份。如果被其他类引用,它总是在一个单独的表中。它无法(轻松)独立于其父实体加载
<!-- 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的从属部分。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>
这取决于您的情况,哪种策略是合适的。您好,谢谢您的回复。is“”外键(FK3294777369BF2:Bucket[BucketId,UserId])上的主键必须与引用的主键(Bucket[BucketId])具有相同的列数“}所以基本上bucketid是bucket表上的主键,它是bucketUser表上foreigh键的一部分。我重写了我的答案,使它更清晰。bucketuser的映射是不可见的,您需要将其设置为“code”。我发现,当我有inverse=true时,复合元素实际上并没有被激活saved@JoshBerke:可能是。NH的反向意思是:不要存储它,它已经从另一条路径存储了。