Skip to content

Commit 9a5a50e

Browse files
authored
Throw for Criteria over not mapped entity (#2487)
Fixes #1095
1 parent 765e1ed commit 9a5a50e

File tree

14 files changed

+141
-196
lines changed

14 files changed

+141
-196
lines changed

src/NHibernate.Test/Async/Criteria/CriteriaQueryTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,6 +2441,22 @@ public async Task SubcriteriaJoinTypesAsync()
24412441
session.Close();
24422442
}
24432443

2444+
public class NotMappedEntity
2445+
{
2446+
public virtual int Id { get; set; }
2447+
public virtual string Name { get; set; }
2448+
}
2449+
2450+
[Test]
2451+
public void CriteriaOnNotMappedEntityAsync()
2452+
{
2453+
using (ISession session = OpenSession())
2454+
{
2455+
Assert.ThrowsAsync<QueryException>(
2456+
() => session.CreateCriteria(typeof(NotMappedEntity)).ListAsync());
2457+
}
2458+
}
2459+
24442460
[Test]
24452461
public void TypeMismatchAsync()
24462462
{

src/NHibernate.Test/Async/Events/Collections/AbstractCollectionEventFixture.cs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//------------------------------------------------------------------------------
99

1010

11+
using System;
1112
using System.Collections;
1213
using System.Collections.Generic;
1314
using NHibernate.Collection;
@@ -38,30 +39,12 @@ protected override string MappingsAssembly
3839

3940
protected override void OnTearDown()
4041
{
41-
IParentWithCollection dummyParent = CreateParent("dummyParent");
42-
dummyParent.NewChildren(CreateCollection());
43-
IChild dummyChild = dummyParent.AddChild("dummyChild");
44-
45-
using (ISession s = OpenSession())
42+
using (var s = OpenSession())
43+
using (var tx = s.BeginTransaction())
4644
{
47-
using (ITransaction tx = s.BeginTransaction())
48-
{
49-
IList children = s.CreateCriteria(dummyChild.GetType()).List();
50-
IList parents = s.CreateCriteria(dummyParent.GetType()).List();
51-
foreach (IParentWithCollection parent in parents)
52-
{
53-
parent.ClearChildren();
54-
s.Delete(parent);
55-
}
56-
foreach (IChild child in children)
57-
{
58-
s.Delete(child);
59-
}
60-
61-
tx.Commit();
62-
}
45+
s.Delete("from System.Object");
46+
tx.Commit();
6347
}
64-
base.OnTearDown();
6548
}
6649

6750
[Test]

src/NHibernate.Test/Async/NHSpecificTest/NH1159/Fixture.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ public async Task DoesNotFlushWithCriteriaWithCommitAsync()
5959

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

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

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

@@ -89,9 +89,9 @@ public async Task DoesNotFlushWithCriteriaWithNeverAsync()
8989

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

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

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

@@ -120,9 +120,9 @@ public async Task DoesNotFlushWithCriteriaWithAutoAsync()
120120

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

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

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

src/NHibernate.Test/Async/NHSpecificTest/NH1688/Fixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected override void OnTearDown()
6868
{
6969
using (ISession session = OpenSession())
7070
{
71-
DetachedCriteria criteria = DetachedCriteria.For<NH1679.DomainClass>("alias");
71+
DetachedCriteria criteria = DetachedCriteria.For<DomainClass>("alias");
7272

7373
action.Invoke(criteria);
7474

@@ -77,4 +77,4 @@ protected override void OnTearDown()
7777
}
7878
}
7979
}
80-
}
80+
}

src/NHibernate.Test/Async/TransactionTest/TransactionNotificationFixture.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using System;
1212
using System.Data;
1313
using System.Data.Common;
14+
using NHibernate.Cfg;
15+
using NHibernate.Mapping.ByCode;
1416
using NHibernate.Transaction;
1517
using NUnit.Framework;
1618

