Skip to content

Fix NH-3150: select id generator improvements #1707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System.Collections.Generic;
using NHibernate.Dialect;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH3150
{
using System.Threading.Tasks;
[TestFixture]
public class SelectGeneratorFixtureAsync : BugTestCase
{
protected override bool AppliesTo(Dialect.Dialect dialect)
{
// Test uses SQL-Server "instead of insert" triggers.
return dialect is MsSql2008Dialect;
}

protected override void OnTearDown()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
// Delete loads in memory and handle the cascade.
s.Delete("from Worker2");
s.CreateQuery("delete from System.Object").ExecuteUpdate();
tx.Commit();
}
}

[Test]
public async Task CanUseNaturalIdWithMoreThanOnePropertyAsync()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
var worker = new Worker
{
Name = "Mr Black",
Position = "Managing Director"
};
await (s.SaveAsync(worker));

var worker2 = new Worker
{
Name = "Mr Black",
Position = "Director"
};
await (s.SaveAsync(worker2));

await (tx.CommitAsync());
Assert.That(worker.Id, Is.EqualTo(1), "Id of first worker should be 1");
Assert.That(worker2.Id, Is.EqualTo(2), "Id of second worker should be 2");
}
}

[Test]
public async Task CanUseKeyWithMoreThanOnePropertyAsync()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
var worker = new WorkerWithExplicitKey
{
Name = "Mr Black",
Position = "Managing Director"
};
await (s.SaveAsync(worker));

var worker2 = new WorkerWithExplicitKey
{
Name = "Mr Black",
Position = "Director"
};
await (s.SaveAsync(worker2));

await (tx.CommitAsync());
Assert.That(worker.Id, Is.EqualTo(1), "Id of first worker should be 1");
Assert.That(worker2.Id, Is.EqualTo(2), "Id of second worker should be 2");
}
}

// Non-regression test case.
[Test]
public async Task CanUseComponentAsNaturalIdAsync()
{
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
var worker = new WorkerWithComponent
{
Nid = new WorkerWithComponent.NidComponent
{
Name = "Mr Black",
Position = "Managing Director"
}
};
await (s.SaveAsync(worker));

var worker2 = new WorkerWithComponent
{
Nid = new WorkerWithComponent.NidComponent
{
Name = "Mr Black",
Position = "Director"
}
};
await (s.SaveAsync(worker2));

await (tx.CommitAsync());
Assert.That(worker.Id, Is.EqualTo(1), "Id of first worker should be 1");
Assert.That(worker2.Id, Is.EqualTo(2), "Id of second worker should be 2");
}
}

[Test]
public async Task IdBagWithSelectPOIDAsync()
{
int workerId;

using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
var worker = new Worker2();
var role = new Role { Description = "keeper" };

worker.Roles = new List<Role>() { role };

await (s.SaveAsync(worker));
await (s.SaveAsync(role));

await (tx.CommitAsync());

workerId = worker.Id;
}

using (var s = OpenSession())
{
var saved_worker = await (s.GetAsync<Worker2>(workerId));
Assert.That(saved_worker.Roles, Is.Not.Null, "roles should not be null");
Assert.That(saved_worker.Roles.Count, Is.EqualTo(1), "roles count should be 1");
}
}
}
}
45 changes: 45 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3150/Class.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.NH3150
{
public class Worker
{
public virtual int Id { get; set; }

public virtual string Name { get; set; }
public virtual string Position { get; set; }
}

public class WorkerWithExplicitKey
{
public virtual int Id { get; set; }

public virtual string Name { get; set; }
public virtual string Position { get; set; }
}

public class WorkerWithComponent
{
public virtual int Id { get; set; }
public virtual NidComponent Nid { get; set; }

public class NidComponent
{
public virtual string Name { get; set; }
public virtual string Position { get; set; }
// No need to implement Equals for what the test does.
}
}

public class Worker2
{
public virtual int Id { get; set; }
public virtual IList<Role> Roles { get; set; }
}

public class Role
{
public virtual int Id { get; set; }
public virtual string Description { get; set; }
}
}
153 changes: 153 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3150/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="NHibernate.Test.NHSpecificTest.NH3150"
assembly="NHibernate.Test">
<class name="Worker">
<id name="Id">
<generator class="select"/>
</id>

<natural-id>
<property name="Name"/>
<property name="Position"/>
</natural-id>
</class>

<class name="WorkerWithExplicitKey">
<id name="Id">
<generator class="select">
<param name="key">Name, Position</param>
</generator>
</id>

<property name="Name"/>
<property name="Position"/>
</class>

<class name="WorkerWithComponent">
<id name="Id">
<generator class="select"/>
</id>

<natural-id>
<component name="Nid">
<property name="Name"/>
<property name="Position"/>
</component>
</natural-id>
</class>

<class name="Worker2">
<id name="Id">
<generator class="identity"/>
</id>

<idbag name="Roles" inverse="false" cascade="all-delete-orphan" table="workerRoles">
<collection-id column="id" type="Int32">
<generator class="select"/>
</collection-id>
<key column="worker_id"/>
<many-to-many column="role_id" class="Role" fetch="join"/>
</idbag>
</class>

<class name="Role">
<id name="Id">
<generator class="identity"/>
</id>
<property name="Description"/>
</class>

<database-object>
<create>
CREATE TRIGGER dbo.id_gen_Worker ON dbo.Worker
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON;

declare @lastval int
set @lastval = (select max(Id) from Worker)
if @lastval is null set @lastval = 0

SELECT * INTO #Inserted FROM Inserted
UPDATE #Inserted set Id = @lastval+1
SET NOCOUNT OFF;
INSERT INTO Worker SELECT * FROM #Inserted
END
</create>
<drop>
DROP TRIGGER dbo.id_gen_Worker;
</drop>
</database-object>

<database-object>
<create>
CREATE TRIGGER dbo.id_gen_WorkerWithExplicitKey ON dbo.WorkerWithExplicitKey
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON;

declare @lastval int
set @lastval = (select max(Id) from WorkerWithExplicitKey)
if @lastval is null set @lastval = 0

SELECT * INTO #Inserted FROM Inserted
UPDATE #Inserted set Id = @lastval+1
SET NOCOUNT OFF;
INSERT INTO WorkerWithExplicitKey SELECT * FROM #Inserted
END
</create>
<drop>
DROP TRIGGER dbo.id_gen_WorkerWithExplicitKey;
</drop>
</database-object>

<database-object>
<create>
CREATE TRIGGER dbo.id_gen_WorkerWithComponent ON dbo.WorkerWithComponent
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON;

declare @lastval int
set @lastval = (select max(Id) from WorkerWithComponent)
if @lastval is null set @lastval = 0

SELECT * INTO #Inserted FROM Inserted
UPDATE #Inserted set Id = @lastval+1
SET NOCOUNT OFF;
INSERT INTO WorkerWithComponent SELECT * FROM #Inserted
END
</create>
<drop>
DROP TRIGGER dbo.id_gen_WorkerWithComponent;
</drop>
</database-object>

<database-object>
<create>
CREATE TRIGGER dbo.id_gen_workerRoles ON dbo.workerRoles
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON;

declare @lastval int
set @lastval = (select max(id) from workerRoles)
if @lastval is null set @lastval = 0

SELECT * INTO #Inserted FROM Inserted
UPDATE #Inserted set id = @lastval+1
SET NOCOUNT OFF;
INSERT INTO workerRoles SELECT * FROM #Inserted
END
GO
</create>
<drop>
DROP TRIGGER dbo.id_gen_workerRoles;
</drop>
</database-object>
</hibernate-mapping>
Loading