views:

41

answers:

1

Hi All, During usage of NHibernate ...

One strange problem that I have bumped into was multiple updates after Session.Flush() command. No data actually changed ... Just wanted to perform Select that all, in my case there was 1000 updates – one per returned row!!!

Such behavior happens only if I’m using properties with custom PropertyAccessor as access type. The reason that I have created custom property was that I have uint and ushort types in my objects so I wanted to cast them from (long or Int32 correspondingly )on the Setter.

The question is how can I disable those updates or why my custom Setter causing such behavior?

void Set(object target, object value)
        {
            If(Value.GetType() == typeof(long))
            {
            Target.GetType().GetProperty(_propertyName).SetValue((uint)value);
            }
        }

Thanks in advance,

Update:------ How can i disable this dirty check before every Flush?

+2  A: 

It seems like your PropertyAccessor is messing up the NHibernate Session dirty tracking. This post shows how you can test if an entity is dirty. You should add this code and debug into the NHibernate source to see exactly why your entities are considered dirty. I suspect that the types won't match.

Update:

Another approach would be to keep your Entity definitions as unsigned but change the db mappings to use signed types in the database. This is how using Fluent NHIbernate and SQL server:

public class TestEntity : Entity
{
    public virtual uint Unsigned { get; set; }
    public virtual ushort UnsignedShort { get; set; }
}

public class TestEntityMap : ClassMap<TestEntity>
{
    public TestEntityMap()
    {
        Map( x => x.Unsigned ).CustomSqlType( "bigint" );
        Map( x => x.UnsignedShort ).CustomSqlType( "int" );
    }
}
David Lynch
10x for a good point regarding types ... Probably i will use session.Evict(obj) option.
user3072
If you can you should consider using "matching" types between your entities and db mappings. Or remap at the mapping layer using CustomSqlType (see http://stackoverflow.com/questions/644496/fluent-nhibernate-schema-generation for an example)
David Lynch
I have performed some debugging of NHibernate source code, and the reason was type mismatch. Like you assumed dirty flag ... the comparison is performed like this return x.Equal(y); in my case x is long and y is uint, so the result is always false. As for your advice, I prefer not to use special NHibernate types in my business logic objects ... I really can't understand why there is no build in support for uint, ushort etc. types ????
user3072