Skip to content

Commit 009416e

Browse files
committed
Test case
1 parent 7530a25 commit 009416e

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using System.Collections.Generic;
2+
using NHibernate.Mapping.ByCode;
3+
using NHibernate.Mapping.ByCode.Conformist;
4+
5+
namespace NHibernate.Test.NHSpecificTest.GH2580
6+
{
7+
using Cascade = NHibernate.Mapping.ByCode.Cascade;
8+
9+
public class FamilyEntity
10+
{
11+
public virtual long Id { get; set; }
12+
public virtual ICollection<PersonEntity> Persons { get; set; }
13+
}
14+
15+
public class FamilyMap : ClassMapping<FamilyEntity>
16+
{
17+
public FamilyMap()
18+
{
19+
Table("Families");
20+
21+
Id(
22+
p => p.Id,
23+
id =>
24+
{
25+
id.Column("Family_Id");
26+
id.Generator(Generators.Increment);
27+
});
28+
29+
Bag(
30+
p => p.Persons,
31+
bag =>
32+
{
33+
bag.Inverse(true);
34+
bag.Cascade(Cascade.All.Include(Cascade.DeleteOrphans));
35+
bag.Lazy(CollectionLazy.NoLazy);
36+
bag.Key(k => k.Column("Family_Id"));
37+
},
38+
a => a.OneToMany());
39+
}
40+
}
41+
42+
public class PersonEntity
43+
{
44+
public virtual long Id { get; set; }
45+
46+
private FamilyEntity _family;
47+
48+
public virtual FamilyEntity Family
49+
{
50+
get { return _family; }
51+
set
52+
{
53+
_family?.Persons.Remove(this);
54+
_family = value;
55+
_family?.Persons.Add(this);
56+
}
57+
}
58+
}
59+
60+
public class PersonMap : ClassMapping<PersonEntity>
61+
{
62+
public PersonMap()
63+
{
64+
Table("Persons");
65+
66+
Id(
67+
p => p.Id,
68+
id =>
69+
{
70+
id.Column("Person_Id");
71+
id.Generator(Generators.Increment);
72+
});
73+
74+
ManyToOne(
75+
p => p.Family,
76+
r =>
77+
{
78+
r.Lazy(LazyRelation.NoLazy);
79+
r.Access(Accessor.Field);
80+
r.Column("Family_Id");
81+
r.NotNullable(true);
82+
});
83+
}
84+
}
85+
86+
public class AdultEntity : PersonEntity
87+
{
88+
}
89+
90+
public class AdultMap : JoinedSubclassMapping<AdultEntity>
91+
{
92+
public AdultMap()
93+
{
94+
Table("Adults");
95+
Key(prop => prop.Column("Person_Id"));
96+
}
97+
}
98+
99+
public class ChildEntity : PersonEntity
100+
{
101+
}
102+
103+
public class ChildMap : JoinedSubclassMapping<ChildEntity>
104+
{
105+
public ChildMap()
106+
{
107+
Table("Children");
108+
Key(prop => prop.Column("Person_Id"));
109+
}
110+
}
111+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System.Linq;
2+
using NHibernate.Cfg.MappingSchema;
3+
using NHibernate.Mapping.ByCode;
4+
using NUnit.Framework;
5+
6+
namespace NHibernate.Test.NHSpecificTest.GH2580
7+
{
8+
[TestFixture]
9+
public class JoinOnSubclassIssue : TestCaseMappingByCode
10+
{
11+
protected override HbmMapping GetMappings()
12+
{
13+
var mapper = new ModelMapper();
14+
mapper.AddMapping<FamilyMap>();
15+
mapper.AddMapping<PersonMap>();
16+
mapper.AddMapping<AdultMap>();
17+
mapper.AddMapping<ChildMap>();
18+
19+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
20+
}
21+
22+
protected override void OnSetUp()
23+
{
24+
using (var session = OpenSession())
25+
using (var transaction = session.BeginTransaction())
26+
{
27+
transaction.Commit();
28+
}
29+
}
30+
31+
protected override void OnTearDown()
32+
{
33+
using (var session = OpenSession())
34+
using (var transaction = session.BeginTransaction())
35+
{
36+
session.CreateQuery("delete from System.Object").ExecuteUpdate();
37+
38+
transaction.Commit();
39+
}
40+
}
41+
42+
[Test]
43+
public void JoinOnSubclassWithBaseTableReferenceInOnClause()
44+
{
45+
using(new SqlLogSpy())
46+
using (var session = OpenSession())
47+
{
48+
var ids = (from adult in session.Query<AdultEntity>()
49+
join child in session.Query<ChildEntity>()
50+
on adult.Family.Id equals child.Family.Id
51+
where adult.Id == 1
52+
select child.Id)
53+
.Distinct()
54+
.ToList();
55+
}
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)