Skip to content

Commit fe9f5d1

Browse files
committed
Relax collection-id mapping
- Allow specifying generator as attribute - Allow using column element instead of requiring attribute - Set default value for type to Int32
1 parent 492c0bf commit fe9f5d1

File tree

12 files changed

+230
-146
lines changed

12 files changed

+230
-146
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
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 type="Int32" column="item_id">
10+
<generator class="increment" />
11+
</collection-id>
12+
<key column="a_id" />
13+
<element type="string" />
14+
</idbag>
1515
</class>
1616
</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.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)