Skip to content

Commit 9507776

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

File tree

5 files changed

+203
-0
lines changed

5 files changed

+203
-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
@@ -693,6 +693,8 @@
693693
<Compile Include="Component\Basic\ComponentWithUniqueConstraintTests.cs" />
694694
<Compile Include="NHSpecificTest\NH3487\Entity.cs" />
695695
<Compile Include="NHSpecificTest\NH3487\Fixture.cs" />
696+
<Compile Include="NHSpecificTest\NH3567\DomainClass.cs" />
697+
<Compile Include="NHSpecificTest\NH3567\NH3567Tests.cs" />
696698
<Compile Include="NHSpecificTest\NH3570\BiFixture.cs" />
697699
<Compile Include="NHSpecificTest\NH3570\Model.cs" />
698700
<Compile Include="NHSpecificTest\NH3570\UniFixture.cs" />
@@ -3068,6 +3070,7 @@
30683070
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
30693071
</ItemGroup>
30703072
<ItemGroup>
3073+
<EmbeddedResource Include="NHSpecificTest\NH3567\Mappings.hbm.xml" />
30713074
<EmbeddedResource Include="NHSpecificTest\NH3570\Mappings.hbm.xml" />
30723075
<EmbeddedResource Include="NHSpecificTest\NH3455\Mappings.hbm.xml" />
30733076
<EmbeddedResource Include="NHSpecificTest\NH3590\Mappings.hbm.xml" />

src/NHibernate/Loader/Criteria/CriteriaQueryTranslator.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ 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>();
48+
49+
4750

4851
public CriteriaQueryTranslator(ISessionFactoryImplementor factory, CriteriaImpl criteria, string rootEntityName,
4952
string rootSQLAlias, ICriteriaQuery outerQuery)
@@ -71,6 +74,7 @@ public CriteriaQueryTranslator(ISessionFactoryImplementor factory, CriteriaImpl
7174
CreateCriteriaEntityNameMap();
7275
CreateCriteriaCollectionPersisters();
7376
CreateCriteriaSQLAliasMap();
77+
CreateSubQuerySpaces();
7478
}
7579

7680
[CLSCompliant(false)] // TODO: Why does this cause a problem in 1.1
@@ -844,5 +848,37 @@ public string[] GetColumnAliasesUsingProjection(ICriteria subcriteria, string pr
844848
}
845849

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

0 commit comments

Comments
 (0)