Hi People,

I have a wierd issue with extra updates being issues to the Database
in an application I'm mucking around with.

I have a following entity structure:

    public abstract class DomainEntity<TId>
    {
        public virtual TId Id { get; protected set; }
        public virtual int Version { get; set; }

        public virtual bool IsTransient
        {
            get { return Equals(Id, default(TId)); }
        }

        public override bool Equals(object obj)
        {
            return Equals(obj as DomainEntity<TId>);
        }

        private Type GetUnproxiedType()
        {
            return GetType();
        }

        public virtual bool Equals(DomainEntity<TId> other)
        {
            if (other == null) { return false; }
            if (ReferenceEquals(this, other)) { return true; }

            if (!IsTransient && !other.IsTransient && Equals(Id,
other.Id))
            {
                var otherType = other.GetUnproxiedType();
                var thisType = GetUnproxiedType();
                return thisType.IsAssignableFrom(otherType) ||
otherType.IsAssignableFrom(thisType);
            }

            return false;
        }

        public override int GetHashCode()
        {
            if (Equals(Id, default(TId))) { return
base.GetHashCode(); }

            return Id.GetHashCode();
        }
    }

    public abstract class AuditedDomainEntity<TId> : DomainEntity<TId>
    {
        public virtual DateTime UpdatedAt { get; set; }
        public virtual string UpdatedBy { get; set; }
        public virtual DateTime CreatedAt { get; set; }
        public virtual string CreatedBy { get; set; }
    }

    public class Item : AuditedDomainEntity<Guid>
    {
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
        public virtual Account Account { get;  protected internal
set; }
        public virtual Item Parent { get; private set; }
        public virtual IList<Item> Children { get; private set; }
    }

Fluent Mapping, each of these inherit the ame as the above classes.

        public DomainEntityMapping()
        {
            // Set id.
            Id(x => x.Id);

            // Set concurrency.
            OptimisticLock.Version();
            Version(entity => entity.Version);
        }

        public AuditedDomainEntityMapping()
        {
            // Set audit.
            Map(x => x.CreatedAt).Column("created_at");
            Map(x => x.CreatedBy).Length(50).Column("created_by");
            Map(x => x.UpdatedAt).Column("updated_at");
            Map(x => x.UpdatedBy).Length(50).Column("updated_by");
        }

        public ItemMapping()
        {
            // Relationships:
            References(x => x.Account).Not.Nullable();
            References(x => x.Parent);
            HasMany(x =>
x.Children).KeyColumn("parent_id").Inverse().Cascade.All();

            // Fields:
            Map(x => x.Name).Length(50);
            Map(x => x.Description).Length(500);
        }

I then have an audit event handler:

    public class AuditEventListener : DefaultSaveOrUpdateEventListener
    {
        public override void OnSaveOrUpdate(SaveOrUpdateEvent @event)
        {
            var entity = @event.Entity as AuditedDomainEntity<Guid>;

            if (entity != null)
            {
                entity.UpdatedAt = DateTime.Now;
                entity.UpdatedBy = GetAuditName();

                if (entity.IsTransient)
                {
                    entity.CreatedAt = DateTime.Now;
                    entity.CreatedBy = GetAuditName();
                }
            }

            base.OnSaveOrUpdate(@event);
        }

        private static string GetAuditName()
        {
            return
string.IsNullOrEmpty(Thread.CurrentPrincipal.Identity.Name) ?
"Unknown" : Thread.CurrentPrincipal.Identity.Name;
        }
    }

When I do a simple select like this:

Session.Get<T>(id)

I'm seeing this:

select item0_.id as id2_0_, children1_.id as id2_1_, item0_.version as
version2_0_, item0_.created_at as created3_2_0_, item0_.created_by as
created4_2_0_, item0_.updated_at as updated5_2_0_, item0_.updated_by
as updated6_2_0_, item0_.name as name2_0_, item0_.description as
descript8_2_0_, item0_.Account_id as Account9_2_0_, item0_.Parent_id
as Parent10_2_0_, children1_.version as version2_1_,
children1_.created_at as created3_2_1_, children1_.created_by as
created4_2_1_, chi

SELECT children0_.parent_id as parent10_1_, children0_.id as id1_,
children0_.id as id2_0_, children0_.version as version2_0_,
children0_.created_at as created3_2_0_, children0_.created_by as
created4_2_0_, children0_.updated_at as updated5_2_0_,
children0_.updated_by as updated6_2_0_, children0_.name as name2_0_,
children0_.description as descript8_2_0_, children0_.Account_id as
Account9_2_0_, children0_.Parent_id as Parent10_2_0_ FROM items
children0_ WHERE children0_.parent_id=((E'83a3e720-

UPDATE items SET version = ((26)::int4), updated_at = ((E'2010-12-10
16:33:35.000000')::timestamp) WHERE id =
((E'83a3e720-2d80-46da-9038-9e48007d58f8')::uuid) AND version =
((25)::int4)

UPDATE items SET version = ((52)::int4), updated_at = ((E'2010-12-10
16:33:35.000000')::timestamp) WHERE id =
((E'a2f2e8b6-6d4b-460c-84d6-9e47010d4945')::uuid) AND version =
((51)::int4)


If I disable the auditing event listener the updates go away, and if I
remove the cascade for children it also goes away only appears when
both are enable, I dont really understand whats going on here, can
someone give me a idea of what i have done wrong?

Thanks,
Maxus



-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to