我有共同的父\兒童情景:NHibernate。不能將兩個孩子家長
public class Order : AdvancedBaseOrder
{
ICollection<ProducerRelation> producers = new List<ProducerRelation>();
public virtual ICollection<ProducerRelation> Producers
{
get { return producers; }
set { producers = value; }
}
}
public class ProducerRelation : BaseProducerRelation
{
public virtual Order Order { get; set; }
public virtual int Number { get; set; }
}
映射:
Order.hbm.xml
....
<set name="Producers" cascade="all-delete-orphan" inverse="true">
<key column="order_id"/>
<one-to-many class="ProducerRelation,Avtobus66.Core"/>
</set>
....
ProducerRelation.hbm.xml
....
<many-to-one name="Order" class="Order, Avtobus66.Core" column="order_id" cascade="none"/>
<property name="Number" >
<column name="number"/>
</property>
....
當我運行這段代碼:
Order order = (Order)session.Get(typeof(Order), 23);
var a = new ProducerRelation();
a.Number = 6;
a.Order = order;
var b = new ProducerRelation();
b.Number = 7;
b.Order = order;
order.Producers.Add(a);
order.Producers.Add(b);
session.Merge(order);
session.Flush();
只有一個我的孩子被添加。我知道,nhibernate正在「看」孩子收藏的變化,但我做錯了什麼?爲什麼nhib不能添加兩個孩子?
SQL:
NHibernate: SELECT ... WHERE this_.id = ?p0;?p0 = 23
NHibernate: INSERT INTO gb_avtobus66.ekbprint_producer_relation (producer_price, producer_price_clean, order_id, ekbprint_product_id, client_id, number) VALUES (?p0, ?p1, ?p2, ?p3, ?p4, ?p5);?p0 = 0, ?p1 = 0, ?p2 = 23, ?p3 = NULL, ?p4 = NULL, ?p5 = 6
NHibernate: SELECT LAST_INSERT_ID()
如果您執行Get操作而不是Load操作,它會起作用嗎? – gt124 2011-03-23 15:16:45
對不起。我使用知識庫模式,這是我在寫作上的一個小錯誤。當然得到 – 2011-03-23 15:38:23
生成的SQL說什麼?哪個子對象被插入? – 2011-03-23 16:07:21