Skip to content

Commit 87cce36

Browse files
hailtondecastrohazzik
authored andcommitted
Test case and fix to NH-3898
(https://nhibernate.jira.com/browse/NH-3898) Configuring a property with generated="insert" it makes "Property.IsUpdatable" "false" even using update="true" in the xml mapping file. Please consider keep this issue as Critical, as it does not break the application, it makes the applications migrated from NHibernate version 2 to 3 or 4 generates inconsistent data within the database. As soon as I can, I will send a pull request
1 parent f91c3b2 commit 87cce36

File tree

4 files changed

+150
-1
lines changed

4 files changed

+150
-1
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+

2+
3+
using System;
4+
using System.Collections.Generic;
5+
namespace NHibernate.Test.NHSpecificTest.NH3898
6+
{
7+
public class Employee
8+
{
9+
private Int32 id;
10+
public virtual Int32 Id
11+
{
12+
get { return id; }
13+
set { id = value; }
14+
}
15+
16+
private String name;
17+
public virtual String Name
18+
{
19+
get { return name; }
20+
set { name = value; }
21+
}
22+
23+
private Int32 promotionCount;
24+
25+
public virtual Int32 PromotionCount
26+
{
27+
get { return promotionCount; }
28+
set { promotionCount = value; }
29+
}
30+
}
31+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using NHibernate.Dialect;
6+
using NUnit.Framework;
7+
using NHibernate.Test.NHSpecificTest;
8+
using Iesi.Collections.Generic;
9+
using NHibernate;
10+
using System.Data;
11+
using NHibernate.Criterion;
12+
13+
namespace NHibernate.Test.NHSpecificTest.NH3898
14+
{
15+
/// <summary>
16+
/// <para>
17+
/// </para>
18+
/// </remarks>
19+
[TestFixture]
20+
public class Fixture : BugTestCase
21+
{
22+
protected override void Configure(NHibernate.Cfg.Configuration configuration)
23+
{
24+
#region removing possible second-level-cache configs
25+
configuration.Properties.Remove(NHibernate.Cfg.Environment.CacheProvider);
26+
configuration.Properties.Remove(NHibernate.Cfg.Environment.UseQueryCache);
27+
configuration.Properties.Add(NHibernate.Cfg.Environment.UseQueryCache, "true");
28+
configuration.Properties.Remove(NHibernate.Cfg.Environment.UseSecondLevelCache);
29+
#endregion
30+
31+
base.Configure(configuration);
32+
}
33+
34+
protected override void OnTearDown()
35+
{
36+
base.OnTearDown();
37+
using (ISession s = OpenSession())
38+
{
39+
int countUpdate = 0;
40+
41+
countUpdate =
42+
s
43+
.CreateSQLQuery("DELETE FROM T_EMPLOYEE")
44+
.ExecuteUpdate();
45+
Assert.AreEqual(1, countUpdate);
46+
47+
s.Flush();
48+
}
49+
}
50+
51+
protected override void OnSetUp()
52+
{
53+
base.OnSetUp();
54+
}
55+
56+
protected override bool AppliesTo(global::NHibernate.Dialect.Dialect dialect)
57+
{
58+
//return dialect as MsSql2005Dialect != null;
59+
return base.AppliesTo(dialect);
60+
}
61+
62+
/// <summary>
63+
/// Test that reproduces the problem.
64+
/// </summary>
65+
[Test]
66+
public void GeneratedInsertUpdateTrue()
67+
{
68+
using (ISession session = this.OpenSession())
69+
{
70+
using (ITransaction tx = session.BeginTransaction())
71+
{
72+
Employee employee = new Employee();
73+
employee.Id = 1;
74+
employee.Name = "Employee 1";
75+
employee.PromotionCount = 9999999;
76+
session.Save(employee);
77+
Assert.AreEqual(0, employee.PromotionCount);
78+
tx.Commit();
79+
}
80+
}
81+
82+
using (ISession session = this.OpenSession())
83+
{
84+
using (ITransaction tx = session.BeginTransaction())
85+
{
86+
Employee employee = session.Get<Employee>(1);
87+
employee.Name = "Employee 1 changed";
88+
employee.PromotionCount++;
89+
Assert.AreEqual(1, employee.PromotionCount);
90+
tx.Commit();
91+
}
92+
}
93+
94+
using (ISession session = this.OpenSession())
95+
{
96+
Employee employee = session.Get<Employee>(1);
97+
Assert.AreEqual("Employee 1 changed", employee.Name);
98+
Assert.AreEqual(1, employee.PromotionCount);
99+
}
100+
}
101+
}
102+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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" default-access="field.camelcase"
4+
default-lazy="true" default-cascade="none">
5+
6+
<class name="Employee" table="T_EMPLOYEE" mutable="true">
7+
<id name="Id" column="C_E_ID">
8+
<generator class="native" />
9+
</id>
10+
<property name="Name" column="C_E_NAME"/>
11+
<property name="PromotionCount" insert="false" generated="insert">
12+
<column name="C_E_PROMOTION_COUNT" default="0"/>
13+
</property>
14+
</class>
15+
</hibernate-mapping>

src/NHibernate/Cfg/XmlHbmBinding/PropertiesBinder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ private void BindValueProperty(HbmProperty propertyMapping, Property property)
302302
throw new MappingException("cannot specify both update=\"true\" and generated=\"" + generation
303303
+ "\" for property: " + propertyMapping.Name);
304304
}
305-
else
305+
//Fix for NH-3898
306+
else if (generation == PropertyGeneration.Always)
306307
{
307308
property.IsUpdateable = false;
308309
}

0 commit comments

Comments
 (0)