Skip to content

Commit 9a6b6bc

Browse files
hailtondecastrohazzik
authored andcommitted
Fix generated on insert property being not updatable (#500)
Fixes #1371
1 parent c89bd84 commit 9a6b6bc

File tree

5 files changed

+165
-11
lines changed

5 files changed

+165
-11
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 NUnit.Framework;
12+
13+
namespace NHibernate.Test.NHSpecificTest.NH3898
14+
{
15+
using System.Threading.Tasks;
16+
[TestFixture]
17+
public class FixtureAsync : BugTestCase
18+
{
19+
protected override void Configure(Cfg.Configuration configuration)
20+
{
21+
configuration.SetProperty(Cfg.Environment.UseQueryCache, "false");
22+
configuration.SetProperty(Cfg.Environment.UseSecondLevelCache, "false");
23+
}
24+
25+
protected override void OnTearDown()
26+
{
27+
using (var s = OpenSession())
28+
using (var tx = s.BeginTransaction())
29+
{
30+
s.CreateQuery("delete from Employee").ExecuteUpdate();
31+
32+
tx.Commit();
33+
}
34+
}
35+
36+
[Test]
37+
public async Task GeneratedInsertUpdateTrueAsync()
38+
{
39+
object id;
40+
using (var session = OpenSession())
41+
using (var tx = session.BeginTransaction())
42+
{
43+
var employee = new Employee
44+
{
45+
Name = "Employee 1",
46+
PromotionCount = 9999999
47+
};
48+
id = await (session.SaveAsync(employee));
49+
await (tx.CommitAsync());
50+
}
51+
52+
using (var session = OpenSession())
53+
using (var tx = session.BeginTransaction())
54+
{
55+
var employee = await (session.GetAsync<Employee>(id));
56+
Assert.That(employee.PromotionCount, Is.EqualTo(0));
57+
employee.Name = "Employee 1 changed";
58+
employee.PromotionCount++;
59+
await (tx.CommitAsync());
60+
}
61+
62+
using (var session = OpenSession())
63+
using (session.BeginTransaction())
64+
{
65+
var employee = await (session.GetAsync<Employee>(id));
66+
Assert.That(employee.Name, Is.EqualTo("Employee 1 changed"));
67+
Assert.That(employee.PromotionCount, Is.EqualTo(1));
68+
}
69+
}
70+
}
71+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH3898
4+
{
5+
public class Employee
6+
{
7+
public virtual int Id { get; set; }
8+
9+
public virtual string Name { get; set; }
10+
11+
public virtual int PromotionCount { get; set; }
12+
}
13+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using NUnit.Framework;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH3898
4+
{
5+
[TestFixture]
6+
public class Fixture : BugTestCase
7+
{
8+
protected override void Configure(Cfg.Configuration configuration)
9+
{
10+
configuration.SetProperty(Cfg.Environment.UseQueryCache, "false");
11+
configuration.SetProperty(Cfg.Environment.UseSecondLevelCache, "false");
12+
}
13+
14+
protected override void OnTearDown()
15+
{
16+
using (var s = OpenSession())
17+
using (var tx = s.BeginTransaction())
18+
{
19+
s.CreateQuery("delete from Employee").ExecuteUpdate();
20+
21+
tx.Commit();
22+
}
23+
}
24+
25+
[Test]
26+
public void GeneratedInsertUpdateTrue()
27+
{
28+
object id;
29+
using (var session = OpenSession())
30+
using (var tx = session.BeginTransaction())
31+
{
32+
var employee = new Employee
33+
{
34+
Name = "Employee 1",
35+
PromotionCount = 9999999
36+
};
37+
id = session.Save(employee);
38+
tx.Commit();
39+
}
40+
41+
using (var session = OpenSession())
42+
using (var tx = session.BeginTransaction())
43+
{
44+
var employee = session.Get<Employee>(id);
45+
Assert.That(employee.PromotionCount, Is.EqualTo(0));
46+
employee.Name = "Employee 1 changed";
47+
employee.PromotionCount++;
48+
tx.Commit();
49+
}
50+
51+
using (var session = OpenSession())
52+
using (session.BeginTransaction())
53+
{
54+
var employee = session.Get<Employee>(id);
55+
Assert.That(employee.Name, Is.EqualTo("Employee 1 changed"));
56+
Assert.That(employee.PromotionCount, Is.EqualTo(1));
57+
}
58+
}
59+
}
60+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
3+
namespace="NHibernate.Test.NHSpecificTest.NH3898">
4+
5+
<class name="Employee">
6+
<id name="Id">
7+
<generator class="native" />
8+
</id>
9+
<property name="Name"/>
10+
<property name="PromotionCount" insert="false" generated="insert">
11+
<column name="PromotionCount" default="0"/>
12+
</property>
13+
</class>
14+
</hibernate-mapping>

src/NHibernate/Cfg/XmlHbmBinding/PropertiesBinder.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -289,23 +289,19 @@ private void BindValueProperty(HbmProperty propertyMapping, Property property)
289289
throw new MappingException("cannot specify both insert=\"true\" and generated=\"" + generation
290290
+ "\" for property: " + propertyMapping.Name);
291291
}
292-
else
293-
{
294-
property.IsInsertable = false;
295-
}
296-
292+
property.IsInsertable = false;
293+
}
294+
if (generation == PropertyGeneration.Always)
295+
{
297296
// properties generated on update can never be updateable...
298-
if (propertyMapping.updateSpecified && property.IsUpdateable && generation == PropertyGeneration.Always)
297+
if (propertyMapping.updateSpecified && property.IsUpdateable)
299298
{
300299
// the user specifically supplied update="true",
301300
// which constitutes an illegal combo
302301
throw new MappingException("cannot specify both update=\"true\" and generated=\"" + generation
303302
+ "\" for property: " + propertyMapping.Name);
304303
}
305-
else
306-
{
307-
property.IsUpdateable = false;
308-
}
304+
property.IsUpdateable = false;
309305
}
310306
}
311307

@@ -421,4 +417,4 @@ private string GetPropertyAccessorName(string propertyMappedAccessor)
421417
return propertyMappedAccessor ?? Mappings.DefaultAccess;
422418
}
423419
}
424-
}
420+
}

0 commit comments

Comments
 (0)