Skip to content

Unify handling of composite values in hql and Criteria #2159

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 8 commits into from
Sep 11, 2019
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
105 changes: 105 additions & 0 deletions src/NHibernate.Test/Async/CompositeId/ClassWithCompositeIdFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,110 @@ public async Task HqlAsync()
Assert.AreEqual(1, results.Count);
}
}

[Test]
public async Task HqlInClauseAsync()
{
//Need to port changes from InLogicOperatorNode.mutateRowValueConstructorSyntaxInInListSyntax
if (!Dialect.SupportsRowValueConstructorSyntaxInInList)
Assert.Ignore();

// insert the new objects
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
await (s.SaveAsync(new ClassWithCompositeId(id) {OneProperty = 5}));
await (s.SaveAsync(new ClassWithCompositeId(secondId) {OneProperty = 10}));
await (s.SaveAsync(new ClassWithCompositeId(new Id(id.KeyString, id.GetKeyShort(), secondId.KeyDateTime))));

await (t.CommitAsync());
}

using (var s = OpenSession())
{
var results = await (s.CreateQuery("from ClassWithCompositeId x where x.Id in (:id1, :id2)")
.SetParameter("id1", id)
.SetParameter("id2", secondId)
.ListAsync<ClassWithCompositeId>());
Assert.That(results.Count, Is.EqualTo(2));
}
}

[Test]
public async Task QueryOverInClauseSubqueryAsync()
{
if (!TestDialect.SupportsRowValueConstructorSyntax)
{
Assert.Ignore();
}

// insert the new objects
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
await (s.SaveAsync(new ClassWithCompositeId(id) {OneProperty = 5}));
await (s.SaveAsync(new ClassWithCompositeId(secondId) {OneProperty = 10}));
await (s.SaveAsync(new ClassWithCompositeId(new Id(id.KeyString, id.GetKeyShort(), secondId.KeyDateTime))));

await (t.CommitAsync());
}

using (var s = OpenSession())
{
var results = await (s.QueryOver<ClassWithCompositeId>().WithSubquery.WhereProperty(p => p.Id).In(QueryOver.Of<ClassWithCompositeId>().Where(p => p.Id.KeyString == id.KeyString).Select(p => p.Id)).ListAsync());
Assert.That(results.Count, Is.EqualTo(2));
}
}

[Test]
public async Task HqlInClauseSubqueryAsync()
{
if (!TestDialect.SupportsRowValueConstructorSyntax)
Assert.Ignore();

// insert the new objects
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
await (s.SaveAsync(new ClassWithCompositeId(id) {OneProperty = 5}));
await (s.SaveAsync(new ClassWithCompositeId(secondId) {OneProperty = 10}));
await (s.SaveAsync(new ClassWithCompositeId(new Id(id.KeyString, id.GetKeyShort(), secondId.KeyDateTime))));

await (t.CommitAsync());
}

using (var s = OpenSession())
{
var results = await (s.CreateQuery("from ClassWithCompositeId x where x.Id in (select s.Id from ClassWithCompositeId s where s.Id.KeyString = :keyString)")
.SetParameter("keyString", id.KeyString).ListAsync());
Assert.That(results.Count, Is.EqualTo(2));
}
}

//GH-1376
[Test]
public async Task HqlInClauseSubquery_ForEntityAsync()
{
if (!TestDialect.SupportsRowValueConstructorSyntax)
Assert.Ignore();

// insert the new objects
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
await (s.SaveAsync(new ClassWithCompositeId(id) {OneProperty = 5}));
await (s.SaveAsync(new ClassWithCompositeId(secondId) {OneProperty = 10}));
await (s.SaveAsync(new ClassWithCompositeId(new Id(id.KeyString, id.GetKeyShort(), secondId.KeyDateTime))));

await (t.CommitAsync());
}

using (var s = OpenSession())
{
var results = await (s.CreateQuery("from ClassWithCompositeId x where x in (select s from ClassWithCompositeId s where s.Id.KeyString = :keyString)")
.SetParameter("keyString", id.KeyString).ListAsync());
Assert.That(results.Count, Is.EqualTo(2));
}
}
}
}
116 changes: 25 additions & 91 deletions src/NHibernate.Test/Async/Criteria/CriteriaQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,122 +288,56 @@ public async Task SubselectWithComponentAsync()
await (t.CommitAsync());
}

