zoukankan      html  css  js  c++  java
  • NHibernate中使用generator为assigned的问题

    Hibernate version:
    1.0.2.0
    Mapping documents:
    Parent.hbm.xml 
    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
      
    <class name="HYLQ.Core.Domain.Parent, HYLQ.Core" table="Parent">
        
    <id name="Id" column="Id" unsaved-value="0">
          
    <generator class="assigned" />
        
    </id>
        
    <property name="Title" type="String" length="200" />

        
    <bag name="Childs" lazy="true" table="Child" inverse="true" cascade="all" 
                access
    ="NHibernate.Generics.GenericAccessor, NHibernate.Generics" >
          
    <key column="pid" />
          
    <one-to-many class="HYLQ.Core.Domain.Child, HYLQ.Core" />
        
    </bag>
        
      
    </class>
    </hibernate-mapping>


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

    测试代码:
    using (ISession session = TestCategory.Factory.OpenSession())
                
    {
                    int id = 1;

                    Parent parent = new Parent();

                    parent.Id = id;
                    parent.Title = "tetetet";

                    Child child = new Child();
                    
    child.Id = 1;
                    child.Memo = "222";
                    child.parent = parent;

                    parent.Childs.Add(child);

                    ITransaction trans = session.BeginTransaction();

                    try
                    {
                        session.Save(parent);
                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        throw;
                    }
                } 

    当我设置了
    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,
    后来就没有出现上述的情况了。

    你们碰到过这种情况吗?



  • 相关阅读:
    51 Nod 1068 Bash游戏v3
    51 Nod Bash 游戏v2
    51 Nod 1073 约瑟夫环
    UVA 12063 Zeros and ones 一道需要好好体会的好题
    51 Nod 1161 Partial sums
    2018中国大学生程序设计竞赛
    UVA 11971 Polygon
    UVA 10900 So do you want to be a 2^n-aire?
    UVA 11346 Possibility
    python with as 的用法
  • 原文地址:https://www.cnblogs.com/maplye/p/442333.html
Copyright © 2011-2022 走看看