Skip to content
Browse files

Back ported fix for NH-995 to 1.2.x branch

SVN: branches/1.2.x@3095
  • Loading branch information...
1 parent b1f1f7b commit 33ee042444fef9d1b9a17874f141031b90138dd4 Karl Chu committed Nov 12, 2007
View
91 src/NHibernate.Test/NHSpecificTest/NH995/Fixture.cs
@@ -0,0 +1,91 @@
+using System.Collections.Generic;
+using NUnit.Framework;
+
+namespace NHibernate.Test.NHSpecificTest.NH995
+{
+ [TestFixture]
+ public class Fixture : BugTestCase
+ {
+ public override string BugNumber
+ {
+ get
+ {
+ return "NH995";
+ }
+ }
+
+ protected override void OnTearDown()
+ {
+ using (ISession s = OpenSession())
+ using(ITransaction tx = s.BeginTransaction())
+ {
+ s.Delete("from ClassC");
+ s.Delete("from ClassB");
+ s.Delete("from ClassA");
+ tx.Commit();
+ }
+ }
+
+ [Test]
+ public void Test()
+ {
+ int a_id;
+ using(ISession s = OpenSession())
+ using(ITransaction tx = s.BeginTransaction())
+ {
+ // Create an A and save it
+ ClassA a = new ClassA();
+ a.Name = "a1";
+ s.Save(a);
+
+ // Create a B and save it
+ ClassB b = new ClassB();
+ b.Id = new ClassBId("bbb", a);
+ b.SomeProp = "Some property";
+ s.Save(b);
+
+ // Create a C and save it
+ ClassC c = new ClassC();
+ c.B = b;
+ s.Save(c);
+
+ tx.Commit();
+
+ a_id = a.Id;
+ }
+
+ // Clear the cache
+ sessions.Evict(typeof(ClassA));
+ sessions.Evict(typeof(ClassB));
+ sessions.Evict(typeof(ClassC));
+
+ using(ISession s = OpenSession())
+ using (ITransaction tx = s.BeginTransaction())
+ {
+ // Load a so we can use it to load b
+ ClassA a = s.Get<ClassA>(a_id);
+
+ // Load b so b will be in cache
+ ClassB b = s.Get<ClassB>(new ClassBId("bbb", a));
+
+ tx.Commit();
+ }
+
+ using(ISession s = OpenSession())
+ using(ITransaction tx = s.BeginTransaction())
+ {
+ using (SqlLogSpy sqlLogSpy = new SqlLogSpy())
+ {
+ IList<ClassC> c_list = s.CreateCriteria(typeof (ClassC)).List<ClassC>();
+ // make sure we initialize B
+ NHibernateUtil.Initialize(c_list[0].B);
+
+ Assert.AreEqual(1, sqlLogSpy.Appender.GetEvents().Length,
+ "Only one SQL should have been issued");
+ }
+
+ tx.Commit();
+ }
+ }
+ }
+}
View
23 src/NHibernate.Test/NHSpecificTest/NH995/LogPlugin.cs
@@ -0,0 +1,23 @@
+using log4net.Plugin;
+using log4net.Repository;
+
+namespace NHibernate.Test.NHSpecificTest.NH995
+{
+ public class LogPlugin : IPlugin
+ {
+ public void Attach(ILoggerRepository repository)
+ {
+
+ }
+
+ public void Shutdown()
+ {
+ // Nothing to do
+ }
+
+ public string Name
+ {
+ get { return "NH995"; }
+ }
+ }
+}
View
31 src/NHibernate.Test/NHSpecificTest/NH995/Mappings.hbm.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
+ namespace="NHibernate.Test.NHSpecificTest.NH995"
+ assembly="NHibernate.Test">
+
+ <class name="ClassA">
+ <id name="Id">
+ <generator class="native"/>
+ </id>
+ <property name="Name"/>
+ </class>
+
+ <class name="ClassB">
+ <composite-id name="Id" class="ClassBId">
+ <key-property name="Code"/>
+ <key-many-to-one name="A"/>
+ </composite-id>
+ <property name="SomeProp"/>
+ </class>
+
+ <class name="ClassC">
+ <id name="Id">
+ <generator class="native"/>
+ </id>
+ <many-to-one name="B">
+ <column name="B_Code"/>
+ <column name="B_A_Id"/>
+ </many-to-one>
+ </class>
+
+</hibernate-mapping>
View
126 src/NHibernate.Test/NHSpecificTest/NH995/Model.cs
@@ -0,0 +1,126 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace NHibernate.Test.NHSpecificTest.NH995
+{
+ [Serializable]
+ public class ClassA
+ {
+ private int id;
+ private string name;
+
+ public virtual int Id
+ {
+ get { return id; }
+ set { id = value; }
+ }
+
+ public virtual string Name
+ {
+ get { return name; }
+ set { name = value; }
+ }
+
+ public override bool Equals(object obj)
+ {
+ ClassA other = obj as ClassA;
+ if (other == null) return false;
+ return this.Id == other.Id;
+ }
+
+ public override int GetHashCode()
+ {
+ return id;
+ }
+ }
+
+ [Serializable]
+ public class ClassBId
+ {
+ private string code;
+ private ClassA a;
+
+ public static int counter = 0;
+ public static Dictionary<int, ClassBId> IdInstances = new Dictionary<int, ClassBId>();
+ public readonly int ID;
+
+ private ClassBId()
+ {
+ this.ID = ++counter;
+ IdInstances.Add(this.ID, this);
+ }
+
+ public ClassBId(string code, ClassA a)
+ : this()
+ {
+ this.code = code;
+ this.a = a;
+ }
+
+ public virtual string Code
+ {
+ get { return code; }
+ set { code = value; }
+ }
+
+ public virtual ClassA A
+ {
+ get { return a; }
+ set { a = value; }
+ }
+
+ public override bool Equals(object obj)
+ {
+ ClassBId other = obj as ClassBId;
+ if (other == null) return false;
+
+ if (this.code != other.code) return false;
+
+ return this.A.Id == other.A.Id;
+ }
+
+ public override int GetHashCode()
+ {
+ return Code.GetHashCode();
+ }
+ }
+
+ [Serializable]
+ public class ClassB
+ {
+ private ClassBId id;
+ private string someProp;
+
+ public virtual ClassBId Id
+ {
+ get { return id; }
+ set { id = value; }
+ }
+
+ public virtual string SomeProp
+ {
+ get { return someProp; }
+ set { someProp = value; }
+ }
+ }
+
+ [Serializable]
+ public class ClassC
+ {
+ private int id;
+ private ClassB b;
+
+ public virtual int Id
+ {
+ get { return id; }
+ set { id = value; }
+ }
+
+ public virtual ClassB B
+ {
+ get { return b; }
+ set { b = value; }
+ }
+ }
+}
View
7 src/NHibernate.Test/NHibernate.Test-2.0.csproj
@@ -436,6 +436,9 @@
<Compile Include="NHSpecificTest\NH965\NH965Fixture.cs" />
<Compile Include="NHSpecificTest\NH980\IdOnly.cs" />
<Compile Include="NHSpecificTest\NH980\NH980Fixture.cs" />
+ <Compile Include="NHSpecificTest\NH995\Fixture.cs" />
+ <Compile Include="NHSpecificTest\NH995\LogPlugin.cs" />
+ <Compile Include="NHSpecificTest\NH995\Model.cs" />
<Compile Include="NHSpecificTest\NodeFixture.cs" />
<Compile Include="NHSpecificTest\OptimisticConcurrencyFixture.cs" />
<Compile Include="NHSpecificTest\ProxyValidator\Fixture.cs" />
@@ -490,6 +493,7 @@
<Compile Include="SqlCommandTest\SqlStringParameterFixture.cs" />
<Compile Include="SqlCommandTest\SqlUpdateBuilderFixture.cs" />
<Compile Include="SqlCommandTest\TemplateFixture.cs" />
+ <Compile Include="SqlLogSpy.cs" />
<Compile Include="SqlTest\Dimension.cs" />
<Compile Include="SqlTest\Employment.cs" />
<Compile Include="SqlTest\FireBirdTest.cs" />
@@ -932,6 +936,9 @@
<EmbeddedResource Include="NHSpecificTest\NH901\Mappings.hbm.xml" />
</ItemGroup>
<ItemGroup>
+ <EmbeddedResource Include="NHSpecificTest\NH995\Mappings.hbm.xml" />
+ </ItemGroup>
+ <ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
View
50 src/NHibernate.Test/SqlLogSpy.cs
@@ -0,0 +1,50 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using log4net;
+using log4net.Appender;
+using log4net.Core;
+using log4net.Repository.Hierarchy;
+
+namespace NHibernate.Test
+{
+ /// <summary>
+ /// A disposible object that taps into the "NHibernate.SQL" logger and
+ /// collection the log entries being logged. This class should be used
+ /// with a C# using-statement
+ /// </summary>
+ public class SqlLogSpy : IDisposable
+ {
+ private Logger sqlLogger;
+ Level prevLogLevel;
+ private MemoryAppender appender;
+
+ public MemoryAppender Appender
+ {
+ get { return appender; }
+ }
+
+ public SqlLogSpy()
+ {
+ ILog log = LogManager.GetLogger("NHibernate.SQL");
+ sqlLogger = log.Logger as Logger;
+ if (sqlLogger == null)
+ throw new Exception("Unable to get the SQL logger");
+
+ // Change the log level to DEBUG and temporarily save the previous log level
+ prevLogLevel = sqlLogger.Level;
+ sqlLogger.Level = Level.Debug;
+
+ // Add a new MemoryAppender to the logger.
+ appender = new MemoryAppender();
+ sqlLogger.AddAppender(appender);
+ }
+
+ public void Dispose()
+ {
+ // Restore the previous log level of the SQL logger and remove the MemoryAppender
+ sqlLogger.Level = prevLogLevel;
+ sqlLogger.RemoveAppender(appender);
+ }
+ }
+}
View
7 src/NHibernate/Type/EntityType.cs
@@ -32,7 +32,12 @@ public System.Type AssociatedClass
public override sealed bool Equals(object x, object y)
{
- return x == y;
+ if (x == y)
+ return true;
+ else if (x != null)
+ return x.Equals(y);
+ else
+ return y.Equals(x);
}
public override int GetHashCode(object x, ISessionFactoryImplementor factory)

0 comments on commit 33ee042

Please sign in to comment.
Something went wrong with that request. Please try again.