NHibernate中使用generator爲assigned的問題

Hibernate version:
1.0.2.0
Mapping documents:
Parent.hbm.xml 
None.gif<?xml version="1.0" encoding="utf-8" ?>
None.gif
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
None.gif  
<class name="HYLQ.Core.Domain.Parent, HYLQ.Core" table="Parent">
None.gif    
<id name="Id" column="Id" unsaved-value="0">
None.gif      
<generator class="assigned" />
None.gif    
</id>
None.gif    
<property name="Title" type="String" length="200" />
None.gif
None.gif    
<bag name="Childs" lazy="true" table="Child" inverse="true" cascade="all" 
None.gif            access
="NHibernate.Generics.GenericAccessor, NHibernate.Generics" >
None.gif      
<key column="pid" />
None.gif      
<one-to-many class="HYLQ.Core.Domain.Child, HYLQ.Core" />
None.gif    
</bag>
None.gif    
None.gif  
</class>
None.gif
</hibernate-mapping>


Child.hbm.xml
None.gif<?xml version="1.0" encoding="utf-8" ?>
None.gif
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
None.gif  
<class name="HYLQ.Core.Domain.Child, HYLQ.Core" table="Child">
None.gif    
<id name="Id" column="Id" unsaved-value="0">
None.gif      
<generator class="assigned" />
None.gif    
</id>
None.gif    
<property name="Memo" type="String" length="200" />
None.gif    
<many-to-one name="Parent" column="pid" class="HYLQ.Core.Domain.Parent, HYLQ.Core"
None.gif            access
="NHibernate.Generics.GenericAccessor, NHibernate.Generics" />
None.gif  
</class>
None.gif
</hibernate-mapping>

測試代碼:
None.gifusing (ISession session = TestCategory.Factory.OpenSession())
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif{
InBlock.gif                int id = 1;
InBlock.gif
InBlock.gif                Parent parent = new Parent();
InBlock.gif
InBlock.gif                parent.Id = id;
InBlock.gif                parent.Title = "tetetet";
InBlock.gif
InBlock.gif                Child child = new Child();
InBlock.gif                child.Id = 1;
InBlock.gif                child.Memo = "222";
InBlock.gif                child.parent = parent;
InBlock.gif
InBlock.gif                parent.Childs.Add(child);
InBlock.gif
InBlock.gif                ITransaction trans = session.BeginTransaction();
InBlock.gif
InBlock.gif                try
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
InBlock.gif                    session.Save(parent);
InBlock.gif                    trans.Commit();
ExpandedSubBlockEnd.gif                }
InBlock.gif                catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
InBlock.gif                    trans.Rollback();
InBlock.gif                    throw;
ExpandedSubBlockEnd.gif                }
ExpandedBlockEnd.gif            } 

當我設置了
chilid.Id =1的時候,則出現了
Test method TestProject1.TestCategory.AddParentChild threw exception: NHibernate.HibernateException: SQL insert, update or delete failed (expected affected row count: 1, actual affected row count: 0). Possible causes: the row was modified or deleted by another user, or a trigger is reporting misleading row count..
跟蹤執行的SQL語句後,發現並不是Insert Child,而是Update Child。
當我將
chilid.Id =1註釋後,就正常,不過chilid.Id他用 unsaved-value="0"代替。
執行的SQL語句都是正常的。

而我在網上看到一些One2Many的例子中其中表的主鍵都是自增長的後非assigned的。
所以這個問題很是困惑,最後,自己寫了一個自定義的Generator來生產ID,
後來就沒有出現上述的情況了。

你們碰到過這種情況嗎?



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章