@@ -21,7 +23,27 @@ namespace NHibernate.Test.TransactionTest
2123
[TestFixture]
2224
public class TransactionNotificationFixtureAsync : TestCase
2325
{
24-
protected override string[] Mappings => Array.Empty<string>();
26+
public class Entity
27+
{
28+
public virtual int Id { get; set; }
29+
public virtual string Name { get; set; }
30+
}
31+
32+
protected override string[] Mappings => null;
33+
34+
protected override void AddMappings(Configuration configuration)
35+
{
36+
var modelMapper = new ModelMapper();
37+
modelMapper.Class<Entity>(
38+
x =>
39+
{
40+
x.Id(e => e.Id);
41+
x.Property(e => e.Name);
42+
x.Table(nameof(Entity));
43+
});
44+
45+
configuration.AddMapping(modelMapper.CompileMappingForAllExplicitlyAddedEntities());
46+
}
2547

2648
[Test]
2749
public async Task CommitAsync()

src/NHibernate.Test/Criteria/CriteriaQueryTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,6 +2538,22 @@ public void SubcriteriaJoinTypes()
25382538
session.Close();
25392539
}
25402540

2541+
public class NotMappedEntity
2542+
{
2543+
public virtual int Id { get; set; }
2544+
public virtual string Name { get; set; }
2545+
}
2546+
2547+
[Test]
2548+
public void CriteriaOnNotMappedEntity()
2549+
{
2550+
using (ISession session = OpenSession())
2551+
{
2552+
Assert.Throws<QueryException>(
2553+
() => session.CreateCriteria(typeof(NotMappedEntity)).List());
2554+
}
2555+
}
2556+
25412557
[Test]
25422558
public void TypeMismatch()
25432559
{

src/NHibernate.Test/Events/Collections/AbstractCollectionEventFixture.cs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections;
23
using System.Collections.Generic;
34
using NHibernate.Collection;
@@ -26,30 +27,12 @@ protected override string MappingsAssembly
2627

2728
protected override void OnTearDown()
2829
{
29-
IParentWithCollection dummyParent = CreateParent("dummyParent");
30-
dummyParent.NewChildren(CreateCollection());
31-
IChild dummyChild = dummyParent.AddChild("dummyChild");
32-
33-
using (ISession s = OpenSession())
30+
using (var s = OpenSession())
31+
using (var tx = s.BeginTransaction())
3432
{
35-
using (ITransaction tx = s.BeginTransaction())
36-
{
37-
IList children = s.CreateCriteria(dummyChild.GetType()).List();
38-
IList parents = s.CreateCriteria(dummyParent.GetType()).List();
39-
foreach (IParentWithCollection parent in parents)
40-
{
41-
parent.ClearChildren();
42-
s.Delete(parent);
43-
}
44-
foreach (IChild child in children)
45-
{
46-
s.Delete(child);
47-
}
48-
49-
tx.Commit();
50-
}
33+
s.Delete("from System.Object");
34+
tx.Commit();
5135
}
52-
base.OnTearDown();
5336
}
5437

5538
[Test]

src/NHibernate.Test/NHSpecificTest/NH1159/ContactTitle.cs

Lines changed: 0 additions & 129 deletions
This file was deleted.

src/NHibernate.Test/NHSpecificTest/NH1159/Fixture.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public void DoesNotFlushWithCriteriaWithCommit()
4848

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

51-
ICriteria query = session.CreateCriteria(typeof(ContactTitle));
51+
ICriteria query = session.CreateCriteria(typeof(Contact));
5252
query.Add(Expression.Eq("Id", (Int64)1));
53-
query.UniqueResult<ContactTitle>();
53+
query.UniqueResult<Contact>();
5454

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

@@ -78,9 +78,9 @@ public void DoesNotFlushWithCriteriaWithNever()
7878

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

81-
ICriteria query = session.CreateCriteria(typeof(ContactTitle));
81+
ICriteria query = session.CreateCriteria(typeof(Contact));
8282
query.Add(Expression.Eq("Id", (Int64)1));
83-
query.UniqueResult<ContactTitle>();
83+
query.UniqueResult<Contact>();
8484

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

@@ -109,9 +109,9 @@ public void DoesNotFlushWithCriteriaWithAuto()
109109

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

112-
ICriteria query = session.CreateCriteria(typeof(ContactTitle));
112+
ICriteria query = session.CreateCriteria(typeof(Contact));
113113
query.Add(Expression.Eq("Id", (Int64)1));
114-
query.UniqueResult<ContactTitle>();
114+
query.UniqueResult<Contact>();
115115

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

0 commit comments

Comments
 (0)