Skip to content

NH-3567 - Unit test (from JIRA reporter) and fix #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3567/DomainClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@


namespace NHibernate.Test.NHSpecificTest.NH3567
{
public class Site
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }


}
public class Post
{
public virtual int Id { get; set; }
public virtual string Content { get; set; }
public virtual Site Site { get; set; }


}

public class Comment
{
public virtual int Id { get; set; }
public virtual string Content { get; set; }
public virtual Post Post { get; set; }

}

}
28 changes: 28 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3567/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.NH3567" >

<class name="Site" >
<id name="Id" type="Int32">
<generator class="assigned" />
</id>
<property name="Name" length="2000" />
</class>

<class name="Post" >
<id name="Id" type="Int32">
<generator class="assigned" />
</id>
<property name="Content" length="2000" />
<many-to-one name="Site" column="PostId" not-null="true"/>
</class>

<class name="Comment" table="Cmt">
<id name="Id" type="Int32">
<generator class="assigned" />
</id>
<property name="Content" length="2000" />

<many-to-one name="Post" column="PostId" not-null="true"/>
</class>
</hibernate-mapping>
107 changes: 107 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3567/NH3567Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using NUnit.Framework;
using NHibernate.Criterion;
using NHibernate.Dialect;

namespace NHibernate.Test.NHSpecificTest.NH3567
{
[TestFixture]
public class NH3567Tests : BugTestCase
{
protected override void OnSetUp()
{
base.OnSetUp();
using (ISession session = this.OpenSession())
{
session.BeginTransaction();
var id = 0;

var site1 = new Site { Id = ++id, Name = "Site 1" };
var site2 = new Site { Id = ++id, Name = "Site 1" };
session.Save(site1);
session.Save(site2);


var p1 = new Post { Id = ++id, Content = "Post 1", Site = site1 };
var p2 = new Post { Id = ++id, Content = "Post 2", Site = site2 };

session.Save(p1);
session.Save(p2);

session.Save(new Comment { Id = ++id, Content = "Comment 1.1", Post = p1 });
session.Save(new Comment { Id = ++id, Content = "Comment 1.2", Post = p1 });
session.Save(new Comment { Id = ++id, Content = "Comment 2.1", Post = p2 });
session.Save(new Comment { Id = ++id, Content = "Comment 2.2", Post = p2 });
session.Flush();
session.Transaction.Commit();
}
}

protected override void OnTearDown()
{
base.OnTearDown();
using (ISession session = this.OpenSession())
{
session.Delete("from Comment");
session.Delete("from Post");
session.Delete("from Site");
session.Flush();
}
}

protected override bool AppliesTo(NHibernate.Dialect.Dialect dialect)
{
return dialect as MsSql2005Dialect != null;
}

[Test]
public void TestFlushModeAuto()
{
using (ISession session = this.OpenSession())
{
session.FlushMode = FlushMode.Auto;
using (var transaction = session.BeginTransaction())
{
var post = session.QueryOver<Post>().Where(x => x.Content == "Post 1").SingleOrDefault();

post.Content = "1";

var comments = session.QueryOver<Comment>().JoinQueryOver(x => x.Post).Where(x => x.Content == "1").List();
Assert.That(comments.Count, Is.EqualTo(2), "Query over returned something different than 2");

post.Content = "I";
var subquery = DetachedCriteria.For(typeof(Post))
.Add(Restrictions.Eq("Content", "I"))
.SetProjection(Projections.Id());
var numberOfComments =
session.CreateCriteria(typeof(Comment))
.Add(Subqueries.PropertyIn("Post.Id", subquery))
.List().Count;
Assert.That(numberOfComments, Is.EqualTo(2), "Query with sub-query returned an invalid number of rows.");

var site = session.Get<Site>(1);
site.Name = "Site 3";

subquery = DetachedCriteria.For(typeof(Post))
.SetProjection(Projections.Id())
.CreateCriteria("Site")
.Add(Restrictions.Eq("Name", "Site 3"));
numberOfComments =
session.CreateCriteria(typeof(Comment))
.Add(Subqueries.PropertyIn("Post.Id", subquery))
.List().Count;

Assert.That(numberOfComments, Is.EqualTo(2), "Query with sub-query returned an invalid number of rows.");


transaction.Rollback();

}

}
}


}


}
3 changes: 3 additions & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,8 @@
<Compile Include="Component\Basic\ComponentWithUniqueConstraintTests.cs" />
<Compile Include="NHSpecificTest\NH3487\Entity.cs" />
<Compile Include="NHSpecificTest\NH3487\Fixture.cs" />
<Compile Include="NHSpecificTest\NH3567\DomainClass.cs" />
<Compile Include="NHSpecificTest\NH3567\NH3567Tests.cs" />
<Compile Include="NHSpecificTest\NH3570\BiFixture.cs" />
<Compile Include="NHSpecificTest\NH3570\Model.cs" />
<Compile Include="NHSpecificTest\NH3570\UniFixture.cs" />
Expand Down Expand Up @@ -3068,6 +3070,7 @@
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="NHSpecificTest\NH3567\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3570\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3455\Mappings.hbm.xml" />
<EmbeddedResource Include="NHSpecificTest\NH3590\Mappings.hbm.xml" />
Expand Down
28 changes: 27 additions & 1 deletion src/NHibernate/Loader/Criteria/CriteriaQueryTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public class CriteriaQueryTranslator : ICriteriaQuery

private readonly ICollection<IParameterSpecification> collectedParameterSpecifications;
private readonly ICollection<NamedParameter> namedParameters;
private readonly ISet<string> subQuerySpaces = new HashSet<string>();



public CriteriaQueryTranslator(ISessionFactoryImplementor factory, CriteriaImpl criteria, string rootEntityName,
string rootSQLAlias, ICriteriaQuery outerQuery)
Expand Down Expand Up @@ -71,6 +74,7 @@ public CriteriaQueryTranslator(ISessionFactoryImplementor factory, CriteriaImpl
CreateCriteriaEntityNameMap();
CreateCriteriaCollectionPersisters();
CreateCriteriaSQLAliasMap();
CreateSubQuerySpaces();
}

[CLSCompliant(false)] // TODO: Why does this cause a problem in 1.1
Expand All @@ -92,6 +96,9 @@ public ISet<string> GetQuerySpaces()
{
result.UnionWith(collectionPersister.CollectionSpaces);
}

result.UnionWith(subQuerySpaces);

return result;
}

Expand Down Expand Up @@ -844,5 +851,24 @@ public string[] GetColumnAliasesUsingProjection(ICriteria subcriteria, string pr
}

#endregion

private void CreateSubQuerySpaces()
{

var subQueries =
rootCriteria.IterateExpressionEntries()
.Select(x => x.Criterion)
.OfType<SubqueryExpression>()
.Select(x => x.Criteria)
.OfType<CriteriaImpl>();

foreach (var criteriaImpl in subQueries)
{
//The RootSqlAlias is not relevant, since we're only retreiving the query spaces
var translator = new CriteriaQueryTranslator(sessionFactory, criteriaImpl, criteriaImpl.EntityOrClassName, RootSqlAlias);
subQuerySpaces.UnionWith(translator.GetQuerySpaces());
}

}
}
}
}