Skip to content

NH-3487 #213

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 2 commits into from
Sep 25, 2014
Merged
Show file tree
Hide file tree
Changes from all 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
49 changes: 49 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3487/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using Iesi.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.NH3487
{
[Serializable()]
public class Entity
{
public virtual Key Id { get; set; }
public virtual string Name { get; set; }

public override bool Equals(object obj)
{
if(obj is Entity)
{
var otherEntity = (Entity)obj;
return otherEntity.Id.Equals(this.Id);
}
return false;
}

public override int GetHashCode()
{
return Id.GetHashCode();
}
}

[Serializable()]
public class Key
{
public virtual int Id { get; set; }

public override bool Equals(object obj)
{
if (obj is Key)
{
var otherEntity = (Key)obj;
return otherEntity.Id == this.Id;
}
return false;
}

public override int GetHashCode()
{
// Important to reproduce the problem - forces the keys to collide in the hash map
return 1;
}
}
}
88 changes: 88 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3487/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.Linq;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using NHibernate.Linq;
using NHibernate.Collection;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH3487
{
[TestFixture]
public class Fixture : BugTestCase
{
private Key key1;
private Key key2;

public override string BugNumber
{
get { return "NH3487"; }
}

protected override void OnSetUp()
{
using (ISession session = OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
key1 = new Key() { Id = 1 };
var entity1 = new Entity { Id = key1, Name = "Bob1" };
session.Save(entity1);

key2 = new Key() { Id = 2 };
var entity2 = new Entity { Id = key2, Name = "Bob2" };
session.Save(entity2);

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

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

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

[Test]
public void Test()
{
IFormatter formatter = new BinaryFormatter();
byte[] serializedSessionArray;

using (ISession session = OpenSession())
{
using (session.BeginTransaction())
{
var entity1 = session.Get<Entity>(key1);
var entity2 = session.Get<Entity>(key2);
}

session.Disconnect();
using (var serializationStream = new MemoryStream())
{
formatter.Serialize(serializationStream, session);
serializationStream.Close();
serializedSessionArray = serializationStream.ToArray();
}
}

ISession deserializedSession;
using (var serializationStream = new MemoryStream(serializedSessionArray))
{
deserializedSession = (ISession)formatter.Deserialize(serializationStream);
}

}
}
}
11 changes: 11 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3487/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.NH3487">

<class name="Entity">
<composite-id name="Id">
<key-property name="Id" />
</composite-id>
<property name="Name" not-null="true" />
</class>

</hibernate-mapping>
3 changes: 3 additions & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,8 @@
<Compile Include="NHSpecificTest\BagWithLazyExtraAndFilter\Domain.cs" />
<Compile Include="NHSpecificTest\BagWithLazyExtraAndFilter\Fixture.cs" />
<Compile Include="Component\Basic\ComponentWithUniqueConstraintTests.cs" />
<Compile Include="NHSpecificTest\NH3487\Entity.cs" />
<Compile Include="NHSpecificTest\NH3487\Fixture.cs" />
<Compile Include="NHSpecificTest\NH3459\Fixture.cs" />
<Compile Include="NHSpecificTest\NH3459\Order.cs" />
<Compile Include="NHSpecificTest\NH2692\Fixture.cs" />
Expand Down Expand Up @@ -2962,6 +2964,7 @@
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="NHSpecificTest\NH3487\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH2692\Mappings.hbm.xml">
<SubType>Designer</SubType>
</EmbeddedResource>
Expand Down
28 changes: 27 additions & 1 deletion src/NHibernate/Tuple/EntityModeToTuplizerMapping.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using NHibernate.Util;

namespace NHibernate.Tuple
{
/// <summary> Centralizes handling of <see cref="EntityMode"/> to <see cref="ITuplizer"/> mappings. </summary>
[Serializable]
public abstract class EntityModeToTuplizerMapping
public abstract class EntityModeToTuplizerMapping : IDeserializationCallback
{

// NH-1660
private readonly IDictionary<EntityMode, ITuplizer> tuplizers
= new LinkedHashMap<EntityMode, ITuplizer>(5, new EntityModeEqualityComparer());
[NonSerialized()]
private bool isFullyDeserialized = false;

public EntityModeToTuplizerMapping()
{
isFullyDeserialized = true;
}

protected internal void AddTuplizer(EntityMode entityMode, ITuplizer tuplizer)
{
EnsureFullyDeserialized();
tuplizers[entityMode] = tuplizer;
}

Expand All @@ -22,6 +32,7 @@ protected internal void AddTuplizer(EntityMode entityMode, ITuplizer tuplizer)
/// <returns> The guessed entity mode. </returns>
public virtual EntityMode? GuessEntityMode(object obj)
{
EnsureFullyDeserialized();
foreach (KeyValuePair<EntityMode, ITuplizer> entry in tuplizers)
{
ITuplizer tuplizer = entry.Value;
Expand All @@ -41,6 +52,7 @@ protected internal void AddTuplizer(EntityMode entityMode, ITuplizer tuplizer)
/// <returns> The tuplizer, or null if not found. </returns>
public virtual ITuplizer GetTuplizerOrNull(EntityMode entityMode)
{
EnsureFullyDeserialized();
ITuplizer result;
tuplizers.TryGetValue(entityMode, out result);
return result;
Expand All @@ -65,5 +77,19 @@ public virtual ITuplizer GetTuplizer(EntityMode entityMode)
}
return tuplizer;
}

private void EnsureFullyDeserialized()
{
if (!isFullyDeserialized)
{
((IDeserializationCallback)this).OnDeserialization(this);
}
}

void IDeserializationCallback.OnDeserialization(object sender)
{
((IDeserializationCallback)tuplizers).OnDeserialization(sender);
isFullyDeserialized = true;
}
}
}
8 changes: 7 additions & 1 deletion src/NHibernate/Util/LinkedHashMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Text;
using NHibernate.DebugHelpers;

Expand All @@ -18,7 +19,7 @@ namespace NHibernate.Util
/// </remarks>
[DebuggerTypeProxy(typeof(CollectionProxy<>))]
[Serializable]
public class LinkedHashMap<TKey, TValue> : IDictionary<TKey, TValue>
public class LinkedHashMap<TKey, TValue> : IDictionary<TKey, TValue>, IDeserializationCallback
{
[Serializable]
protected class Entry
Expand Down Expand Up @@ -359,6 +360,11 @@ private bool RemoveImpl(TKey key)
return result;
}

void IDeserializationCallback.OnDeserialization(object sender)
{
((IDeserializationCallback)entries).OnDeserialization(sender);
}

#region System.Object Members

public override string ToString()
Expand Down