Skip to content

Commit f3d2b8d

Browse files
committed
NH-3488 - Strongly Typed Updates and Deletes
1 parent fac75a2 commit f3d2b8d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+4962
-1578
lines changed

src/NHibernate.Test/Hql/Ast/BulkManipulation.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,29 @@ public void SimpleInsert()
6464
data.Cleanup();
6565
}
6666

67+
[Test]
68+
public void SimpleInsertFromAggregate()
69+
{
70+
var data = new TestData(this);
71+
data.Prepare();
72+
73+
ISession s = OpenSession();
74+
ITransaction t = s.BeginTransaction();
75+
76+
s.CreateQuery("insert into Pickup (id, Vin, Owner) select id, max(Vin), max(Owner) from Car group by id").ExecuteUpdate();
77+
78+
t.Commit();
79+
t = s.BeginTransaction();
80+
81+
s.CreateQuery("delete Vehicle").ExecuteUpdate();
82+
83+
t.Commit();
84+
s.Close();
85+
86+
data.Cleanup();
87+
}
88+
89+
6790
[Test]
6891
public void InsertWithManyToOne()
6992
{
@@ -86,6 +109,31 @@ public void InsertWithManyToOne()
86109
data.Cleanup();
87110
}
88111

112+
[Test]
113+
public void InsertWithManyToOneAsParameter()
114+
{
115+
var data = new TestData(this);
116+
data.Prepare();
117+
118+
ISession s = OpenSession();
119+
ITransaction t = s.BeginTransaction();
120+
121+
var mother = data.Butterfly;
122+
123+
s.CreateQuery(
124+
"insert into Animal (description, bodyWeight, mother) select description, bodyWeight, :mother from Human")
125+
.SetEntity("mother",mother)
126+
.ExecuteUpdate();
127+
128+
t.Commit();
129+
t = s.BeginTransaction();
130+
131+
t.Commit();
132+
s.Close();
133+
134+
data.Cleanup();
135+
}
136+
89137
[Test]
90138
public void InsertWithMismatchedTypes()
91139
{
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using NHibernate.Hql.Ast.ANTLR;
4+
using NHibernate.Util;
5+
6+
namespace NHibernate.Test.NHSpecificTest.NH3438
7+
{
8+
public class BaseFixture: TestCase
9+
{
10+
private readonly IDictionary<string, IFilter> emptyfilters = new CollectionHelper.EmptyMapClass<string, IFilter>();
11+
12+
#region Overrides of TestCase
13+
14+
protected override IList Mappings
15+
{
16+
get { return new string[0]; }
17+
}
18+
19+
#endregion
20+
21+
protected override void Configure(Cfg.Configuration configuration)
22+
{
23+
var assembly = GetType().Assembly;
24+
string mappingNamespace = GetType().Namespace;
25+
foreach (var resource in assembly.GetManifestResourceNames())
26+
{
27+
if (resource.StartsWith(mappingNamespace) && resource.EndsWith(".hbm.xml"))
28+
{
29+
configuration.AddResource(resource, assembly);
30+
}
31+
}
32+
}
33+
34+
public string GetSql(string query)
35+
{
36+
return GetSql(query, null);
37+
}
38+
39+
public string GetSql(string query, IDictionary<string, string> replacements)
40+
{
41+
var qt = new QueryTranslatorImpl(null, new HqlParseEngine(query, false, sessions).Parse(), emptyfilters, sessions);
42+
qt.Compile(replacements, false);
43+
return qt.SQLString;
44+
}
45+
}
46+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace NHibernate.Test.NHSpecificTest.NH3438.Domain
2+
{
3+
public class Address
4+
{
5+
private string street;
6+
private string city;
7+
private string postalCode;
8+
private string country;
9+
private StateProvince stateProvince;
10+
11+
public string Street
12+
{
13+
get { return street; }
14+
set { street = value; }
15+
}
16+
17+
public string City
18+
{
19+
get { return city; }
20+
set { city = value; }
21+
}
22+
23+
public string PostalCode
24+
{
25+
get { return postalCode; }
26+
set { postalCode = value; }
27+
}
28+
29+
public string Country
30+
{
31+
get { return country; }
32+
set { country = value; }
33+
}
34+
35+
public StateProvince StateProvince
36+
{
37+
get { return stateProvince; }
38+
set { stateProvince = value; }
39+
}
40+
}
41+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System.Collections.Generic;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH3438.Domain
4+
{
5+
public class Animal
6+
{
7+
private long id;
8+
private float bodyWeight;
9+
private ISet<Animal> offspring;
10+
private Animal mother;
11+
private Animal father;
12+
private string description;
13+
private Zoo zoo;
14+
private string serialNumber;
15+
16+
public virtual long Id
17+
{
18+
get { return id; }
19+
set { id = value; }
20+
}
21+
22+
public virtual float BodyWeight
23+
{
24+
get { return bodyWeight; }
25+
set { bodyWeight = value; }
26+
}
27+
28+
public virtual ISet<Animal> Offspring
29+
{
30+
get { return offspring; }
31+
set { offspring = value; }
32+
}
33+
34+
public virtual Animal Mother
35+
{
36+
get { return mother; }
37+
set { mother = value; }
38+
}
39+
40+
public virtual Animal Father
41+
{
42+
get { return father; }
43+
set { father = value; }
44+
}
45+
46+
public virtual string Description
47+
{
48+
get { return description; }
49+
set { description = value; }
50+
}
51+
52+
public virtual Zoo Zoo
53+
{
54+
get { return zoo; }
55+
set { zoo = value; }
56+
}
57+
58+
public virtual string SerialNumber
59+
{
60+
get { return serialNumber; }
61+
set { serialNumber = value; }
62+
}
63+
64+
public virtual void AddOffspring(Animal offSpring)
65+
{
66+
if (offspring == null)
67+
{
68+
offspring = new HashSet<Animal>();
69+
}
70+
71+
offspring.Add(offSpring);
72+
}
73+
}
74+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.NHSpecificTest.NH3438.Domain"
5+
default-access="field.camelcase">
6+
7+
<class name="Animal">
8+
<id name="Id">
9+
<generator class="native"/>
10+
</id>
11+
<property name="Description"/>
12+
<property name="BodyWeight" column="body_weight"/>
13+
<many-to-one name="Mother" column="mother_id"/>
14+
<many-to-one name="Father" column="father_id"/>
15+
<many-to-one name="Zoo" column="zoo_id"/>
16+
<property name="SerialNumber"/>
17+
<set name="Offspring" order-by="father_id">
18+
<key column="mother_id"/>
19+
<one-to-many class="Animal"/>
20+
</set>
21+
<joined-subclass name="Reptile">
22+
<key column="animal"/>
23+
<property name="BodyTemperature"/>
24+
<joined-subclass name="Lizard">
25+
<key column="reptile"/>
26+
</joined-subclass>
27+
</joined-subclass>
28+
<joined-subclass name="Dragon">
29+
<key column="animal"/>
30+
<property name="FireTemperature"/>
31+
</joined-subclass>
32+
<joined-subclass name="Mammal">
33+
<key column="animal"/>
34+
<property name="Pregnant"/>
35+
<property name="Birthdate" type="date"/>
36+
<joined-subclass name="DomesticAnimal">
37+
<key column="mammal"/>
38+
<many-to-one name="owner"/>
39+
<joined-subclass name="Cat">
40+
<key column="mammal"/>
41+
</joined-subclass>
42+
<joined-subclass name="Dog">
43+
<key column="mammal"/>
44+
</joined-subclass>
45+
</joined-subclass>
46+
<joined-subclass name="Human">
47+
<key column="mammal"/>
48+
<component name="Name">
49+
<property name="First" column="name_first"/>
50+
<property name="Initial" column="name_initial"/>
51+
<property name="Last" column="name_last"/>
52+
</component>
53+
<property name="NickName"/>
54+
<property name="Height"/>
55+
56+
<property name="IntValue"/>
57+
<property name="FloatValue"/>
58+
<property name="BigDecimalValue"/>
59+
<property name="BigIntegerValue"/>
60+
61+
<bag name="Friends">
62+
<key column="human1"/>
63+
<many-to-many column="human2" class="Human"/>
64+
</bag>
65+
<map name="Family">
66+
<key column="human1"/>
67+
<map-key column="relationship" type="string"/>
68+
<many-to-many column="human2" class="Human"/>
69+
</map>
70+
<bag name="Pets" inverse="true">
71+
<key column="owner"/>
72+
<one-to-many class="DomesticAnimal"/>
73+
</bag>
74+
<set name="NickNames" lazy="false" table="human_nick_names" sort="natural">
75+
<key column="human"/>
76+
<element column="nick_name" type="string" not-null="true"/>
77+
</set>
78+
<map name="Addresses" table="addresses">
79+
<key column="human"/>
80+
<map-key type="string" column="type"/>
81+
<composite-element class="Address">
82+
<property name="Street"/>
83+
<property name="City"/>
84+
<property name="PostalCode"/>
85+
<property name="Country"/>
86+
<many-to-one name="StateProvince" column="state_prov_id" class="StateProvince"/>
87+
</composite-element>
88+
</map>
89+
</joined-subclass>
90+
</joined-subclass>
91+
</class>
92+
93+
<class name="User" table="`User`">
94+
<id name="Id">
95+
<generator class="foreign">
96+
<param name="property">human</param>
97+
</generator>
98+
</id>
99+
<property name="UserName"/>
100+
<one-to-one name="Human" constrained="true"/>
101+
<list name="Permissions">
102+
<key column="userId"/>
103+
<list-index column="permissionId"/>
104+
<element type="string" column="permissionName"/>
105+
</list>
106+
</class>
107+
108+
<class name="Zoo" discriminator-value="Z">
109+
<id name="Id">
110+
<generator class="native"/>
111+
</id>
112+
<discriminator column="zooType" type="character"/>
113+
<property name="Name" type="string"/>
114+
<property name="Classification"/>
115+
<map name="Mammals">
116+
<key column="mammalZoo_id"/>
117+
<index type="string" column="name"/>
118+
<one-to-many class="Mammal"/>
119+
</map>
120+
<map name="Animals" inverse="true">
121+
<key column="zoo_id"/>
122+
<index type="string" column="serialNumber"/>
123+
<one-to-many class="Animal"/>
124+
</map>
125+
<component name="Address" class="Address">
126+
<property name="Street"/>
127+
<property name="City"/>
128+
<property name="PostalCode"/>
129+
<property name="Country"/>
130+
<many-to-one name="StateProvince" column="state_prov_id" class="StateProvince"/>
131+
</component>
132+
<subclass name="PettingZoo" discriminator-value="P"/>
133+
</class>
134+
135+
<class name="StateProvince">
136+
<id name="Id">
137+
<generator class="native"/>
138+
</id>
139+
<property name="Name"/>
140+
<property name="IsoCode"/>
141+
</class>
142+
143+
<class name="Joiner">
144+
<id name="Id">
145+
<generator class="native"/>
146+
</id>
147+
<property name="Name"/>
148+
<join table="JOINED">
149+
<key column="ID"/>
150+
<property name="JoinedName"/>
151+
</join>
152+
</class>
153+
154+
</hibernate-mapping>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace NHibernate.Test.NHSpecificTest.NH3438.Domain
2+
{
3+
public enum Classification
4+
{
5+
Cool = 0,
6+
Lame = 1
7+
}
8+
}

0 commit comments

Comments
 (0)