Skip to content

Commit 24e3b82

Browse files
authored
Add files via upload
1 parent 25be55c commit 24e3b82

File tree

4 files changed

+196
-0
lines changed

4 files changed

+196
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace NHibernate.Test.NHSpecificTest.GH3539;
6+
7+
public class CardInfo
8+
{
9+
private readonly ISet<string> _oldCards;
10+
11+
protected CardInfo() { }
12+
13+
public CardInfo(params string[] cards)
14+
{
15+
_oldCards = cards.ToHashSet();
16+
}
17+
18+
public virtual ISet<string> GetCardsCopy()
19+
{
20+
return _oldCards.ToHashSet();
21+
}
22+
23+
public override bool Equals(object obj)
24+
{
25+
if (obj is null)
26+
{
27+
return false;
28+
}
29+
if (ReferenceEquals(this, obj))
30+
{
31+
return true;
32+
}
33+
if (obj.GetType() != GetType())
34+
{
35+
return false;
36+
}
37+
var other = (CardInfo) obj;
38+
return _oldCards.SetEquals(other._oldCards);
39+
}
40+
41+
public override int GetHashCode()
42+
{
43+
var hashCode = new HashCode();
44+
hashCode.Add(_oldCards);
45+
return hashCode.ToHashCode();
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.IO;
3+
using System.Reflection;
4+
using NHibernate.Cfg;
5+
using NUnit.Framework;
6+
7+
namespace NHibernate.Test.NHSpecificTest.GH3539
8+
{
9+
[TestFixture]
10+
public class ImmutableComponentTest : TestCase
11+
{
12+
protected override string MappingsAssembly
13+
{
14+
get { return "NHibernate.Test"; }
15+
}
16+
17+
protected override string[] Mappings
18+
{
19+
get { return Array.Empty<string>(); }
20+
}
21+
22+
protected override void Configure(Configuration configuration)
23+
{
24+
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(
25+
"NHibernate.Test.NHSpecificTest.GH3539.Person.hbm.xml"))
26+
{
27+
using (StreamReader reader = new StreamReader(stream))
28+
{
29+
string mapping = reader.ReadToEnd();
30+
31+
configuration.AddXml(mapping);
32+
configuration.SetProperty(Cfg.Environment.GenerateStatistics, "true");
33+
}
34+
}
35+
}
36+
37+
protected override void OnTearDown()
38+
{
39+
using (ISession s = Sfi.OpenSession())
40+
using (ITransaction t = s.BeginTransaction())
41+
{
42+
s.Delete("from Person");
43+
t.Commit();
44+
}
45+
46+
base.OnTearDown();
47+
}
48+
49+
[Test]
50+
public void TestComponent()
51+
{
52+
int personId;
53+
using (ISession s = Sfi.OpenSession())
54+
using (ITransaction t = s.BeginTransaction())
55+
{
56+
var person = new Person(age: 20)
57+
{
58+
CardInfo = new("card1", "card2")
59+
};
60+
s.Save(person);
61+
t.Commit();
62+
s.Flush();
63+
personId = person.Id;
64+
}
65+
66+
using (ISession s = Sfi.OpenSession())
67+
using (ITransaction t = s.BeginTransaction())
68+
{
69+
var restored = s.Get<Person>(personId);
70+
71+
var oldCardInfo = restored.CardInfo;
72+
73+
Assert.That(oldCardInfo.GetCardsCopy().Contains("card1"));
74+
Assert.That(oldCardInfo.GetCardsCopy().Contains("card2"));
75+
76+
var newCardInfo = new CardInfo("card1", "card2");
77+
78+
Assert.That(oldCardInfo.Equals(newCardInfo), Is.True);
79+
80+
restored.CardInfo = newCardInfo;
81+
82+
// Expected behaviour:
83+
//
84+
// At this point there should be no DML statements because newCardInfo
85+
// is the same as the old one. But NHibernate will generate DELETE
86+
// followed by 2 INSERT into OldCards table. I'm not sure how to fail
87+
// an assertion when an unexpected DML is issued.
88+
89+
t.Commit();
90+
}
91+
}
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace NHibernate.Test.NHSpecificTest.GH3539
2+
{
3+
public class Person
4+
{
5+
private int _id;
6+
private int _age;
7+
private CardInfo _cardInfo;
8+
9+
protected Person() { }
10+
11+
public Person(int age)
12+
{
13+
_cardInfo = new();
14+
_age = age;
15+
}
16+
17+
public virtual int Id
18+
{
19+
get { return _id; }
20+
set { _id = value; }
21+
}
22+
23+
public virtual int Age
24+
{
25+
get { return _age; }
26+
set { _age = value; }
27+
}
28+
29+
public virtual CardInfo CardInfo
30+
{
31+
get { return _cardInfo; }
32+
set { _cardInfo = value; }
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping
3+
xmlns="urn:nhibernate-mapping-2.2"
4+
assembly="NHibernate.Test"
5+
namespace="NHibernate.Test.NHSpecificTest.GH3539">
6+
7+
<class name="Person" table="People">
8+
<id name="_id" type="int" column="ID" access="field">
9+
<generator class="increment"/>
10+
</id>
11+
<property name="_age" type="int" access="field"/>
12+
13+
<component name="_cardInfo" access="field">
14+
<set name="_oldCards" table="OldCards" cascade="all" access="field" >
15+
<key column="PersonId" />
16+
<element column="Card" type="String" />
17+
</set>
18+
</component>
19+
</class>
20+
21+
</hibernate-mapping>

0 commit comments

Comments
 (0)