Skip to content

Commit 80baac0

Browse files
committed
Merge branch 'JKeyser-NH-3453-NH-3480'
2 parents 283d0c1 + ad206cf commit 80baac0

File tree

18 files changed

+445
-56
lines changed

18 files changed

+445
-56
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace NHibernate.Test.NHSpecificTest.NH3453
7+
{
8+
class Direction
9+
{
10+
11+
#region Compisite ID
12+
13+
public virtual Int32 Id1 { get; set; }
14+
15+
public virtual Int32 Id2 { get; set; }
16+
17+
#endregion
18+
19+
public virtual Guid GUID { get; set; }
20+
21+
public override int GetHashCode()
22+
{
23+
return string.Format("{0} - {1}", Id1, Id2).GetHashCode();
24+
}
25+
26+
public override bool Equals(object obj)
27+
{
28+
return Id1 == ((Direction)obj).Id1 &&
29+
Id2 == ((Direction)obj).Id2;
30+
}
31+
}
32+
33+
class DirectionReferrer
34+
{
35+
public virtual Guid GUID { get; set; }
36+
37+
public virtual Direction Direction { get; set; }
38+
}
39+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using NUnit.Framework;
3+
4+
namespace NHibernate.Test.NHSpecificTest.NH3453
5+
{
6+
[TestFixture]
7+
public class Fixture : BugTestCase
8+
{
9+
public override string BugNumber
10+
{
11+
get { return "NH3453"; }
12+
}
13+
14+
[Test]
15+
public void PropertyRefWithCompositeIdUpdateTest()
16+
{
17+
using (var spy = new SqlLogSpy())
18+
using (var session = OpenSession())
19+
using (session.BeginTransaction())
20+
{
21+
22+
var direction1 = new Direction { Id1 = 1, Id2 = 1, GUID = Guid.NewGuid() };
23+
session.Save(direction1);
24+
25+
var direction2 = new Direction { Id1 = 2, Id2 = 2, GUID = Guid.NewGuid() };
26+
session.Save(direction2);
27+
28+
session.Flush();
29+
30+
var directionReferrer = new DirectionReferrer
31+
{
32+
GUID = Guid.NewGuid(),
33+
Direction = direction1,
34+
};
35+
36+
session.Save(directionReferrer);
37+
38+
directionReferrer.Direction = direction2;
39+
40+
session.Update(directionReferrer);
41+
42+
session.Flush();
43+
44+
Console.WriteLine(spy.ToString());
45+
Assert.That(true);
46+
}
47+
}
48+
49+
}
50+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.NHSpecificTest.NH3453"
5+
default-lazy="false"
6+
default-access="property">
7+
8+
<class name="Direction" table="Direction">
9+
<composite-id>
10+
<key-property column="Id1" name="Id1" />
11+
<key-property column="Id2" name="Id2" />
12+
</composite-id>
13+
<property column="GUID" name="GUID" not-null="true" unique="true"/>
14+
</class>
15+
16+
<class name="DirectionReferrer" table="DirectionReferrer">
17+
<id column="GUID" name="GUID"></id>
18+
<many-to-one name="Direction" column="guid_Direction" not-null="false" property-ref="GUID" />
19+
</class>
20+
21+
</hibernate-mapping>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace NHibernate.Test.NHSpecificTest.NH3480
5+
{
6+
class Entity
7+
{
8+
public Entity()
9+
{
10+
Children = new HashSet<Child>();
11+
}
12+
13+
public virtual Key Id { get; set; }
14+
public virtual string Name { get; set; }
15+
public virtual int OtherId { get; set; }
16+
public virtual ISet<Child> Children { get; set; }
17+
18+
public override bool Equals(object obj)
19+
{
20+
if(obj is Entity)
21+
{
22+
var otherEntity = (Entity)obj;
23+
return otherEntity.Id.Equals(this.Id);
24+
}
25+
return false;
26+
}
27+
28+
public override int GetHashCode()
29+
{
30+
return Id.GetHashCode();
31+
}
32+
33+
public class Key
34+
{
35+
public virtual Guid Id { get; set; }
36+
37+
public override bool Equals(object obj)
38+
{
39+
if (obj is Key)
40+
{
41+
var otherEntity = (Key)obj;
42+
return otherEntity.Id.Equals(this.Id);
43+
}
44+
return false;
45+
}
46+
47+
public override int GetHashCode()
48+
{
49+
// Needed to reproduce the problem
50+
return 20.GetHashCode();
51+
}
52+
}
53+
}
54+
55+
class Child
56+
{
57+
public virtual Guid Id { get; set; }
58+
public virtual string Name { get; set; }
59+
public virtual Entity Parent { get; set; }
60+
}
61+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Linq;
3+
using NHibernate.Linq;
4+
using NHibernate.Collection;
5+
using NUnit.Framework;
6+
7+
namespace NHibernate.Test.NHSpecificTest.NH3480
8+
{
9+
[TestFixture]
10+
public class Fixture : BugTestCase
11+
{
12+
public override string BugNumber
13+
{
14+
get { return "NH3480"; }
15+
}
16+
17+
protected override void OnSetUp()
18+
{
19+
using (ISession session = OpenSession())
20+
{
21+
using (ITransaction transaction = session.BeginTransaction())
22+
{
23+
var parent1 = new Entity { Id = new Entity.Key() { Id = Guid.NewGuid() }, Name = "Bob", OtherId = 20 };
24+
session.Save(parent1);
25+
26+
var child1 = new Child { Name = "Bob1", Parent = parent1 };
27+
session.Save(child1);
28+
29+
var child2 = new Child { Name = "Bob2", Parent = parent1 };
30+
session.Save(child2);
31+
32+
session.Flush();
33+
transaction.Commit();
34+
}
35+
}
36+
}
37+
38+
protected override void OnTearDown()
39+
{
40+
using (ISession session = OpenSession())
41+
{
42+
using (ITransaction transaction = session.BeginTransaction())
43+
{
44+
session.Delete("from System.Object");
45+
46+
session.Flush();
47+
transaction.Commit();
48+
}
49+
}
50+
}
51+
52+
[Test]
53+
public void Test()
54+
{
55+
using (ISession session = OpenSession())
56+
{
57+
using (session.BeginTransaction())
58+
{
59+
var result = from e in session.Query<Entity>()
60+
where e.Name == "Bob"
61+
select e;
62+
var entity = result.Single();
63+
64+
NHibernateUtil.Initialize(entity.Children);
65+
}
66+
}
67+
}
68+
}
69+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.NH3480">
3+
4+
<class name="Entity">
5+
<composite-id name="Id">
6+
<key-property name="Id" />
7+
</composite-id>
8+
<property name="Name" not-null="true" />
9+
<property name="OtherId" unique="true" not-null="true" />
10+
<set name="Children" cascade="all-delete-orphan" inverse="true">
11+
<key property-ref="OtherId" column="Parent" />
12+
<one-to-many class="Child" />
13+
</set>
14+
</class>
15+
16+
<class name="Child">
17+
<id name="Id" generator="guid.comb" />
18+
<property name="Name" not-null="true" />
19+
<many-to-one name="Parent" property-ref="OtherId" not-null="true" />
20+
</class>
21+
22+
</hibernate-mapping>

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,10 @@
720720
<Compile Include="Component\Basic\ComponentWithUniqueConstraintTests.cs" />
721721
<Compile Include="NHSpecificTest\NH3372\Entity.cs" />
722722
<Compile Include="NHSpecificTest\NH3372\Fixture.cs" />
723+
<Compile Include="NHSpecificTest\NH3453\Classes.cs" />
724+
<Compile Include="NHSpecificTest\NH3453\Fixture.cs" />
725+
<Compile Include="NHSpecificTest\NH3480\Entity.cs" />
726+
<Compile Include="NHSpecificTest\NH3480\Fixture.cs" />
723727
<Compile Include="NHSpecificTest\NH3487\Entity.cs" />
724728
<Compile Include="NHSpecificTest\NH3487\Fixture.cs" />
725729
<Compile Include="NHSpecificTest\NH3567\DomainClass.cs" />
@@ -3124,6 +3128,10 @@
31243128
<EmbeddedResource Include="LazyComponentTest\Person.hbm.xml" />
31253129
<Content Include="NHSpecificTest\NH3372\Mappings.hbm.xml" />
31263130
<EmbeddedResource Include="NHSpecificTest\NH3567\Mappings.hbm.xml" />
3131+
<EmbeddedResource Include="NHSpecificTest\NH3453\Mappings.hbm.xml" />
3132+
<EmbeddedResource Include="NHSpecificTest\NH3480\Mappings.hbm.xml">
3133+
<SubType>Designer</SubType>
3134+
</EmbeddedResource>
31273135
<EmbeddedResource Include="NHSpecificTest\NH3570\Mappings.hbm.xml" />
31283136
<EmbeddedResource Include="NHSpecificTest\NH2053\Mappings.hbm.xml" />
31293137
<EmbeddedResource Include="NHSpecificTest\NH3455\Mappings.hbm.xml" />

src/NHibernate/Engine/JoinHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static string[] GetRHSColumnNames(IAssociationType type, ISessionFactoryI
3131
IJoinable joinable = type.GetAssociatedJoinable(factory);
3232
if (uniqueKeyPropertyName == null)
3333
{
34-
return joinable.KeyColumnNames;
34+
return joinable.JoinColumnNames;
3535
}
3636
else
3737
{

src/NHibernate/Loader/Collection/OneToManyJoinWalker.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using NHibernate.Engine;
34
using NHibernate.Persister.Collection;
@@ -57,7 +58,7 @@ private void InitStatementString(IOuterJoinLoadable elementPersister, string ali
5758
int collectionJoins = CountCollectionPersisters(associations) + 1;
5859
CollectionSuffixes = BasicLoader.GenerateSuffixes(joins + 1, collectionJoins);
5960

60-
SqlStringBuilder whereString = WhereString(alias, oneToManyPersister.KeyColumnNames, subquery, batchSize);
61+
SqlStringBuilder whereString = WhereString(oneToManyPersister.GenerateTableAliasForKeyColumns(alias), oneToManyPersister.KeyColumnNames, subquery, batchSize);
6162
string filter = oneToManyPersister.FilterFragment(alias, EnabledFilters);
6263
whereString.Insert(0, StringHelper.MoveAndToBeginning(filter));
6364

@@ -66,7 +67,7 @@ private void InitStatementString(IOuterJoinLoadable elementPersister, string ali
6667
new SqlSelectBuilder(Factory).SetSelectClause(
6768
oneToManyPersister.SelectFragment(null, null, alias, Suffixes[joins], CollectionSuffixes[0], true)
6869
+ SelectString(associations)).SetFromClause(elementPersister.FromTableFragment(alias)
69-
+ elementPersister.FromJoinFragment(alias, true, true)).SetWhereClause(
70+
+ oneToManyPersister.FromJoinFragment(alias, true, true)).SetWhereClause(
7071
whereString.ToSqlString()).SetOuterJoins(ojf.ToFromFragmentString,
7172
ojf.ToWhereFragmentString
7273
+ elementPersister.WhereJoinFragment(alias, true, true));

0 commit comments

Comments
 (0)