Skip to content

ComposedId Entity with Lazy Property is not proxified #1439

Open
@MarkPerryBV

Description

@MarkPerryBV

An entity having a composite id directly mapped to the entity properties (not mapped with a component), and having lazy properties, is not proxified by the session. The lazy property is not loaded, but the instance yielded by the session is not a proxy and so cannot lazy-load the property.

Initial report follows.


When you query out a ComposedId entity (one with a composite primary key) and that entity also features a property marked as Lazy then NH will try and update that lazy property to "null" at the end of the UOW.

Entity

public class CompositeEntity
	{
		public virtual int Id { get; set; }
		public virtual string Name { get; set; }
		public virtual string LazyProperty { get; set; }

		public override int GetHashCode()
		{
			return (Id + "|" + Name).GetHashCode();
		}
		public override bool Equals(object obj)
		{
			if (obj == null)
				return false;
			return obj.GetHashCode() == GetHashCode();
		}
	}

Class Mapping:

public class CompositeEntityMap : ClassMapping<CompositeEntity>{
    public CompositeEntityMap(){
        ComposedId(c =>{
             c.Property(t => t.Id);
             c.Property(t => t.Name);
         });
        Property(x => x.LazyProperty, x => x.Lazy(true));
    }
}

Test

using (ISession session = OpenSession())
using (var tran = session.BeginTransaction()){
  var result = (from e in session.Query<CompositeEntity>()
			 where e.Name == "Bob"
			 select e).ToList();
	session.Flush();
	tran.Commit();
}

I don't know how to Assert that an update has taken place but I have done enough debugging to know that the entity comparison between current and previous state the lazy property is:
previous: IUnintitalisedLazyproperty
current: null

and then NH issues an update to the DB and makes the lazy property null. This only happens on Composite/ComposedId Entities though.

Any questions please ask. PR with test case on it to follow.

PR with test case (first attempt at a test case so bear with me)
#1440

To see the update you also need to flush all messages from NH (I saw this bug when using the NH Profiler).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions