Skip to content

Fixes for NH-3480 and NH-3453 #211

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

Closed
wants to merge 5 commits into from
Closed
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
39 changes: 39 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3453/Classes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NHibernate.Test.NHSpecificTest.NH3453
{
class Direction
{

#region Compisite ID

public virtual Int32 Id1 { get; set; }

public virtual Int32 Id2 { get; set; }

#endregion

public virtual Guid GUID { get; set; }

public override int GetHashCode()
{
return string.Format("{0} - {1}", Id1, Id2).GetHashCode();
}

public override bool Equals(object obj)
{
return Id1 == ((Direction)obj).Id1 &&
Id2 == ((Direction)obj).Id2;
}
}

class DirectionReferrer
{
public virtual Guid GUID { get; set; }

public virtual Direction Direction { get; set; }
}
}
50 changes: 50 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3453/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH3453
{
[TestFixture]
public class Fixture : BugTestCase
{
public override string BugNumber
{
get { return "NH3453"; }
}

[Test]
public void PropertyRefWithCompositeIdUpdateTest()
{
using (var spy = new SqlLogSpy())
using (var session = OpenSession())
using (session.BeginTransaction())
{

var direction1 = new Direction { Id1 = 1, Id2 = 1, GUID = Guid.NewGuid() };
session.Save(direction1);

var direction2 = new Direction { Id1 = 2, Id2 = 2, GUID = Guid.NewGuid() };
session.Save(direction2);

session.Flush();

var directionReferrer = new DirectionReferrer
{
GUID = Guid.NewGuid(),
Direction = direction1,
};

session.Save(directionReferrer);

directionReferrer.Direction = direction2;

session.Update(directionReferrer);

session.Flush();

Console.WriteLine(spy.ToString());
Assert.That(true);
}
}

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

<class name="Direction" table="Direction">
<composite-id>
<key-property column="Id1" name="Id1" />
<key-property column="Id2" name="Id2" />
</composite-id>
<property column="GUID" name="GUID" not-null="true" unique="true"/>
</class>

<class name="DirectionReferrer" table="DirectionReferrer">
<id column="GUID" name="GUID"></id>
<many-to-one name="Direction" column="guid_Direction" not-null="false" property-ref="GUID" />
</class>

</hibernate-mapping>
61 changes: 61 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3480/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.NH3480
{
class Entity
{
public Entity()
{
Children = new HashSet<Child>();
}

public virtual Key Id { get; set; }
public virtual string Name { get; set; }
public virtual int OtherId { get; set; }
public virtual ISet<Child> Children { 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();
}

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

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

public override int GetHashCode()
{
// Needed to reproduce the problem
return 20.GetHashCode();
}
}
}

class Child
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual Entity Parent { get; set; }
}
}
69 changes: 69 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3480/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Linq;
using NHibernate.Linq;
using NHibernate.Collection;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH3480
{
[TestFixture]
public class Fixture : BugTestCase
{
public override string BugNumber
{
get { return "NH3480"; }
}

protected override void OnSetUp()
{
using (ISession session = OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
var parent1 = new Entity { Id = new Entity.Key() { Id = Guid.NewGuid() }, Name = "Bob", OtherId = 20 };
session.Save(parent1);

var child1 = new Child { Name = "Bob1", Parent = parent1 };
session.Save(child1);

var child2 = new Child { Name = "Bob2", Parent = parent1 };
session.Save(child2);

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()
{
using (ISession session = OpenSession())
{
using (session.BeginTransaction())
{
var result = from e in session.Query<Entity>()
where e.Name == "Bob"
select e;
var entity = result.Single();

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

<class name="Entity">
<composite-id name="Id">
<key-property name="Id" />
</composite-id>
<property name="Name" not-null="true" />
<property name="OtherId" unique="true" not-null="true" />
<set name="Children" cascade="all-delete-orphan" inverse="true">
<key property-ref="OtherId" column="Parent" />
<one-to-many class="Child" />
</set>
</class>

<class name="Child">
<id name="Id" generator="guid.comb" />
<property name="Name" not-null="true" />
<many-to-one name="Parent" property-ref="OtherId" not-null="true" />
</class>

</hibernate-mapping>
8 changes: 8 additions & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,10 @@
<Compile Include="NHSpecificTest\BagWithLazyExtraAndFilter\Domain.cs" />
<Compile Include="NHSpecificTest\BagWithLazyExtraAndFilter\Fixture.cs" />
<Compile Include="Component\Basic\ComponentWithUniqueConstraintTests.cs" />
<Compile Include="NHSpecificTest\NH3453\Classes.cs" />
<Compile Include="NHSpecificTest\NH3453\Fixture.cs" />
<Compile Include="NHSpecificTest\NH3480\Entity.cs" />
<Compile Include="NHSpecificTest\NH3480\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 +2966,10 @@
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="NHSpecificTest\NH3453\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3480\Mappings.hbm.xml">
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="NHSpecificTest\NH2692\Mappings.hbm.xml">
<SubType>Designer</SubType>
</EmbeddedResource>
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Engine/JoinHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public static string[] GetRHSColumnNames(IAssociationType type, ISessionFactoryI
string uniqueKeyPropertyName = type.RHSUniqueKeyPropertyName;
IJoinable joinable = type.GetAssociatedJoinable(factory);
if (uniqueKeyPropertyName == null)
{
return joinable.KeyColumnNames;
{
return joinable.JoinColumnNames;
}
else
{
Expand Down
5 changes: 3 additions & 2 deletions src/NHibernate/Loader/Collection/OneToManyJoinWalker.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using NHibernate.Engine;
using NHibernate.Persister.Collection;
Expand Down Expand Up @@ -57,7 +58,7 @@ private void InitStatementString(IOuterJoinLoadable elementPersister, string ali
int collectionJoins = CountCollectionPersisters(associations) + 1;
CollectionSuffixes = BasicLoader.GenerateSuffixes(joins + 1, collectionJoins);

SqlStringBuilder whereString = WhereString(alias, oneToManyPersister.KeyColumnNames, subquery, batchSize);
SqlStringBuilder whereString = WhereString(oneToManyPersister.GenerateTableAliasForKeyColumns(alias), oneToManyPersister.KeyColumnNames, subquery, batchSize);
string filter = oneToManyPersister.FilterFragment(alias, EnabledFilters);
whereString.Insert(0, StringHelper.MoveAndToBeginning(filter));

Expand All @@ -66,7 +67,7 @@ private void InitStatementString(IOuterJoinLoadable elementPersister, string ali
new SqlSelectBuilder(Factory).SetSelectClause(
oneToManyPersister.SelectFragment(null, null, alias, Suffixes[joins], CollectionSuffixes[0], true)
+ SelectString(associations)).SetFromClause(elementPersister.FromTableFragment(alias)
+ elementPersister.FromJoinFragment(alias, true, true)).SetWhereClause(
+ oneToManyPersister.FromJoinFragment(alias, true, true)).SetWhereClause(
whereString.ToSqlString()).SetOuterJoins(ojf.ToFromFragmentString,
ojf.ToWhereFragmentString
+ elementPersister.WhereJoinFragment(alias, true, true));
Expand Down
Loading