Skip to content

Fix transient detection for proxy #2045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 177 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH2043/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;
using NHibernate.Linq;

namespace NHibernate.Test.NHSpecificTest.GH2043
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : TestCaseMappingByCode
{
private Guid _entityWithClassProxy2Id;
private Guid _entityWithInterfaceProxy2Id;
private Guid _entityWithClassLookupId;
private Guid _entityWithInterfaceLookupId;

protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<EntityWithClassProxyDefinition>(rc =>
{
rc.Table("ProxyDefinition");
rc.Proxy(typeof(EntityWithClassProxyDefinition));

rc.Id(x => x.Id);
rc.Property(x => x.Name);
});

mapper.Class<EntityWithInterfaceProxyDefinition>(rc =>
{
rc.Table("IProxyDefinition");
rc.Proxy(typeof(IEntityProxy));

rc.Id(x => x.Id);
rc.Property(x => x.Name);
});

mapper.Class<EntityWithClassLookup>(rc =>
{
rc.Id(x => x.Id);
rc.Property(x => x.Name);
rc.ManyToOne(x => x.EntityLookup, x => x.Class(typeof(EntityWithClassProxyDefinition)));
});

mapper.Class<EntityWithInterfaceLookup>(rc =>
{
rc.Id(x => x.Id);
rc.Property(x => x.Name);
rc.ManyToOne(x => x.EntityLookup, x => x.Class(typeof(EntityWithInterfaceProxyDefinition)));
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}


protected override void OnSetUp()
{
using(var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var entityCP1 = new EntityWithClassProxyDefinition
{
Id = Guid.NewGuid(),
Name = "Name 1"
};

var entityCP2 = new EntityWithClassProxyDefinition
{
Id = Guid.NewGuid(),
Name = "Name 2"
};
_entityWithClassProxy2Id = entityCP2.Id;

var entityIP1 = new EntityWithInterfaceProxyDefinition
{
Id = Guid.NewGuid(),
Name = "Name 1"
};

var entityIP2 = new EntityWithInterfaceProxyDefinition
{
Id = Guid.NewGuid(),
Name = "Name 2"
};
_entityWithInterfaceProxy2Id = entityIP2.Id;

session.Save(entityCP1);
session.Save(entityCP2);
session.Save(entityIP1);
session.Save(entityIP2);

var entityCL = new EntityWithClassLookup
{
Id = Guid.NewGuid(),
Name = "Name 1",
EntityLookup = (EntityWithClassProxyDefinition)session.Load(typeof(EntityWithClassProxyDefinition), entityCP1.Id)
};
_entityWithClassLookupId = entityCL.Id;

var entityIL = new EntityWithInterfaceLookup
{
Id = Guid.NewGuid(),
Name = "Name 1",
EntityLookup = (IEntityProxy)session.Load(typeof(EntityWithInterfaceProxyDefinition), entityIP1.Id)
};
_entityWithInterfaceLookupId = entityIL.Id;

session.Save(entityCL);
session.Save(entityIL);

session.Flush();
transaction.Commit();
}
}


protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.Delete("from System.Object");

session.Flush();
transaction.Commit();
}
}


[Test]
public async Task UpdateEntityWithClassLookupAsync()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var entityToUpdate = await (session.Query<EntityWithClassLookup>()
.FirstAsync(e => e.Id == _entityWithClassLookupId));

entityToUpdate.EntityLookup = (EntityWithClassProxyDefinition) await (session.LoadAsync(typeof(EntityWithClassProxyDefinition), _entityWithClassProxy2Id));

await (session.UpdateAsync(entityToUpdate));
await (session.FlushAsync());
await (transaction.CommitAsync());
}
}


[Test]
public async Task UpdateEntityWithInterfaceLookupAsync()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var entityToUpdate = await (session.Query<EntityWithInterfaceLookup>()
.FirstAsync(e => e.Id == _entityWithInterfaceLookupId));

entityToUpdate.EntityLookup = (IEntityProxy) await (session.LoadAsync(typeof(EntityWithInterfaceProxyDefinition), _entityWithInterfaceProxy2Id));

