Skip to content

NullReferenceException in EntityEntry.GetLoadedValue on an update of a never loaded detached entity #2137

Closed
@picbeats

Description

@picbeats

Update of detached entity added in a previous session fails with a NullReferenceException

Here is the test
Enity.cs:

using System;

namespace NHibernate.Test.NHSpecificTest.GH0000
{
	class Entity
	{
		public virtual Guid Id { get; set; }
		public virtual string Name { get; set; }
		public virtual ExtendedProperties ExtendedProperties { get; set; }
	}

	class ExtendedProperties
	{
		public virtual Guid Id { get; set; }

		public virtual string Value { get; set; }
	}
}


Mappings.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
                   namespace="NHibernate.Test.NHSpecificTest.GH0000">

  <class name="Entity">
    <id name="Id" generator="guid.comb"/>
    <property name="Name"/>
    <many-to-one name="ExtendedProperties" class="ExtendedProperties" unique="true" cascade="all-delete-orphan" lazy="proxy" />

  </class>

  <class name="ExtendedProperties">
    <id name="Id" generator="guid.comb"/>
    <property name="Value"/>
  </class>

</hibernate-mapping>


Fixture.cs

using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH0000
{
	[TestFixture]
	public class Fixture : BugTestCase
	{
		private Entity e1;

		protected override void OnSetUp()
		{
			using (var session = OpenSession())
			using (var transaction = session.BeginTransaction())
			{
				e1 = new Entity {Name = "Bob"};
				session.Save(e1);

				transaction.Commit();
			}
		}

		protected override void OnTearDown()
		{
			using (var session = OpenSession())
			using (var transaction = session.BeginTransaction())
			{
				// The HQL delete does all the job inside the database without loading the entities, but it does
				// not handle delete order for avoiding violating constraints if any. Use
				// session.Delete("from System.Object");
				// instead if in need of having NHibernate ordering the deletes, but this will cause
				// loading the entities in the session.
				session.CreateQuery("delete from System.Object").ExecuteUpdate();

				transaction.Commit();
			}
		}

		[Test]
		public void TestUpdateDetachedEntity()
		{
			using (var session = OpenSession())
			using (var transaction = session.BeginTransaction())
			{
				e1.Name = "Sally";
				session.Update(e1);
				transaction.Commit();
			}
		}
	}
}


Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions