我正在尝试保存一对多映射,nHibernate 会抛出此错误,因为它为ordPsiPt
表的两个条目分配了相同的 ID。
我环顾四周,使用Session.merge()
也不起作用。
这是类的映射:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="FrancosPoS" namespace="FrancosPoS.DBMapping" xmlns="urn:nhibernate-mapping-2.2">
<class name="ordPsiPt" table="ord_psi_pt" lazy="true" >
<id name="idOrdPastaIT">
<generator class="identity" />
</id>
<many-to-one lazy="false" name="ordPsi">
<column name="idOrdPastaI" sql-type="int(11)" not-null="true" />
</many-to-one>
<many-to-one lazy="false" name="pastaTopping">
<column name="idPastaT" sql-type="int(11)" not-null="true" />
</many-to-one>
<property name="amount">
<column name="Amount" sql-type="varchar(50)" not-null="true" />
</property>
</class>
</hibernate-mapping>
order_psi:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="FrancosPoS" namespace="FrancosPoS.DBMapping" xmlns="urn:nhibernate-mapping-2.2">
<class name="ordPsi" table="ord_psi" lazy="true" >
<id name="idOrdPastaI">
<generator class="identity" />
</id>
<many-to-one lazy="false" name="order">
<column name="idOrder" sql-type="int(11)" not-null="true" />
</many-to-one>
<many-to-one lazy="false" name="pastaIndividual" class="pastaIndividual">
<column name="idPastaI" sql-type="int(11)" not-null="false" />
</many-to-one>
<property name="obs">
<column name="obs" sql-type="varchar(50)" not-null="false" />
</property>
<property name="price">
<column name="price" sql-type="decimal(8,4)" not-null="true" />
</property>
<bag name="ordPsiPt" table="ordPsiPt" cascade="save-update" inverse="true">
<key column="idOrdPastaIT"/>
<one-to-many class="ordPsiPt"/>
</bag>
</class>
</hibernate-mapping>
命令:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="FrancosPoS" namespace="FrancosPoS.DBMapping" xmlns="urn:nhibernate-mapping-2.2">
<class name="order" table="`order`" lazy="true" >
<id name="idOrder">
<generator class="identity" />
</id>
<property name="price">
<column name="price" sql-type="decimal(8,4)" not-null="true" />
</property>
<property name="cash">
<column name="cash" sql-type="tinyint(1)" not-null="false" />
</property>
<property name="credit">
<column name="credit" sql-type="tinyint(1)" not-null="false" />
</property>
<property name="obs">
<column name="obs" sql-type="varchar(350)" not-null="false" />
</property>
<bag name="orderPsi" table="ordPsi" cascade="all" inverse="true">
<key column="idOrdPastaI"/>
<one-to-many class="ordPsi"/>
</bag>
</class>
</hibernate-mapping>
该包采用IList
cs 映射。
当我SaveOrUpdate()
使用 order 对象调用时,在一个 orderPsi 中有两个 orderPsiPt(仅作为示例),我收到了关于标识符的错误。
我尝试conn.Session.Merge(order.orderPsi[0].ordPsiPt[1]);
仅用于此示例,但也没有成功。
如果有帮助,这里是两个 ordPsiPt 的手表:http: //db.tt/2wNWldyd
我也试过看看这个:
http://ayende.com/blog/4282/nhibernate-cross-session-operations