NHibernate的做对选择更新? [英] Nhibernate doing updates on select?

查看:145
本文介绍了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>



我用库< T> 类用下面的方法(会话是返回当前会话属性):

I use a Repository<T> class with the following method (Session is a property that returns the current session):

public IEnumerable<T> FindAll(DetachedCriteria criteria)
{
  return criteria.GetExecutableCriteria(Session).List<T>();
}

现在,当我做了以下(会话中使用相同的会话库):

Now when I do the following (the session is the same session used in the repository):

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语句是存在的,因为我用的,如果没有错误发生自动提交的每一笔交易服务层。我只是崩溃我的服务层这里更容易可视化。

The commit statement is there, because I use a service layer which automatically commits every transaction if no errors occured. I just collapsed my service layer here for easier visualization..

我的 ToDTOs 方法简单地转换成一个DTO:

My ToDTOs method simply converts to a 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的日志显示以下的输出:

My nhibernate log shows the following output:

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 ...
...

因此,通过选择产品它发出的每一个产品的更新语句返回时,会话提交,即使什么都没有的产品被更改。

So by selecting the products it issues an update statement for every product returned when the session commits, even though nothing has been changed in the products..

任何想法?

推荐答案

我只用了这种效果,当我有不从属性比已分配给它的值返回相同的值的实体。然后它是由NH视为肮脏

I only had this effect when I had an entity that does not return the same value from the property than the value that has been assigned to it. Then it is treated as dirty by NH.

例如:

class Foo
{
  private string name;

  public string Name 
  { 
    // does not return null when null had been set
    get { return name ?? "No Name"; }
    set { name = value; }
  }

}






这是我会怎么写映射文件


This is how I would write the mapping file.

<class name="Product" table="Products">
    <id name="Id" column="ProductId">
      <generator class="guid.comb"/>
    </id>
    <property name="Name" column="ProductName" not-null="true" />
    <property name="PricePerMonth" not-null="true" />
    <property name="DefaultBillingInterval" not-null="true" />
    <property name="AdditionalInfo" />
</class>

您不需要指定类型。他们是由NHibernate的在运行时确定。

You don't need to specify types. They are determined by NHibernate at runtime.

这篇关于NHibernate的做对选择更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