Skip to content

Commit 3a2e1cb

Browse files
authored
Relax collection-id mapping (#3687)
1 parent 4374a33 commit 3a2e1cb

File tree

14 files changed

+324
-157
lines changed

14 files changed

+324
-157
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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;
12+
using System.Collections;
13+
using System.Collections.Generic;
14+
using NUnit.Framework;
15+
16+
namespace NHibernate.Test.CollectionTest
17+
{
18+
using System.Threading.Tasks;
19+
[TestFixture]
20+
public class IdBagRelaxedFixtureAsync : TestCase
21+
{
22+
protected override string[] Mappings
23+
{
24+
get { return new string[] { "CollectionTest.IdBagRelaxedFixture.hbm.xml" }; }
25+
}
26+
27+
protected override string MappingsAssembly
28+
{
29+
get { return "NHibernate.Test"; }
30+
}
31+
32+
protected override void OnTearDown()
33+
{
34+
using( ISession s = OpenSession() )
35+
{
36+
s.Delete( "from A" );
37+
s.Flush();
38+
}
39+
}
40+
41+
[Test]
42+
public async Task SimpleAsync()
43+
{
44+
A a = new A();
45+
a.Name = "first generic type";
46+
a.Items = new List<string>();
47+
a.Items.Add( "first string" );
48+
a.Items.Add( "second string" );
49+
50+
ISession s = OpenSession();
51+
await (s.SaveOrUpdateAsync( a ));
52+
// this flush should test how NH wraps a generic collection with its
53+
// own persistent collection
54+
await (s.FlushAsync());
55+
s.Close();
56+
Assert.IsNotNull( a.Id );
57+
Assert.AreEqual( "first string", ( string ) a.Items[ 0 ] );
58+
59+
s = OpenSession();
60+
a = ( A ) await (s.LoadAsync( typeof( A ), a.Id ));
61+
Assert.AreEqual( "first string", ( string ) a.Items[ 0 ], "first item should be 'first string'" );
62+
Assert.AreEqual( "second string", ( string ) a.Items[ 1 ], "second item should be 'second string'" );
63+
// ensuring the correct generic type was constructed
64+
a.Items.Add( "third string" );
65+
Assert.AreEqual( 3, a.Items.Count, "3 items in the list now" );
66+
67+
a.Items[ 1 ] = "new second string";
68+
await (s.FlushAsync());
69+
s.Close();
70+
}
71+
}
72+
}

src/NHibernate.Test/CollectionTest/IdBagFixture.hbm.xml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
<generator class="native" />
66
</id>
77
<property name="Name" column="aname" />
8-
<idbag name="Items" cascade="all-delete-orphan">
9-
<collection-id type="Int32" column="item_id">
10-
<generator class="increment" />
11-
</collection-id>
12-
<key column="a_id" />
13-
<element type="string" />
14-
</idbag>
8+
<idbag name="Items" cascade="all-delete-orphan">
9+
<collection-id>
10+
<column name="item_id" />
11+
<generator class="increment" />
12+
<type name="Int64" />
13+
</collection-id>
14+
<key column="a_id" />
15+
<element type="string" />
16+
</idbag>
1517
</class>
1618
</hibernate-mapping>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using NUnit.Framework;
5+
6+
namespace NHibernate.Test.CollectionTest
7+
{
8+
[TestFixture]
9+
public class IdBagRelaxedFixture : TestCase
10+
{
11+
protected override string[] Mappings
12+
{
13+
get { return new string[] { "CollectionTest.IdBagRelaxedFixture.hbm.xml" }; }
14+
}
15+
16+
protected override string MappingsAssembly
17+
{
18+
get { return "NHibernate.Test"; }
19+
}
20+
21+
protected override void OnTearDown()
22+
{
23+
using( ISession s = OpenSession() )
24+
{
25+
s.Delete( "from A" );
26+
s.Flush();
27+
}
28+
}
29+
30+
[Test]
31+
public void Simple()
32+
{
33+
A a = new A();
34+
a.Name = "first generic type";
35+
a.Items = new List<string>();
36+
a.Items.Add( "first string" );
37+
a.Items.Add( "second string" );
38+
39+
ISession s = OpenSession();
40+
s.SaveOrUpdate( a );
41+
// this flush should test how NH wraps a generic collection with its
42+
// own persistent collection
43+
s.Flush();
44+
s.Close();
45+
Assert.IsNotNull( a.Id );
46+
Assert.AreEqual( "first string", ( string ) a.Items[ 0 ] );
47+
48+
s = OpenSession();
49+
a = ( A ) s.Load( typeof( A ), a.Id );
50+
Assert.AreEqual( "first string", ( string ) a.Items[ 0 ], "first item should be 'first string'" );
51+
Assert.AreEqual( "second string", ( string ) a.Items[ 1 ], "second item should be 'second string'" );
52+
// ensuring the correct generic type was constructed
53+
a.Items.Add( "third string" );
54+
Assert.AreEqual( 3, a.Items.Count, "3 items in the list now" );
55+
56+
a.Items[ 1 ] = "new second string";
57+
s.Flush();
58+
s.Close();
59+
}
60+
}
61+
}
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" namespace="NHibernate.Test.CollectionTest">
3+
<class name="A" table="a" lazy="false">
4+
<id name="Id" column="id" unsaved-value="null">
5+
<generator class="native" />
6+
</id>
7+
<property name="Name" column="aname" />
8+
<idbag name="Items" cascade="all-delete-orphan">
9+
<collection-id column="item_id" generator="increment" />
10+
<key column="a_id" />
11+
<element type="string" />
12+
</idbag>
13+
</class>
14+
</hibernate-mapping>

src/NHibernate.Test/MappingByCode/MappersTests/CollectionIdMapperTests.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public void WhenCreateThenHasDefaultTypeAndGenerator()
1717
var hbmId = new HbmCollectionId();
1818
new CollectionIdMapper(hbmId);
1919
Assert.That(hbmId.generator.@class, Is.Not.Null.And.Not.Empty);
20-
Assert.That(hbmId.type, Is.Not.Null.And.Not.Empty);
20+
Assert.That(hbmId.type1, Is.Not.Null.And.Not.Empty);
21+
Assert.That(hbmId.type, Is.Null);
2122
}
2223

