Skip to content

Commit 4e019f9

Browse files
committed
Merge branch 'gliljas-NH-3567'
2 parents 9bf8ceb + 28e2c01 commit 4e019f9

File tree

5 files changed

+194
-1
lines changed

5 files changed

+194
-1
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
@@ -722,6 +722,8 @@
722722
<Compile Include="NHSpecificTest\NH3372\Fixture.cs" />
723723
<Compile Include="NHSpecificTest\NH3487\Entity.cs" />
724724
<Compile Include="NHSpecificTest\NH3487\Fixture.cs" />
725+
<Compile Include="NHSpecificTest\NH3567\DomainClass.cs" />
726+
<Compile Include="NHSpecificTest\NH3567\NH3567Tests.cs" />
725727
<Compile Include="NHSpecificTest\NH3570\BiFixture.cs" />
726728
<Compile Include="NHSpecificTest\NH3570\Model.cs" />
727729
<Compile Include="NHSpecificTest\NH3570\UniFixture.cs" />
@@ -3115,6 +3117,7 @@
31153117
<ItemGroup>
31163118
<EmbeddedResource Include="LazyComponentTest\Person.hbm.xml" />
31173119
<Content Include="NHSpecificTest\NH3372\Mappings.hbm.xml" />
3120+
<EmbeddedResource Include="NHSpecificTest\NH3567\Mappings.hbm.xml" />
31183121
<EmbeddedResource Include="NHSpecificTest\NH3570\Mappings.hbm.xml" />
31193122
<EmbeddedResource Include="NHSpecificTest\NH3455\Mappings.hbm.xml" />
31203123
<EmbeddedResource Include="NHSpecificTest\NH3590\Mappings.hbm.xml" />

src/NHibernate/Loader/Criteria/CriteriaQueryTranslator.cs

Lines changed: 27 additions & 1 deletion
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
@@ -92,6 +96,9 @@ public ISet<string> GetQuerySpaces()
9296
{
9397
result.UnionWith(collectionPersister.CollectionSpaces);
9498
}
99+
100+
result.UnionWith(subQuerySpaces);
101+
95102
return result;
96103
}
97104

@@ -844,5 +851,24 @@ public string[] GetColumnAliasesUsingProjection(ICriteria subcriteria, string pr
844851
}
845852

846853
#endregion
854+
855+
private void CreateSubQuerySpaces()
856+
{
857+
858+
var subQueries =
859+
rootCriteria.IterateExpressionEntries()
860+
.Select(x => x.Criterion)
861+
.OfType<SubqueryExpression>()
862+
.Select(x => x.Criteria)
863+
.OfType<CriteriaImpl>();
864+
865+
foreach (var criteriaImpl in subQueries)
866+
{
867+
//The RootSqlAlias is not relevant, since we're only retreiving the query spaces
868+
var translator = new CriteriaQueryTranslator(sessionFactory, criteriaImpl, criteriaImpl.EntityOrClassName, RootSqlAlias);
869+
subQuerySpaces.UnionWith(translator.GetQuerySpaces());
870+
}
871+
872+
}
847873
}
848-
}
874+
}

0 commit comments

Comments
 (0)