我有以下類:Nhibernate在選擇上進行更新?
public class Product
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual Decimal PricePerMonth { get; set; }
public virtual BillingInterval DefaultBillingInterval { get; set; }
public virtual string AdditionalInfo { get; set; }
}
和映射如下所示:
<class name="Product" table="Products">
<id name="Id" column="ProductId">
<generator class="guid.comb"/>
</id>
<property name="Name" column="ProductName" not-null="true" type="String" />
<property name="PricePerMonth" column="PricePerMonth" not-null="true" type="Decimal" />
<property name="DefaultBillingInterval" type="int" not-null="true" />
<property name="AdditionalInfo" type="string" not-null="false" />
</class>
我使用Repository<T>
類下面的方法(Session
是返回當前會話的屬性):
public IEnumerable<T> FindAll(DetachedCriteria criteria)
{
return criteria.GetExecutableCriteria(Session).List<T>();
}
現在,當我執行以下操作(會話是在存儲庫中使用的同一會話):
IEnumerable<ProductDTO> productDTOs = null;
using(ITransaction tx = session.BeginTransaction(IsolationLevel.ReadCommitted))
{
var products = repository.FindAll(new DetachedCriteria.For<Product>().Add(Restrictions.Like("Name", "Some Product%")));
productDTOs = ToDTOs(products);
tx.Commit();
}
// Do stuff with DTO's
commit語句在那裏,因爲我使用了一個服務層,如果沒有錯誤發生,它會自動提交每個事務。我崩潰了我的服務層在這裏更容易可視化..
我ToDTOs
方法簡單地轉換爲DTO:
private IEnumerable<ProductDTO> ToDTO(IEnumerable<Product> products)
{
return products.Select(x => new ProductDTO()
{
Id = x.Id,
Name = x.Name,
PricePerMonth = x.PricePerMonth,
AdditionalInfo = x.AdditionalInfo
}).ToList();
}
我NHibernate的日誌顯示以下的輸出:
2010-01-04 19:13:11,140 [4] DEBUG NHibernate.SQL - SELECT ... From Products ...
2010-01-04 19:13:11,237 [4] DEBUG NHibernate.SQL - UPDATE Products ...
2010-01-04 19:13:11,548 [4] DEBUG NHibernate.SQL - UPDATE Products ...
...
所以選擇該產品發佈會話提交時返回的每個產品的更新聲明,即使產品中沒有任何更改..
有何想法?
這是*真*實體是如何實現的?我不相信你。 – 2010-01-04 18:44:15
這是......的確,我有一個產品所附的訂單清單,但是我已經從類和映射中刪除了這個清單(如圖所示),並且它是相同的結果。 我的困惑是更深刻的事實,這不會發生在我的其他實體.. – 2010-01-04 18:46:44