await (session.UpdateAsync(entityToUpdate));
await (session.FlushAsync());
await (transaction.CommitAsync());
}
}
}
}
13 changes: 13 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2043/EntityWithClassLookup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH2043
{
public class EntityWithClassLookup
{
public virtual Guid Id { get; set; }

public virtual string Name { get; set; }

public virtual EntityWithClassProxyDefinition EntityLookup { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH2043
{
public class EntityWithClassProxyDefinition
{
public virtual Guid Id { get; set; }

public virtual string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH2043
{
public class EntityWithInterfaceLookup
{
public virtual Guid Id { get; set; }

public virtual string Name { get; set; }

public virtual IEntityProxy EntityLookup { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH2043
{
public class EntityWithInterfaceProxyDefinition: IEntityProxy
{
public virtual Guid Id { get; set; }

public virtual string Name { get; set; }
}
}
165 changes: 165 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2043/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
using System;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH2043
{
[TestFixture]
public class Fixture : TestCaseMappingByCode
{
private Guid _entityWithClassProxy2Id;
private Guid _entityWithInterfaceProxy2Id;
private Guid _entityWithClassLookupId;
private Guid _entityWithInterfaceLookupId;

protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<EntityWithClassProxyDefinition>(rc =>
{
rc.Table("ProxyDefinition");
rc.Proxy(typeof(EntityWithClassProxyDefinition));

rc.Id(x => x.Id);
rc.Property(x => x.Name);
});

mapper.Class<EntityWithInterfaceProxyDefinition>(rc =>
{
rc.Table("IProxyDefinition");
rc.Proxy(typeof(IEntityProxy));

rc.Id(x => x.Id);
rc.Property(x => x.Name);
});

mapper.Class<EntityWithClassLookup>(rc =>
{
rc.Id(x => x.Id);
rc.Property(x => x.Name);
rc.ManyToOne(x => x.EntityLookup, x => x.Class(typeof(EntityWithClassProxyDefinition)));
});

mapper.Class<EntityWithInterfaceLookup>(rc =>
{
rc.Id(x => x.Id);
rc.Property(x => x.Name);
rc.ManyToOne(x => x.EntityLookup, x => x.Class(typeof(EntityWithInterfaceProxyDefinition)));
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}


protected override void OnSetUp()
{
using(var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var entityCP1 = new EntityWithClassProxyDefinition
{
Id = Guid.NewGuid(),
Name = "Name 1"
};

var entityCP2 = new EntityWithClassProxyDefinition
{
Id = Guid.NewGuid(),
Name = "Name 2"
};
_entityWithClassProxy2Id = entityCP2.Id;

var entityIP1 = new EntityWithInterfaceProxyDefinition
{
Id = Guid.NewGuid(),
Name = "Name 1"
};

var entityIP2 = new EntityWithInterfaceProxyDefinition
{
Id = Guid.NewGuid(),
Name = "Name 2"
};
_entityWithInterfaceProxy2Id = entityIP2.Id;

session.Save(entityCP1);
session.Save(entityCP2);
session.Save(entityIP1);
session.Save(entityIP2);

var entityCL = new EntityWithClassLookup
{
Id = Guid.NewGuid(),
Name = "Name 1",
EntityLookup = (EntityWithClassProxyDefinition)session.Load(typeof(EntityWithClassProxyDefinition), entityCP1.Id)
};
_entityWithClassLookupId = entityCL.Id;

var entityIL = new EntityWithInterfaceLookup
{
Id = Guid.NewGuid(),
Name = "Name 1",
EntityLookup = (IEntityProxy)session.Load(typeof(EntityWithInterfaceProxyDefinition), entityIP1.Id)
};
_entityWithInterfaceLookupId = entityIL.Id;

session.Save(entityCL);
session.Save(entityIL);

session.Flush();
transaction.Commit();
}
}


protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.Delete("from System.Object");

session.Flush();
transaction.Commit();
}
}


[Test]
public void UpdateEntityWithClassLookup()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var entityToUpdate = session.Query<EntityWithClassLookup>()
.First(e => e.Id == _entityWithClassLookupId);

entityToUpdate.EntityLookup = (EntityWithClassProxyDefinition) session.Load(typeof(EntityWithClassProxyDefinition), _entityWithClassProxy2Id);

session.Update(entityToUpdate);
session.Flush();
transaction.Commit();
}
}


[Test]
public void UpdateEntityWithInterfaceLookup()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var entityToUpdate = session.Query<EntityWithInterfaceLookup>()
.First(e => e.Id == _entityWithInterfaceLookupId);

entityToUpdate.EntityLookup = (IEntityProxy) session.Load(typeof(EntityWithInterfaceProxyDefinition), _entityWithInterfaceProxy2Id);

session.Update(entityToUpdate);
session.Flush();
transaction.Commit();
}
}
}
}
Loading