Skip to content

Commit a56e683

Browse files
Merge pull request #1707 from fredericDelaporte/NH-3150
Fix NH-3150: select id generator improvements
2 parents 018cf1c + 4620c94 commit a56e683

File tree

14 files changed

+931
-67
lines changed

14 files changed

+931
-67
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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 NHibernate.Dialect;
13+
using NUnit.Framework;
14+
15+
namespace NHibernate.Test.NHSpecificTest.NH3150
16+
{
17+
using System.Threading.Tasks;
18+
[TestFixture]
19+
public class SelectGeneratorFixtureAsync : BugTestCase
20+
{
21+
protected override bool AppliesTo(Dialect.Dialect dialect)
22+
{
23+
// Test uses SQL-Server "instead of insert" triggers.
24+
return dialect is MsSql2008Dialect;
25+
}
26+
27+
protected override void OnTearDown()
28+
{
29+
using (var s = OpenSession())
30+
using (var tx = s.BeginTransaction())
31+
{
32+
// Delete loads in memory and handle the cascade.
33+
s.Delete("from Worker2");
34+
s.CreateQuery("delete from System.Object").ExecuteUpdate();
35+
tx.Commit();
36+
}
37+
}
38+
39+
[Test]
40+
public async Task CanUseNaturalIdWithMoreThanOnePropertyAsync()
41+
{
42+
using (var s = OpenSession())
43+
using (var tx = s.BeginTransaction())
44+
{
45+
var worker = new Worker
46+
{
47+
Name = "Mr Black",
48+
Position = "Managing Director"
49+
};
50+
await (s.SaveAsync(worker));
51+
52+
var worker2 = new Worker
53+
{
54+
Name = "Mr Black",
55+
Position = "Director"
56+
};
57+
await (s.SaveAsync(worker2));
58+
59+
await (tx.CommitAsync());
60+
Assert.That(worker.Id, Is.EqualTo(1), "Id of first worker should be 1");
61+
Assert.That(worker2.Id, Is.EqualTo(2), "Id of second worker should be 2");
62+
}
63+
}
64+
65+
[Test]
66+
public async Task CanUseKeyWithMoreThanOnePropertyAsync()
67+
{
68+
using (var s = OpenSession())
69+
using (var tx = s.BeginTransaction())
70+
{
71+
var worker = new WorkerWithExplicitKey
72+
{
73+
Name = "Mr Black",
74+
Position = "Managing Director"
75+
};
76+
await (s.SaveAsync(worker));
77+
78+
var worker2 = new WorkerWithExplicitKey
79+
{
80+
Name = "Mr Black",
81+
Position = "Director"
82+
};
83+
await (s.SaveAsync(worker2));
84+
85+
await (tx.CommitAsync());
86+
Assert.That(worker.Id, Is.EqualTo(1), "Id of first worker should be 1");
87+
Assert.That(worker2.Id, Is.EqualTo(2), "Id of second worker should be 2");
88+
}
89+
}
90+
91+
// Non-regression test case.
92+
[Test]
93+
public async Task CanUseComponentAsNaturalIdAsync()
94+
{
95+
using (var s = OpenSession())
96+
using (var tx = s.BeginTransaction())
97+
{
98+
var worker = new WorkerWithComponent
99+
{
100+
Nid = new WorkerWithComponent.NidComponent
101+
{
102+
Name = "Mr Black",
103+
Position = "Managing Director"
104+
}
105+
};
106+
await (s.SaveAsync(worker));
107+
108+
var worker2 = new WorkerWithComponent
109+
{
110+
Nid = new WorkerWithComponent.NidComponent
111+
{
112+
Name = "Mr Black",
113+
Position = "Director"
114+
}
115+
};
116+
await (s.SaveAsync(worker2));
117+
118+
await (tx.CommitAsync());
119+
Assert.That(worker.Id, Is.EqualTo(1), "Id of first worker should be 1");
120+
Assert.That(worker2.Id, Is.EqualTo(2), "Id of second worker should be 2");
121+
}
122+
}
123+
124+
[Test]
125+
public async Task IdBagWithSelectPOIDAsync()
126+
{
127+
int workerId;
128+
129+
using (var s = OpenSession())
130+
using (var tx = s.BeginTransaction())
131+
{
132+
var worker = new Worker2();
133+
var role = new Role { Description = "keeper" };
134+
135+
worker.Roles = new List<Role>() { role };
136+
137+
await (s.SaveAsync(worker));
138+
await (s.SaveAsync(role));
139+
140+
await (tx.CommitAsync());
141+
142+
workerId = worker.Id;
143+
}
144+
145+
using (var s = OpenSession())
146+
{
147+
var saved_worker = await (s.GetAsync<Worker2>(workerId));
148+
Assert.That(saved_worker.Roles, Is.Not.Null, "roles should not be null");
149+
Assert.That(saved_worker.Roles.Count, Is.EqualTo(1), "roles count should be 1");
150+
}
151+
}
152+
}
153+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Collections.Generic;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH3150
4+
{
5+
public class Worker
6+
{
7+
public virtual int Id { get; set; }
8+
9+
public virtual string Name { get; set; }
10+
public virtual string Position { get; set; }
11+
}
12+
13+
public class WorkerWithExplicitKey
14+
{
15+
public virtual int Id { get; set; }
16+
17+
public virtual string Name { get; set; }
18+
public virtual string Position { get; set; }
19+
}
20+
21+
public class WorkerWithComponent
22+
{
23+
public virtual int Id { get; set; }
24+
public virtual NidComponent Nid { get; set; }
25+
26+
public class NidComponent
27+
{
28+
public virtual string Name { get; set; }
29+
public virtual string Position { get; set; }
30+
// No need to implement Equals for what the test does.
31+
}
32+
}
33+
34+
public class Worker2
35+
{
36+
public virtual int Id { get; set; }
37+
public virtual IList<Role> Roles { get; set; }
38+
}
39+
40+
public class Role
41+
{
42+
public virtual int Id { get; set; }
43+
public virtual string Description { get; set; }
44+
}
45+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
namespace="NHibernate.Test.NHSpecificTest.NH3150"
4+
assembly="NHibernate.Test">
5+
<class name="Worker">
6+
<id name="Id">
7+
<generator class="select"/>
8+
</id>
9+
10+
<natural-id>
11+
<property name="Name"/>
12+
<property name="Position"/>
13+
</natural-id>
14+
</class>
15+
16+
<class name="WorkerWithExplicitKey">
17+
<id name="Id">
18+
<generator class="select">
19+
<param name="key">Name, Position</param>
20+
</generator>
21+
</id>
22+
23+
<property name="Name"/>
24+
<property name="Position"/>
25+
</class>
26+
27+
<class name="WorkerWithComponent">
28+
<id name="Id">
29+
<generator class="select"/>
30+
</id>
31+
32+
<natural-id>
33+
<component name="Nid">
34+
<property name="Name"/>
35+
<property name="Position"/>
36+
</component>
37+
</natural-id>
38+
</class>
39+
40+
<class name="Worker2">
41+
<id name="Id">
42+
<generator class="identity"/>
43+
</id>
44+
45+
<idbag name="Roles" inverse="false" cascade="all-delete-orphan" table="workerRoles">
46+
<collection-id column="id" type="Int32">
47+
<generator class="select"/>
48+
</collection-id>
49+
<key column="worker_id"/>
50+
<many-to-many column="role_id" class="Role" fetch="join"/>
51+
</idbag>
52+
</class>
53+
54+
<class name="Role">
55+
<id name="Id">
56+
<generator class="identity"/>
57+
</id>
58+
<property name="Description"/>
59+
</class>
60+
61+
<database-object>
62+
<create>
63+
CREATE TRIGGER dbo.id_gen_Worker ON dbo.Worker
64+
INSTEAD OF INSERT
65+
AS
66+
BEGIN
67+
SET NOCOUNT ON;
68+
69+
declare @lastval int
70+
set @lastval = (select max(Id) from Worker)
71+
if @lastval is null set @lastval = 0
72+
73+
SELECT * INTO #Inserted FROM Inserted
74+
UPDATE #Inserted set Id = @lastval+1
75+
SET NOCOUNT OFF;
76+
INSERT INTO Worker SELECT * FROM #Inserted
77+
END
78+
</create>
79+
<drop>
80+
DROP TRIGGER dbo.id_gen_Worker;
81+
</drop>
82+
</database-object>
83+
84+
<database-object>
85+
<create>
86+
CREATE TRIGGER dbo.id_gen_WorkerWithExplicitKey ON dbo.WorkerWithExplicitKey
87+
INSTEAD OF INSERT
88+
AS
89+
BEGIN
90+
SET NOCOUNT ON;
91+
92+
declare @lastval int
93+
set @lastval = (select max(Id) from WorkerWithExplicitKey)
94+
if @lastval is null set @lastval = 0
95+
96+
SELECT * INTO #Inserted FROM Inserted
97+
UPDATE #Inserted set Id = @lastval+1
98+
SET NOCOUNT OFF;
99+
INSERT INTO WorkerWithExplicitKey SELECT * FROM #Inserted
100+
END
101+
</create>
102+
<drop>
103+
DROP TRIGGER dbo.id_gen_WorkerWithExplicitKey;
104+
</drop>
105+
</database-object>
106+
107+
<database-object>
108+
<create>
109+
CREATE TRIGGER dbo.id_gen_WorkerWithComponent ON dbo.WorkerWithComponent
110+
INSTEAD OF INSERT
111+
AS
112+
BEGIN
113+
SET NOCOUNT ON;
114+
115+
declare @lastval int
116+
set @lastval = (select max(Id) from WorkerWithComponent)
117+
if @lastval is null set @lastval = 0
118+
119+
SELECT * INTO #Inserted FROM Inserted
120+
UPDATE #Inserted set Id = @lastval+1
121+
SET NOCOUNT OFF;
122+
INSERT INTO WorkerWithComponent SELECT * FROM #Inserted
123+
END
124+
</create>
125+
<drop>
126+
DROP TRIGGER dbo.id_gen_WorkerWithComponent;
127+
</drop>
128+
</database-object>
129+
130+
<database-object>
131+
<create>
132+
CREATE TRIGGER dbo.id_gen_workerRoles ON dbo.workerRoles
133+
INSTEAD OF INSERT
134+
AS
135+
BEGIN
136+
SET NOCOUNT ON;
137+
138+
declare @lastval int
139+
set @lastval = (select max(id) from workerRoles)
140+
if @lastval is null set @lastval = 0
141+
142+
SELECT * INTO #Inserted FROM Inserted
143+
UPDATE #Inserted set id = @lastval+1
144+
SET NOCOUNT OFF;
145+
INSERT INTO workerRoles SELECT * FROM #Inserted
146+
END
147+
GO
148+
</create>
149+
<drop>
150+
DROP TRIGGER dbo.id_gen_workerRoles;
151+
</drop>
152+
</database-object>
153+
</hibernate-mapping>

0 commit comments

Comments
 (0)