I have the following class:
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; }
}
and the mapping looks like this:
<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>
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
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..
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();
}
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..
Any ideas?