Skip to content

Commit bb35733

Browse files
committed
NH2982
1 parent d2fd6d0 commit bb35733

File tree

6 files changed

+121
-7
lines changed

6 files changed

+121
-7
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using NUnit.Framework;
2+
using NHibernate.Criterion;
3+
4+
namespace NHibernate.Test.NHSpecificTest.NH2982
5+
{
6+
[TestFixture]
7+
public class Fixture : BugTestCase
8+
{
9+
protected override void OnSetUp()
10+
{
11+
base.OnSetUp();
12+
13+
using (ISession session = OpenSession())
14+
using (ITransaction transaction = session.BeginTransaction())
15+
{
16+
var e1 = new Entity { Id = 1, Name = "A" };
17+
session.Save(e1);
18+
transaction.Commit();
19+
}
20+
}
21+
22+
protected override void OnTearDown()
23+
{
24+
using (ISession session = OpenSession())
25+
using (ITransaction transaction = session.BeginTransaction())
26+
{
27+
session.Delete("from System.Object");
28+
session.Flush();
29+
transaction.Commit();
30+
}
31+
32+
base.OnTearDown();
33+
}
34+
35+
[Test]
36+
public void SimpleExpressionWithProxy()
37+
{
38+
using (ISession session = OpenSession())
39+
using (session.BeginTransaction())
40+
{
41+
var a = session.Load<Entity>(1);
42+
var restriction = Restrictions.Eq("A", a);
43+
Assert.AreEqual("A = Entity#1", restriction.ToString());
44+
}
45+
}
46+
47+
[Test]
48+
public void SimpleExpressionWithNewInstance()
49+
{
50+
var a = new Entity() { Id = 2, Name = "2" };
51+
var restriction = Restrictions.Eq("A", a);
52+
Assert.AreEqual(@"A = Entity@" + a.GetHashCode() + "(hash)", restriction.ToString());
53+
}
54+
55+
[Test]
56+
public void SimpleExpressionWithNull()
57+
{
58+
var restriction = Restrictions.Eq("A", null);
59+
Assert.AreEqual("A = null", restriction.ToString());
60+
}
61+
62+
[Test]
63+
public void SimpleExpressionWithPrimitive()
64+
{
65+
var restriction = Restrictions.Eq("A", 5);
66+
Assert.AreEqual("A = 5", restriction.ToString());
67+
}
68+
}
69+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping
3+
xmlns="urn:nhibernate-mapping-2.2"
4+
assembly="NHibernate.Test"
5+
namespace="NHibernate.Test.NHSpecificTest.NH2982">
6+
7+
<class name="Entity">
8+
<id name="Id"/>
9+
<property name="Name" />
10+
</class>
11+
12+
</hibernate-mapping>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Collections.Generic;
4+
5+
namespace NHibernate.Test.NHSpecificTest.NH2982
6+
{
7+
public class Entity
8+
{
9+
public virtual int Id { get; set; }
10+
public virtual string Name { get; set; }
11+
public override string ToString()
12+
{
13+
throw new InvalidOperationException(".ToString() is called which can result in lazy loading side effects.");
14+
}
15+
}
16+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,8 @@
937937
<Compile Include="NHSpecificTest\NH2913\Fixture.cs" />
938938
<Compile Include="NHSpecificTest\NH2959\Entity.cs" />
939939
<Compile Include="NHSpecificTest\NH2959\Fixture.cs" />
940+
<Compile Include="NHSpecificTest\NH2982\Fixture.cs" />
941+
<Compile Include="NHSpecificTest\NH2982\Model.cs" />
940942
<Compile Include="NHSpecificTest\NH3004\Fixture.cs" />
941943
<Compile Include="NHSpecificTest\NH3004\TestSqlClientDriver.cs" />
942944
<Compile Include="NHSpecificTest\NH941\Domain.cs" />
@@ -2797,6 +2799,7 @@
27972799
<Compile Include="IdGen\Enhanced\Forcedtable\BasicForcedTableSequenceTest.cs" />
27982800
<Compile Include="IdGen\Enhanced\Forcedtable\HiLoForcedTableSequenceTest.cs" />
27992801
<Compile Include="IdGen\Enhanced\Forcedtable\PooledForcedTableSequenceTest.cs" />
2802+
<EmbeddedResource Include="NHSpecificTest\NH2982\Mappings.hbm.xml" />
28002803
<EmbeddedResource Include="NHSpecificTest\NH2914\Mappings.hbm.xml" />
28012804
<EmbeddedResource Include="NHSpecificTest\NH2959\Mappings.hbm.xml" />
28022805
<Compile Include="IdGen\Enhanced\Table\Entity.cs" />

src/NHibernate/Criterion/SimpleExpression.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public override IProjection[] GetProjections()
161161

162162
public override string ToString()
163163
{
164-
return (_projection ?? (object)propertyName) + Op + value;
164+
return (_projection ?? (object)propertyName) + Op + ValueToStrings();
165165
}
166166

167167
/// <summary>
@@ -172,5 +172,14 @@ protected virtual string Op
172172
{
173173
get { return op; }
174174
}
175+
176+
string ValueToStrings()
177+
{
178+
if(value!=null && value.GetType().IsPrimitive)
179+
{
180+
return value.ToString();
181+
}
182+
return ObjectUtils.IdentityToString(value);
183+
}
175184
}
176185
}

src/NHibernate/Util/ObjectUtils.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,18 @@ public static string IdentityToString(object obj)
9898
{
9999
if (obj == null)
100100
{
101-
return null;
101+
return "null";
102102
}
103-
return new StringBuilder()
104-
.Append(obj.GetType().FullName)
105-
.Append('@')
106-
.Append(obj.GetHashCode())
107-
.ToString();
103+
104+
var proxy = obj as NHibernate.Proxy.INHibernateProxy;
105+
106+
if (null != proxy)
107+
{
108+
var init = proxy.HibernateLazyInitializer;
109+
return StringHelper.Unqualify(init.EntityName) + "#" + init.Identifier;
110+
}
111+
112+
return StringHelper.Unqualify(obj.GetType().FullName) + "@" + obj.GetHashCode() + "(hash)";
108113
}
109114

110115
private class NullClass

0 commit comments

Comments
 (0)