2324
[Test]
@@ -27,19 +28,21 @@ public void WhenSetGeneratorThenChangeType()
2728
new CollectionIdMapper(hbmId).Generator(Generators.HighLow);
2829

2930
Assert.That(hbmId.generator.@class, Is.EqualTo("hilo"));
30-
Assert.That(hbmId.type.ToLowerInvariant(), Does.Contain("int"));
31+
Assert.That(hbmId.type1.ToLowerInvariant(), Does.Contain("int"));
32+
Assert.That(hbmId.type, Is.Null);
3133
}
3234

3335
[Test]
3436
public void WhenForceTypeThenNotChangeType()
3537
{
3638
var hbmId = new HbmCollectionId();
3739
var collectionIdMapper = new CollectionIdMapper(hbmId);
38-
collectionIdMapper.Type((IIdentifierType) NHibernateUtil.Int64);
40+
collectionIdMapper.Type(NHibernateUtil.Int64);
3941
collectionIdMapper.Generator(Generators.HighLow);
4042

4143
Assert.That(hbmId.generator.@class, Is.EqualTo("hilo"));
42-
Assert.That(hbmId.type, Is.EqualTo("Int64"));
44+
Assert.That(hbmId.type1, Is.EqualTo("Int64"));
45+
Assert.That(hbmId.type, Is.Null);
4346
}
4447

4548
[Test]

src/NHibernate.Tool.HbmXsd/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class Program
1010
private static void Main(string[] args)
1111
{
1212
string outFile = Path.GetFullPath(args.Length == 0
13-
? @"..\..\..\..\NHibernate\Cfg\MappingSchema\Hbm.generated.cs"
13+
? Path.Combine("..", "..", "..", "..", "NHibernate", "Cfg", "MappingSchema", "Hbm.generated.cs")
1414
: args[0]);
1515
if (!Directory.Exists(Path.GetDirectoryName(outFile)))
1616
{
@@ -19,11 +19,12 @@ private static void Main(string[] args)
1919
Environment.ExitCode = -1;
2020
return;
2121
}
22+
2223
var currentUiCulture = new CultureInfo("en-us");
2324
Thread.CurrentThread.CurrentCulture = currentUiCulture;
2425
Thread.CurrentThread.CurrentUICulture = currentUiCulture;
2526
new HbmCodeGenerator().Execute(outFile);
2627
Console.WriteLine("Done");
2728
}
2829
}
29-
}
30+
}

0 commit comments

Comments
 (0)