Skip to content

Commit

Permalink
Throw for Criteria over not mapped entity (#2487)
Browse files Browse the repository at this point in the history
Fixes #1095
  • Loading branch information
bahusoid authored Sep 7, 2020
1 parent 765e1ed commit 9a5a50e
Show file tree
Hide file tree
Showing 14 changed files with 141 additions and 196 deletions.
16 changes: 16 additions & 0 deletions src/NHibernate.Test/Async/Criteria/CriteriaQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2441,6 +2441,22 @@ public async Task SubcriteriaJoinTypesAsync()
session.Close();
}

public class NotMappedEntity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}

[Test]
public void CriteriaOnNotMappedEntityAsync()
{
using (ISession session = OpenSession())
{
Assert.ThrowsAsync<QueryException>(
() => session.CreateCriteria(typeof(NotMappedEntity)).ListAsync());
}
}

[Test]
public void TypeMismatchAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//------------------------------------------------------------------------------


using System;
using System.Collections;
using System.Collections.Generic;
using NHibernate.Collection;
Expand Down Expand Up @@ -38,30 +39,12 @@ protected override string MappingsAssembly

protected override void OnTearDown()
{
IParentWithCollection dummyParent = CreateParent("dummyParent");
dummyParent.NewChildren(CreateCollection());
IChild dummyChild = dummyParent.AddChild("dummyChild");

using (ISession s = OpenSession())
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
using (ITransaction tx = s.BeginTransaction())
{
IList children = s.CreateCriteria(dummyChild.GetType()).List();
IList parents = s.CreateCriteria(dummyParent.GetType()).List();
foreach (IParentWithCollection parent in parents)
{
parent.ClearChildren();
s.Delete(parent);
}
foreach (IChild child in children)
{
s.Delete(child);
}

tx.Commit();
}
s.Delete("from System.Object");
tx.Commit();
}
base.OnTearDown();
}

[Test]
Expand Down
12 changes: 6 additions & 6 deletions src/NHibernate.Test/Async/NHSpecificTest/NH1159/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public async Task DoesNotFlushWithCriteriaWithCommitAsync()

Assert.That(HibernateInterceptor.CallCount, Is.EqualTo(1));

ICriteria query = session.CreateCriteria(typeof(ContactTitle));
ICriteria query = session.CreateCriteria(typeof(Contact));
query.Add(Expression.Eq("Id", (Int64)1));
await (query.UniqueResultAsync<ContactTitle>());
await (query.UniqueResultAsync<Contact>());

Assert.That(HibernateInterceptor.CallCount, Is.EqualTo(1));

Expand Down Expand Up @@ -89,9 +89,9 @@ public async Task DoesNotFlushWithCriteriaWithNeverAsync()

Assert.That(HibernateInterceptor.CallCount, Is.EqualTo(1));

ICriteria query = session.CreateCriteria(typeof(ContactTitle));
ICriteria query = session.CreateCriteria(typeof(Contact));
query.Add(Expression.Eq("Id", (Int64)1));
await (query.UniqueResultAsync<ContactTitle>());
await (query.UniqueResultAsync<Contact>());

Assert.That(HibernateInterceptor.CallCount, Is.EqualTo(1));

Expand Down Expand Up @@ -120,9 +120,9 @@ public async Task DoesNotFlushWithCriteriaWithAutoAsync()

Assert.That(HibernateInterceptor.CallCount, Is.EqualTo(1));

ICriteria query = session.CreateCriteria(typeof(ContactTitle));
ICriteria query = session.CreateCriteria(typeof(Contact));
query.Add(Expression.Eq("Id", (Int64)1));
await (query.UniqueResultAsync<ContactTitle>());
await (query.UniqueResultAsync<Contact>());

