2010-10-07 92 views
0

我有一个问题让我的数据对象检索使用NHibernate保存到数据库中。我没有得到任何例外,所以很难知道去哪里看。有什么建议么?Nhibernate更新不会改变数据库

string name = "somenewname"; 
string repositoryId = "somerepositoryid"; 

ISession nhbSession = GetNhbSession(session); 
nhbSession.Transaction.Begin(); 
try 
{ 
    RepositoryDto existingRepository = nhbSession.Get<RepositoryDto>(repositoryId); 
    if (existingRepository == null || existingRepository.TimeDeleted != null) 
     throw new Exception("Repository does not exist in system or is deleted. Cannot modify."); 

    existingRepository.Name = name; //works fine up to here as expected; is in DB with old name 
    nhbSession.Update(existingRepository); 
    nhbSession.Transaction.Commit(); 
} 
catch (Exception ex) 
{ 
    nhbSession.Transaction.Rollback(); 
    throw new Exception("Error modifying repository: " + ex.Message, ex); 
} 


<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="SomeComponent" 
    namespace="SomeComponent.Repository.DTO" auto-import="true" default-lazy="false"> 
    <class name="RepositoryDto" table="Repository" mutable="false"> 
    <id name="RepositoryId" column="repositoryId" unsaved-value="0"> 
     <generator class="assigned"/> 
    </id> 
    <property name="SystemId" column="systemId" not-null="true" /> 
    <property name="TimeCreated" column="timeCreated" not-null="true" /> 
    <property name="TimeDeleted" column="timeDeleted" not-null="false" /> 
    <property name="Name" column="name" not-null="true" /> 
    </class> 
</hibernate-mapping> 

回答

1

我已经找到了问题。在我的RepositoryDto类映射剩余的不同类映射的复制和粘贴中,我猜想有一个“mutable =”false“'。一个非常讨厌的人找到!

<class name="RepositoryDto" table="Repository" mutable="false"> 

变化

<class name="RepositoryDto" table="Repository" mutable="true"> <!-- or just delete mutable to use default of true --> 

从形式的NHibernate的一个异常“RepositoryDto映射是这么说的,你不能把这个更新对象是不可变”将是不错!

+0

哈哈......真的。 – rebelliard 2010-10-07 11:55:30

1

你缺少Flush

// etc... 
nhbSession.Update(existingRepository); 
nhbSession.Flush(); 
nhbSession.Transaction.Commit(); 
// etc... 

更多:Committing the database transaction

+0

添加刷新不会改变行为。我努力了。它不应该被需要,因为默认情况下它会在提交时自动刷新。非常感谢。 – Lisa 2010-10-07 02:50:25

+0

@Lisa:你可以尝试使用而不是tx.Begin(),就像http://www.codepaste.net/nji87s? – rebelliard 2010-10-07 02:59:21

+0

再次感谢,但似乎确保处理交易仍然无济于事。我甚至试图处理会议,以防万一缺失。 我知道正在对RepositoryDto对象的NHibernate'本地或缓存'版本进行更改,因为如果我在事务中执行了另一个Get调用,它具有新名称。尽管如此,仍然没有坚持DB。 – Lisa 2010-10-07 03:31:23