Skip to content

Commit 10d11d1

Browse files
committed
Ported back fix for NH-3057
1 parent 9594b02 commit 10d11d1

File tree

5 files changed

+124
-4
lines changed

5 files changed

+124
-4
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.Generic;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH3057
4+
{
5+
public abstract class BaseClass
6+
{
7+
public virtual int Id { get; set; }
8+
public virtual string InheritedProperty { get; set; }
9+
}
10+
11+
public class AClass
12+
{
13+
private ICollection<BClass> bs = new List<BClass>();
14+
15+
public virtual int Id { get; set; }
16+
17+
public virtual ICollection<BClass> Bs
18+
{
19+
get { return bs; }
20+
protected internal set { bs = value; }
21+
}
22+
}
23+
24+
public class BClass : BaseClass
25+
{
26+
public virtual AClass A { get; set; }
27+
}
28+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.Linq;
2+
using NHibernate.Linq;
3+
using NUnit.Framework;
4+
5+
namespace NHibernate.Test.NHSpecificTest.NH3057
6+
{
7+
[TestFixture]
8+
public class Fixture : BugTestCase
9+
{
10+
protected override void OnSetUp()
11+
{
12+
using (var session = OpenSession())
13+
using (var transaction = session.BeginTransaction())
14+
{
15+
var a = new AClass { Id = 1 };
16+
session.Save(a);
17+
18+
var b = new BClass { Id = 2, A = a, InheritedProperty = "B2" };
19+
session.Save(b);
20+
21+
transaction.Commit();
22+
}
23+
}
24+
25+
protected override void OnTearDown()
26+
{
27+
using (var session = OpenSession())
28+
using (var transaction = session.BeginTransaction())
29+
{
30+
session.Delete("from System.Object");
31+
transaction.Commit();
32+
}
33+
}
34+
35+
[Test]
36+
public void CollectionQueryOnJoinedSubclassInheritedProperty()
37+
{
38+
using (var session = OpenSession())
39+
using (session.BeginTransaction())
40+
{
41+
var entities = session.Query<AClass>()
42+
.Where(a => a.Bs.Any(b => b.InheritedProperty == "B2"))
43+
.ToList();
44+
45+
Assert.AreEqual(1, entities.Count);
46+
Assert.AreEqual(1, entities[0].Id);
47+
}
48+
}
49+
50+
[Test]
51+
public void CollectionQueryOnJoinedSubclassInheritedPropertyHql()
52+
{
53+
using (var session = OpenSession())
54+
using (session.BeginTransaction())
55+
{
56+
var entities = session.CreateQuery("from AClass a where exists (from a.Bs b where b.InheritedProperty = 'B2')")
57+
.List<AClass>();
58+
59+
Assert.AreEqual(1, entities.Count);
60+
Assert.AreEqual(1, entities[0].Id);
61+
}
62+
}
63+
}
64+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.NH3057">
5+
6+
<class name="BaseClass" table="baseclass">
7+
<id name="Id" column="id">
8+
<generator class="assigned" />
9+
</id>
10+
<property name="InheritedProperty" column="inheritedproperty" />
11+
12+
<joined-subclass name="BClass" table="bclass">
13+
<key column="id" />
14+
<many-to-one name="A" column="aid" class="AClass" />
15+
</joined-subclass>
16+
</class>
17+
18+
<class name="AClass" table="aclass">
19+
<id name="Id" column="id" generator="assigned" />
20+
<set name="Bs" inverse="true" lazy="true">
21+
<key column="aid" />
22+
<one-to-many class="BClass" />
23+
</set>
24+
</class>
25+
</hibernate-mapping>

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,8 @@
687687
<Compile Include="NHSpecificTest\NH3332\State.cs" />
688688
<Compile Include="NHSpecificTest\NH3332\StateDescription.cs" />
689689
<Compile Include="NHSpecificTest\NH3332\TestJoinsWithSameTable.cs" />
690+
<Compile Include="NHSpecificTest\NH3057\Domain.cs" />
691+
<Compile Include="NHSpecificTest\NH3057\Fixture.cs" />
690692
<Compile Include="NHSpecificTest\NH2651\Fixture.cs" />
691693
<Compile Include="NHSpecificTest\NH2651\Model.cs" />
692694
<Compile Include="NHSpecificTest\NH2789\Entities.cs" />
@@ -2877,6 +2879,7 @@
28772879
<EmbeddedResource Include="NHSpecificTest\NH2860\Mappings.hbm.xml" />
28782880
<EmbeddedResource Include="NHSpecificTest\NH3332\Mappings.hbm.xml" />
28792881
<EmbeddedResource Include="NHSpecificTest\NH3050\Mappings.hbm.xml" />
2882+
<EmbeddedResource Include="NHSpecificTest\NH3057\Mappings.hbm.xml" />
28802883
<EmbeddedResource Include="NHSpecificTest\NH2651\Mappings.hbm.xml" />
28812884
<EmbeddedResource Include="NHSpecificTest\NH3121\Mappings.hbm.xml" />
28822885
<EmbeddedResource Include="NHSpecificTest\NH2789\Mappings.hbm.xml" />

src/NHibernate/Hql/Ast/ANTLR/Tree/FromElementFactory.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,10 @@ private FromElement CreateEntityAssociation(
348348
Log.Debug("createEntityAssociation() : One to many - path = " + _path + " role = " + role + " associatedEntityName = " + associatedEntityName);
349349
}
350350

351-
JoinSequence joinSequence = CreateJoinSequence(roleAlias, joinType);
351+
var joinSequence = CreateJoinSequence(roleAlias, joinType);
352352

353-
elem = CreateJoin(associatedEntityName, roleAlias, joinSequence, (EntityType)_queryableCollection.ElementType, false);
353+
elem = CreateJoin(associatedEntityName, roleAlias, joinSequence, (EntityType) _queryableCollection.ElementType, false);
354+
elem.UseFromFragment |= elem.IsImplied && elem.Walker.IsSubQuery;
354355
}
355356
else
356357
{
@@ -359,8 +360,7 @@ private FromElement CreateEntityAssociation(
359360
Log.Debug("createManyToMany() : path = " + _path + " role = " + role + " associatedEntityName = " + associatedEntityName);
360361
}
361362

362-
elem = CreateManyToMany(role, associatedEntityName,
363-
roleAlias, entityPersister, (EntityType)_queryableCollection.ElementType, joinType);
363+
elem = CreateManyToMany(role, associatedEntityName, roleAlias, entityPersister, (EntityType)_queryableCollection.ElementType, joinType);
364364
_fromClause.Walker.AddQuerySpaces(_queryableCollection.CollectionSpaces);
365365
}
366366
elem.CollectionTableAlias = roleAlias;

0 commit comments

Comments
 (0)