【发布时间】:2013-10-14 06:51:10
【问题描述】:
我在 NHibernate 2 应用程序中有一个 Product 实体,它与位置具有多对多关系。 Location 实体没有任何导航属性或返回产品的映射。它的映射如下:
<bag name="Locations" table="ProductLocation" cascade="none">
<key column="ProductId" />
<many-to-many column="LocationId" class="Location"/>
</bag>
该产品还有一个复合元素,一个具有通过 ProductComponent 类映射的浓度的组件。该类没有导航属性或映射回产品。
<bag name="ProductComponents" table="ProductComponent" access="nosetter.camelcase">
<key column="ProductId" />
<composite-element class="ProductComponent">
<property name="Concentration"/>
<many-to-one name="Component" column="ComponentId" access="nosetter.camelcase"/>
</composite-element>
</bag>
当一次只插入一个产品时,这一切都很好。但是,当批量插入多个产品时它会失败。
虽然产品本身可以很好地插入,但每个产品都有自己的唯一 ID,多对多 (Locations) 和复合元素 (ProductComponent) 中的元素不能很好地插入。这是因为 NHibernate 多次执行对具有相同 ProductId 的 ProductLocation 表的插入。
这会导致链接表中出现重复记录。如何预防?
【问题讨论】:
标签: tsql nhibernate