if (TestDialect.SupportsOperatorAll)
//Note: It might require separate test dialect flag like SupportsRowValueConstructorWithOperatorAll
if (TestDialect.SupportsOperatorAll && TestDialect.SupportsRowValueConstructorSyntax)
{
using (ISession session = OpenSession())
using (ITransaction t = session.BeginTransaction())
{
try
{
await (session.CreateCriteria<Student>()
await (session.CreateCriteria<Student>()
.Add(Subqueries.PropertyEqAll("CityState", dc))
.ListAsync());

Assert.Fail("should have failed because cannot compare subquery results with multiple columns");
}
catch (QueryException)
{
// expected
}
await (t.RollbackAsync());
}
}

if (TestDialect.SupportsOperatorAll)
{
using (ISession session = OpenSession())
using (ITransaction t = session.BeginTransaction())
{
try
{
await (session.CreateCriteria<Student>()
await (session.CreateCriteria<Student>()
.Add(Property.ForName("CityState").EqAll(dc))
.ListAsync());

Assert.Fail("should have failed because cannot compare subquery results with multiple columns");
}
catch (QueryException)
{
// expected
}
finally
{
await (t.RollbackAsync());
}
}
}

using (ISession session = OpenSession())
using (ITransaction t = session.BeginTransaction())
if (TestDialect.SupportsRowValueConstructorSyntax)
{
try
using (ISession session = OpenSession())
{
await (session.CreateCriteria<Student>()
.Add(Subqueries.In(odessaWa, dc))
.ListAsync());

Assert.Fail("should have failed because cannot compare subquery results with multiple columns");
}
catch (NHibernate.Exceptions.GenericADOException)
{
// expected
}
finally
{
await (t.RollbackAsync());
.Add(Subqueries.In(odessaWa, dc))
.ListAsync());
}
}

using (ISession session = OpenSession())
using (ITransaction t = session.BeginTransaction())
{
DetachedCriteria dc2 = DetachedCriteria.For<Student>("st1")
.Add(Property.ForName("st1.CityState").EqProperty("st2.CityState"))
.SetProjection(Property.ForName("CityState"));

try

using (ISession session = OpenSession())
{
DetachedCriteria dc2 = DetachedCriteria.For<Student>("st1")
.Add(Property.ForName("st1.CityState").EqProperty("st2.CityState"))
.SetProjection(Property.ForName("CityState"));
await (session.CreateCriteria<Student>("st2")
.Add( Subqueries.Eq(odessaWa, dc2))
.ListAsync());
Assert.Fail("should have failed because cannot compare subquery results with multiple columns");
}
catch (NHibernate.Exceptions.GenericADOException)
{
// expected
}
finally
{
await (t.RollbackAsync());
.Add( Subqueries.Eq(odessaWa, dc2))
.ListAsync());
}
}

using (ISession session = OpenSession())
using (ITransaction t = session.BeginTransaction())
{
DetachedCriteria dc3 = DetachedCriteria.For<Student>("st")
.CreateCriteria("Enrolments")
.CreateCriteria("Course")
.Add(Property.ForName("Description").Eq("Hibernate Training"))
.SetProjection(Property.ForName("st.CityState"));
try

using (ISession session = OpenSession())
{
DetachedCriteria dc3 = DetachedCriteria.For<Student>("st")
.CreateCriteria("Enrolments")
.CreateCriteria("Course")
.Add(Property.ForName("Description").Eq("Hibernate Training"))
.SetProjection(Property.ForName("st.CityState"));
await (session.CreateCriteria<Enrolment>("e")
.Add(Subqueries.Eq(odessaWa, dc3))
.ListAsync());

Assert.Fail("should have failed because cannot compare subquery results with multiple columns");
}
catch (NHibernate.Exceptions.GenericADOException)
{
// expected
}
finally
{
await (t.RollbackAsync());
.Add(Subqueries.Eq(odessaWa, dc3))
.ListAsync());
}
}

