Skip to content

Commit e313b07

Browse files
Cease having dynamic composite-id causing swallowed exceptions (#1938)
Fixes #1047 - NH-3865 Co-authored-by: Frédéric Delaporte <[email protected]>
1 parent 17ec8ee commit e313b07

File tree

4 files changed

+142
-4
lines changed

4 files changed

+142
-4
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Collections.Generic;
12+
using NUnit.Framework;
13+
14+
namespace NHibernate.Test.NHSpecificTest.NH3865
15+
{
16+
using System.Threading.Tasks;
17+
[TestFixture]
18+
public class FixtureAsync : BugTestCase
19+
{
20+
private const string _entityName = "MyEntity";
21+
22+
private IDictionary<string, object> _compId;
23+
24+
protected override void OnSetUp()
25+
{
26+
_compId = new Dictionary<string, object>
27+
{
28+
["IdPart1"] = 1,
29+
["IdPart2"] = 2
30+
};
31+
var entity = new Dictionary<string, object>
32+
{
33+
["CompId"] = _compId,
34+
["Name"] = "some name"
35+
};
36+
37+
using (var s = OpenSession())
38+
using (var tx = s.BeginTransaction())
39+
{
40+
s.Save(_entityName, entity);
41+
tx.Commit();
42+
}
43+
}
44+
45+
[Test]
46+
public async Task ReadDynamicEntityWithCompositeIdAsync()
47+
{
48+
using (var s = OpenSession())
49+
using (s.BeginTransaction())
50+
{
51+
var entity = (IDictionary<string, object>) await (s.GetAsync(_entityName, _compId));
52+
Assert.That(entity["Name"], Is.EqualTo("some name"));
53+
}
54+
}
55+
56+
protected override void OnTearDown()
57+
{
58+
using (var s = OpenSession())
59+
using (var tx = s.BeginTransaction())
60+
{
61+
s.Delete($"from MyEntity {_entityName}");
62+
tx.Commit();
63+
}
64+
}
65+
}
66+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Collections.Generic;
2+
using NUnit.Framework;
3+
4+
namespace NHibernate.Test.NHSpecificTest.NH3865
5+
{
6+
[TestFixture]
7+
public class Fixture : BugTestCase
8+
{
9+
private const string _entityName = "MyEntity";
10+
11+
private IDictionary<string, object> _compId;
12+
13+
protected override void OnSetUp()
14+
{
15+
_compId = new Dictionary<string, object>
16+
{
17+
["IdPart1"] = 1,
18+
["IdPart2"] = 2
19+
};
20+
var entity = new Dictionary<string, object>
21+
{
22+
["CompId"] = _compId,
23+
["Name"] = "some name"
24+
};
25+
26+
using (var s = OpenSession())
27+
using (var tx = s.BeginTransaction())
28+
{
29+
s.Save(_entityName, entity);
30+
tx.Commit();
31+
}
32+
}
33+
34+
[Test]
35+
public void ReadDynamicEntityWithCompositeId()
36+
{
37+
using (var s = OpenSession())
38+
using (s.BeginTransaction())
39+
{
40+
var entity = (IDictionary<string, object>) s.Get(_entityName, _compId);
41+
Assert.That(entity["Name"], Is.EqualTo("some name"));
42+
}
43+
}
44+
45+
protected override void OnTearDown()
46+
{
47+
using (var s = OpenSession())
48+
using (var tx = s.BeginTransaction())
49+
{
50+
s.Delete($"from MyEntity {_entityName}");
51+
tx.Commit();
52+
}
53+
}
54+
}
55+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.NHSpecificTest.NH3865">
5+
6+
<class entity-name="MyEntity">
7+
<composite-id name="CompId">
8+
<key-property name="IdPart1" type="Int32">
9+
<column name="Id1"/>
10+
</key-property>
11+
<key-property name="IdPart2" type="Int32">
12+
<column name="Id2"/>
13+
</key-property>
14+
</composite-id>
15+
<property name="Name" type="string"/>
16+
</class>
17+
18+
</hibernate-mapping>
19+

src/NHibernate/Mapping/Component.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,15 @@ public System.Type ComponentClass
133133
get
134134
{
135135
// NH Different implementation (we use reflection only when needed)
136-
if (componentClass == null)
136+
if (componentClass == null && !IsDynamic)
137137
{
138138
try
139139
{
140140
componentClass = ReflectHelper.ClassForName(componentClassName);
141141
}
142142
catch (Exception cnfe)
143143
{
144-
if (!IsDynamic) // TODO remove this if leave the Exception
145-
throw new MappingException("component class not found: " + componentClassName, cnfe);
146-
return null;
144+
throw new MappingException("component class not found: " + componentClassName, cnfe);
147145
}
148146
}
149147
return componentClass;

0 commit comments

Comments
 (0)