Assert.That(HibernateInterceptor.CallCount, Is.EqualTo(2));

Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate.Test/Async/NHSpecificTest/NH1688/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected override void OnTearDown()
{
using (ISession session = OpenSession())
{
DetachedCriteria criteria = DetachedCriteria.For<NH1679.DomainClass>("alias");
DetachedCriteria criteria = DetachedCriteria.For<DomainClass>("alias");

action.Invoke(criteria);

Expand All @@ -77,4 +77,4 @@ protected override void OnTearDown()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using System;
using System.Data;
using System.Data.Common;
using NHibernate.Cfg;
using NHibernate.Mapping.ByCode;
using NHibernate.Transaction;
using NUnit.Framework;

Expand All @@ -21,7 +23,27 @@ namespace NHibernate.Test.TransactionTest
[TestFixture]
public class TransactionNotificationFixtureAsync : TestCase
{
protected override string[] Mappings => Array.Empty<string>();
public class Entity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}

protected override string[] Mappings => null;

protected override void AddMappings(Configuration configuration)
{
var modelMapper = new ModelMapper();
modelMapper.Class<Entity>(
x =>
{
x.Id(e => e.Id);
x.Property(e => e.Name);
x.Table(nameof(Entity));
});

configuration.AddMapping(modelMapper.CompileMappingForAllExplicitlyAddedEntities());
}

[Test]
public async Task CommitAsync()
Expand Down
16 changes: 16 additions & 0 deletions src/NHibernate.Test/Criteria/CriteriaQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2538,6 +2538,22 @@ public void SubcriteriaJoinTypes()
session.Close();
}

public class NotMappedEntity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}

[Test]
public void CriteriaOnNotMappedEntity()
{
using (ISession session = OpenSession())
{
Assert.Throws<QueryException>(
() => session.CreateCriteria(typeof(NotMappedEntity)).List());
}
}

[Test]
public void TypeMismatch()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using NHibernate.Collection;
Expand Down Expand Up @@ -26,30 +27,12 @@ protected override string MappingsAssembly

protected override void OnTearDown()
{
IParentWithCollection dummyParent = CreateParent("dummyParent");
dummyParent.NewChildren(CreateCollection());
IChild dummyChild = dummyParent.AddChild("dummyChild");

using (ISession s = OpenSession())
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
using (ITransaction tx = s.BeginTransaction())
{
IList children = s.CreateCriteria(dummyChild.GetType()).List();
IList parents = s.CreateCriteria(dummyParent.GetType()).List();
foreach (IParentWithCollection parent in parents)
{
parent.ClearChildren();
s.Delete(parent);
}
foreach (IChild child in children)
{
s.Delete(child);
}

tx.Commit();
}
s.Delete("from System.Object");
tx.Commit();
}
base.OnTearDown();
}

[Test]
Expand Down
129 changes: 0 additions & 129 deletions src/NHibernate.Test/NHSpecificTest/NH1159/ContactTitle.cs

This file was deleted.

12 changes: 6 additions & 6 deletions src/NHibernate.Test/NHSpecificTest/NH1159/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public void DoesNotFlushWithCriteriaWithCommit()

Assert.That(HibernateInterceptor.CallCount, Is.EqualTo(1));

ICriteria query = session.CreateCriteria(typeof(ContactTitle));
ICriteria query = session.CreateCriteria(typeof(Contact));
query.Add(Expression.Eq("Id", (Int64)1));
query.UniqueResult<ContactTitle>();
query.UniqueResult<Contact>();

Assert.That(HibernateInterceptor.CallCount, Is.EqualTo(1));

Expand Down Expand Up @@ -78,9 +78,9 @@ public void DoesNotFlushWithCriteriaWithNever()

Assert.That(HibernateInterceptor.CallCount, Is.EqualTo(1));

ICriteria query = session.CreateCriteria(typeof(ContactTitle));
ICriteria query = session.CreateCriteria(typeof(Contact));
query.Add(Expression.Eq("Id", (Int64)1));
query.UniqueResult<ContactTitle>();
query.UniqueResult<Contact>();

Assert.That(HibernateInterceptor.CallCount, Is.EqualTo(1));

Expand Down Expand Up @@ -109,9 +109,9 @@ public void DoesNotFlushWithCriteriaWithAuto()

Assert.That(HibernateInterceptor.CallCount, Is.EqualTo(1));

ICriteria query = session.CreateCriteria(typeof(ContactTitle));
ICriteria query = session.CreateCriteria(typeof(Contact));
query.Add(Expression.Eq("Id", (Int64)1));
query.UniqueResult<ContactTitle>();
query.UniqueResult<Contact>();

Assert.That(HibernateInterceptor.CallCount, Is.EqualTo(2));

Expand Down
Loading

0 comments on commit 9a5a50e

Please sign in to comment.