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?
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.
Example:
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>
You don't need to specify types. They are determined by NHibernate at runtime.
Older post, but maybe this will help someone down the road.
I have a Data Repository C# Class Library (Oracle as the database). The table value was NULL but in my repo the value was defined as Decimal and should have been ?Decimal. That fixed this update issue when running a select.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With