using (ISession session = OpenSession())
using (ITransaction t = session.BeginTransaction())
{
Expand Down
105 changes: 105 additions & 0 deletions src/NHibernate.Test/CompositeId/ClassWithCompositeIdFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,5 +228,110 @@ public void Hql()
Assert.AreEqual(1, results.Count);
}
}

[Test]
public void HqlInClause()
{
//Need to port changes from InLogicOperatorNode.mutateRowValueConstructorSyntaxInInListSyntax
if (!Dialect.SupportsRowValueConstructorSyntaxInInList)
Assert.Ignore();

// insert the new objects
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
s.Save(new ClassWithCompositeId(id) {OneProperty = 5});
s.Save(new ClassWithCompositeId(secondId) {OneProperty = 10});
s.Save(new ClassWithCompositeId(new Id(id.KeyString, id.GetKeyShort(), secondId.KeyDateTime)));

t.Commit();
}

using (var s = OpenSession())
{
var results = s.CreateQuery("from ClassWithCompositeId x where x.Id in (:id1, :id2)")
.SetParameter("id1", id)
.SetParameter("id2", secondId)
.List<ClassWithCompositeId>();
Assert.That(results.Count, Is.EqualTo(2));
}
}

[Test]
public void QueryOverInClauseSubquery()
{
if (!TestDialect.SupportsRowValueConstructorSyntax)
{
Assert.Ignore();
}

// insert the new objects
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
s.Save(new ClassWithCompositeId(id) {OneProperty = 5});
s.Save(new ClassWithCompositeId(secondId) {OneProperty = 10});
s.Save(new ClassWithCompositeId(new Id(id.KeyString, id.GetKeyShort(), secondId.KeyDateTime)));

t.Commit();
}

using (var s = OpenSession())
{
var results = s.QueryOver<ClassWithCompositeId>().WithSubquery.WhereProperty(p => p.Id).In(QueryOver.Of<ClassWithCompositeId>().Where(p => p.Id.KeyString == id.KeyString).Select(p => p.Id)).List();
Assert.That(results.Count, Is.EqualTo(2));
}
}

[Test]
public void HqlInClauseSubquery()
{
if (!TestDialect.SupportsRowValueConstructorSyntax)
Assert.Ignore();

// insert the new objects
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
s.Save(new ClassWithCompositeId(id) {OneProperty = 5});
s.Save(new ClassWithCompositeId(secondId) {OneProperty = 10});
s.Save(new ClassWithCompositeId(new Id(id.KeyString, id.GetKeyShort(), secondId.KeyDateTime)));

t.Commit();
}

using (var s = OpenSession())
{
var results = s.CreateQuery("from ClassWithCompositeId x where x.Id in (select s.Id from ClassWithCompositeId s where s.Id.KeyString = :keyString)")
.SetParameter("keyString", id.KeyString).List();
Assert.That(results.Count, Is.EqualTo(2));
}
}

//GH-1376
[Test]
public void HqlInClauseSubquery_ForEntity()
{
if (!TestDialect.SupportsRowValueConstructorSyntax)
Assert.Ignore();

// insert the new objects
using (ISession s = OpenSession())
using (ITransaction t = s.BeginTransaction())
{
s.Save(new ClassWithCompositeId(id) {OneProperty = 5});
s.Save(new ClassWithCompositeId(secondId) {OneProperty = 10});
s.Save(new ClassWithCompositeId(new Id(id.KeyString, id.GetKeyShort(), secondId.KeyDateTime)));

t.Commit();
}

using (var s = OpenSession())
{
var results = s.CreateQuery("from ClassWithCompositeId x where x in (select s from ClassWithCompositeId s where s.Id.KeyString = :keyString)")
.SetParameter("keyString", id.KeyString).List();
Assert.That(results.Count, Is.EqualTo(2));
}
}
}
}
4 changes: 3 additions & 1 deletion src/NHibernate.Test/CompositeId/Id.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public string KeyString
set { _keyString = value; }
}

//KeyShort is commented to test access attributes; So expose it as method instead
public short GetKeyShort() => _keyShort;
// public short KeyShort {
// get { return _keyShort;}
// set {_keyShort = value;}
Expand Down Expand Up @@ -55,4 +57,4 @@ public override bool Equals(object obj)
return false;
}
}
}
}
Loading