Skip to content

Commit c1f0cd8

Browse files
author
Gunnar Liljas
committed
NH3567 - Unit test (from JIRA reporter) and fix
1 parent 6fedcee commit c1f0cd8

File tree

5 files changed

+204
-0
lines changed

5 files changed

+204
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+

2+
3+
namespace NHibernate.Test.NHSpecificTest.NH3567
4+
{
5+
public class Site
6+
{
7+
public virtual int Id { get; set; }
8+
public virtual string Name { get; set; }
9+
10+
11+
}
12+
public class Post
13+
{
14+
public virtual int Id { get; set; }
15+
public virtual string Content { get; set; }
16+
public virtual Site Site { get; set; }
17+
18+
19+
}
20+
21+
public class Comment
22+
{
23+
public virtual int Id { get; set; }
24+
public virtual string Content { get; set; }
25+
public virtual Post Post { get; set; }
26+
27+
}
28+
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
3+
namespace="NHibernate.Test.NHSpecificTest.NH3567" >
4+
5+
<class name="Site" >
6+
<id name="Id" type="Int32">
7+
<generator class="assigned" />
8+
</id>
9+
<property name="Name" length="2000" />
10+
</class>
11+
12+
<class name="Post" >
13+
<id name="Id" type="Int32">
14+
<generator class="assigned" />
15+
</id>
16+
<property name="Content" length="2000" />
17+
<many-to-one name="Site" column="PostId" not-null="true"/>
18+
</class>
19+
20+
<class name="Comment" table="Cmt">
21+
<id name="Id" type="Int32">
22+
<generator class="assigned" />
23+
</id>
24+
<property name="Content" length="2000" />
25+
26+
<many-to-one name="Post" column="PostId" not-null="true"/>
27+
</class>
28+
</hibernate-mapping>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using NUnit.Framework;
2+
using NHibernate.Criterion;
3+
using NHibernate.Dialect;
4+
5+
namespace NHibernate.Test.NHSpecificTest.NH3567
6+
{
7+
[TestFixture]
8+
public class NH3567Tests : BugTestCase
9+
{
10+
protected override void OnSetUp()
11+
{
12+
base.OnSetUp();
13+
using (ISession session = this.OpenSession())
14+
{
15+
session.BeginTransaction();
16+
var id = 0;
17+
18+
var site1 = new Site { Id = ++id, Name = "Site 1" };
19+
var site2 = new Site { Id = ++id, Name = "Site 1" };
20+
session.Save(site1);
21+
session.Save(site2);
22+
23+
24+
var p1 = new Post { Id = ++id, Content = "Post 1", Site = site1 };
25+
var p2 = new Post { Id = ++id, Content = "Post 2", Site = site2 };
26+
27+
session.Save(p1);
28+
session.Save(p2);
29+
30+
session.Save(new Comment { Id = ++id, Content = "Comment 1.1", Post = p1 });
31+
session.Save(new Comment { Id = ++id, Content = "Comment 1.2", Post = p1 });
32+
session.Save(new Comment { Id = ++id, Content = "Comment 2.1", Post = p2 });
33+
session.Save(new Comment { Id = ++id, Content = "Comment 2.2", Post = p2 });
34+
session.Flush();
35+
session.Transaction.Commit();
36+
}
37+
}
38+
39+
protected override void OnTearDown()
40+
{
41+
base.OnTearDown();
42+
using (ISession session = this.OpenSession())
43+
{
44+
session.Delete("from Comment");
45+
session.Delete("from Post");
46+
session.Delete("from Site");
47+
session.Flush();
48+
}
49+
}
50+
51+
protected override bool AppliesTo(NHibernate.Dialect.Dialect dialect)
52+
{
53+
return dialect as MsSql2005Dialect != null;
54+
}
55+
56+
[Test]
57+
public void TestFlushModeAuto()
58+
{
59+
using (ISession session = this.OpenSession())
60+
{
61+
session.FlushMode = FlushMode.Auto;
62+
using (var transaction = session.BeginTransaction())
63+
{
64+
var post = session.QueryOver<Post>().Where(x => x.Content == "Post 1").SingleOrDefault();
65+
66+
post.Content = "1";
67+
68+
var comments = session.QueryOver<Comment>().JoinQueryOver(x => x.Post).Where(x => x.Content == "1").List();
69+
Assert.That(comments.Count, Is.EqualTo(2), "Query over returned something different than 2");
70+
71+
post.Content = "I";
72+
var subquery = DetachedCriteria.For(typeof(Post))
73+
.Add(Restrictions.Eq("Content", "I"))
74+
.SetProjection(Projections.Id());
75+
var numberOfComments =
76+
session.CreateCriteria(typeof(Comment))
77+
.Add(Subqueries.PropertyIn("Post.Id", subquery))
78+
.List().Count;
79+
Assert.That(numberOfComments, Is.EqualTo(2), "Query with sub-query returned an invalid number of rows.");
80+
81+
var site = session.Get<Site>(1);
82+
site.Name = "Site 3";
83+
84+
subquery = DetachedCriteria.For(typeof(Post))
85+
.SetProjection(Projections.Id())
86+
.CreateCriteria("Site")
87+
.Add(Restrictions.Eq("Name", "Site 3"));
88+
numberOfComments =
89+
session.CreateCriteria(typeof(Comment))
90+
.Add(Subqueries.PropertyIn("Post.Id", subquery))
91+
.List().Count;
92+
93+
Assert.That(numberOfComments, Is.EqualTo(2), "Query with sub-query returned an invalid number of rows.");
94+
95+
96+
transaction.Rollback();
97+
98+
}
99+
100+
}
101+
}
102+
103+
104+
}
105+
106+
107+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,8 @@
737737
</Compile>
738738
<Compile Include="NHSpecificTest\NH3202\Domain.cs" />
739739
<Compile Include="NHSpecificTest\NH3202\Fixture.cs" />
740+
<Compile Include="NHSpecificTest\NH3567\DomainClass.cs" />
741+
<Compile Include="NHSpecificTest\NH3567\NH3567Tests.cs" />
740742
<Compile Include="NHSpecificTest\NH646\Domain.cs" />
741743
<Compile Include="NHSpecificTest\NH646\Fixture.cs" />
742744
<Compile Include="NHSpecificTest\Futures\LinqToFutureValueFixture.cs" />
@@ -2971,6 +2973,7 @@
29712973
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
29722974
</ItemGroup>
29732975
<ItemGroup>
2976+
<EmbeddedResource Include="NHSpecificTest\NH3567\Mappings.hbm.xml" />
29742977
<EmbeddedResource Include="NHSpecificTest\NH2692\Mappings.hbm.xml">
29752978
<SubType>Designer</SubType>
29762979
</EmbeddedResource>

src/NHibernate/Loader/Criteria/CriteriaQueryTranslator.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class CriteriaQueryTranslator : ICriteriaQuery
4444

4545
private readonly ICollection<IParameterSpecification> collectedParameterSpecifications;
4646
private readonly ICollection<NamedParameter> namedParameters;
47+
private readonly ISet<string> subQuerySpaces = new HashSet<string>();
4748

4849
public CriteriaQueryTranslator(ISessionFactoryImplementor factory, CriteriaImpl criteria, string rootEntityName,
4950
string rootSQLAlias, ICriteriaQuery outerQuery)
@@ -71,6 +72,7 @@ public CriteriaQueryTranslator(ISessionFactoryImplementor factory, CriteriaImpl
7172
CreateCriteriaEntityNameMap();
7273
CreateCriteriaCollectionPersisters();
7374
CreateCriteriaSQLAliasMap();
75+
CreateSubQuerySpaces();
7476
}
7577

7678
[CLSCompliant(false)] // TODO: Why does this cause a problem in 1.1
@@ -92,6 +94,9 @@ public ISet<string> GetQuerySpaces()
9294
{
9395
result.UnionWith(collectionPersister.CollectionSpaces);
9496
}
97+
98+
result.UnionWith(subQuerySpaces);
99+
95100
return result;
96101
}
97102

@@ -838,5 +843,37 @@ public string[] GetColumnAliasesUsingProjection(ICriteria subcriteria, string pr
838843
}
839844

840845
#endregion
846+
847+
private void CreateSubQuerySpaces()
848+
{
849+
850+
var subQueries = new List<CriteriaImpl>();
851+
GetSubQueries(rootCriteria, subQueries);
852+
853+
foreach (var criteriaImpl in subQueries)
854+
{
855+
//The RootSqlAlias is not relevant, since we're only retreiving the query spaces
856+
var translator = new CriteriaQueryTranslator(sessionFactory, criteriaImpl, criteriaImpl.EntityOrClassName, RootSqlAlias);
857+
subQuerySpaces.UnionWith(translator.GetQuerySpaces());
858+
}
859+
860+
}
861+
862+
private void GetSubQueries(CriteriaImpl criteriaImpl, List<CriteriaImpl> subQueries)
863+
{
864+
var subQueryExpressions =
865+
criteriaImpl.IterateExpressionEntries().Select(x => x.Criterion).OfType<SubqueryExpression>().ToList();
866+
867+
foreach (var subqueryExpression in subQueryExpressions)
868+
{
869+
var impl = subqueryExpression.Criteria as CriteriaImpl;
870+
if (impl != null)
871+
{
872+
subQueries.Add(impl);
873+
GetSubQueries(impl, subQueries);
874+
}
875+
}
876+
877+
}
841878
}
842879
}

0 commit comments

Comments
 (0)