Skip to content

Commit 39f5e6f

Browse files
NH-3488 - Documentation, modernizing tests, minor refactoring
* C#7 * White-spaces * Use of new ReflectionCache * ...
1 parent 153f5a0 commit 39f5e6f

Some content is hidden

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

52 files changed

+1436
-1572
lines changed

doc/reference/modules/batch.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ session.Close();]]></programlisting>
133133
(DML) statements: <literal>INSERT</literal>, <literal>UPDATE</literal>, <literal>DELETE</literal>)
134134
data directly in the database will not affect in-memory state. However, NHibernate provides methods
135135
for bulk SQL-style DML statement execution which are performed through the
136-
Hibernate Query Language (<link linkend="queryhql">HQL</link>).
136+
Hibernate Query Language (<link linkend="queryhql">HQL</link>). A
137+
<link linkend="querylinq-modifying">Linq implementation</link> is available too.
137138
</para>
138139

139140
<para>

doc/reference/modules/query_linq.xml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,101 @@ IList<Cat> oldCats =
467467
.ToList();]]></programlisting>
468468
</sect1>
469469

470+
<sect1 id="querylinq-modifying">
471+
<title>Modifying entities inside the database</title>
472+
473+
<para>
474+
Beginning with NHibernate 5.0, Linq queries can be used for inserting, updating or deleting entities.
475+
The query defines the data to delete, update or insert, and then <literal>Delete</literal>,
476+
<literal>Update</literal> and <literal>Insert</literal> queryable extension methods allow to delete it,
477+
or instruct in which way it should updated or inserted. Those queries happen entirely inside the
478+
database, without extracting corresponding entities out of the database.
479+
</para>
480+
<para>
481+
These operations are a Linq implementation of <xref linkend="batch-direct" />, with the same abilities
482+
and limitations.
483+
</para>
484+
485+
<sect2 id="querylinq-modifying-insert">
486+
<title>Inserting new entities</title>
487+
<para>
488+
<literal>Insert</literal> method extension expects a NHibernate queryable defining the data source of
489+
the insert. This data can be entities or a projection. Then it allows specifying the target entity type
490+
to insert, and how to convert source data to those target entities. Two forms of target specification
491+
exist.
492+
</para>
493+
<para>
494+
Using projection to target entity:
495+
</para>
496+
<programlisting><![CDATA[session.Query<Cat>()
497+
.Where(c => c.BodyWeight > 20)
498+
.Insert()
499+
.As(c => new Dog { Name = c.Name + "dog", BodyWeight = c.BodyWeight });]]></programlisting>
500+
<para>
501+
Or using assignments:
502+
</para>
503+
<programlisting><![CDATA[session.Query<Cat>()
504+
.Where(c => c.BodyWeight > 20)
505+
.Insert()
506+
.Into<Dog>(a => a
507+
.Set(d => d.Name, c => c.Name + "dog")
508+
.Set(d => d.BodyWeight, c => c.BodyWeight));]]></programlisting>
509+
<para>
510+
In both cases, unspecified properties are not included in the resulting SQL insert.
511+
<link linkend="mapping-declaration-version"><literal>version</literal></link> and
512+
<link linkend="mapping-declaration-timestamp"><literal>timestamp</literal></link> properties are
513+
exceptions. If not specified, they are inserted with their <literal>seed</literal> value.
514+
</para>
515+
<para>
516+
For more information on <literal>Insert</literal> limitations, please refer to
517+
<xref linkend="batch-direct" />.
518+
</para>
519+
</sect2>
520+
521+
<sect2 id="querylinq-modifying-update">
522+
<title>Updating entities</title>
523+
<para>
524+
<literal>Update</literal> method extension expects a queryable defining the entities to update.
525+
Then it allows specifying which properties should be updated with which values. As for
526+
<literal>Insert</literal>, two forms of target specification exist.
527+
</para>
528+
<para>
529+
Using projection to updated entity:
530+
</para>
531+
<programlisting><![CDATA[session.Query<Cat>()
532+
.Where(c => c.BodyWeight > 20)
533+
.Update()
534+
.As(c => new Cat { BodyWeight = c.BodyWeight / 2 });]]></programlisting>
535+
<para>
536+
Or using assignments:
537+
</para>
538+
<programlisting><![CDATA[session.Query<Cat>()
539+
.Where(c => c.BodyWeight > 20)
540+
.Update()
541+
.Assign(a => a
542+
.Set(c => c.BodyWeight, c => c.BodyWeight / 2));]]></programlisting>
543+
<para>
544+
In both cases, unspecified properties are not included in the resulting SQL update. This could
545+
be changed for <link linkend="mapping-declaration-version"><literal>version</literal></link> and
546+
<link linkend="mapping-declaration-timestamp"><literal>timestamp</literal></link> properties:
547+
<literal>As</literal> and <literal>Assign</literal> methods take an optional boolean parameter,
548+
<literal>versioned</literal>, which allows incrementing the version. Custom version types
549+
(<literal>NHibernate.Usertype.IUserVersionType</literal>) are not supported.
550+
</para>
551+
</sect2>
552+
553+
<sect2 id="querylinq-modifying-delete">
554+
<title>Deleting entities</title>
555+
<para>
556+
<literal>Delete</literal> method extension expects a queryable defining the entities to delete.
557+
It immediately deletes them.
558+
</para>
559+
<programlisting><![CDATA[session.Query<Cat>()
560+
.Where(c => c.BodyWeight > 20)
561+
.Delete();]]></programlisting>
562+
</sect2>
563+
</sect1>
564+
470565
<sect1 id="querylinq-querycache">
471566
<title>Query cache</title>
472567

src/NHibernate.Test/NHSpecificTest/NH3488/Domain/Address.cs renamed to src/NHibernate.Test/LinqBulkManipulation/Domain/Address.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace NHibernate.Test.NHSpecificTest.NH3488.Domain
1+
namespace NHibernate.Test.LinqBulkManipulation.Domain
22
{
33
public class Address
44
{

src/NHibernate.Test/NHSpecificTest/NH3488/Domain/Animal.cs renamed to src/NHibernate.Test/LinqBulkManipulation/Domain/Animal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Collections.Generic;
22

3-
namespace NHibernate.Test.NHSpecificTest.NH3488.Domain
3+
namespace NHibernate.Test.LinqBulkManipulation.Domain
44
{
55
public class Animal
66
{

src/NHibernate.Test/NHSpecificTest/NH3488/Domain/Animal.hbm.xml renamed to src/NHibernate.Test/LinqBulkManipulation/Domain/Animal.hbm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
33
assembly="NHibernate.Test"
4-
namespace="NHibernate.Test.NHSpecificTest.NH3488.Domain"
4+
namespace="NHibernate.Test.LinqBulkManipulation.Domain"
55
default-access="field.camelcase">
66

77
<class name="Animal">

src/NHibernate.Test/NHSpecificTest/NH3488/Domain/Classification.cs renamed to src/NHibernate.Test/LinqBulkManipulation/Domain/Classification.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace NHibernate.Test.NHSpecificTest.NH3488.Domain
1+
namespace NHibernate.Test.LinqBulkManipulation.Domain
22
{
33
public enum Classification
44
{

src/NHibernate.Test/NHSpecificTest/NH3488/Domain/CrazyCompositeKey.cs renamed to src/NHibernate.Test/LinqBulkManipulation/Domain/CrazyCompositeKey.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace NHibernate.Test.NHSpecificTest.NH3488.Domain
1+
namespace NHibernate.Test.LinqBulkManipulation.Domain
22
{
33
public class CrazyCompositeKey
44
{

src/NHibernate.Test/NHSpecificTest/NH3488/Domain/DomesticAnimal.cs renamed to src/NHibernate.Test/LinqBulkManipulation/Domain/DomesticAnimal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace NHibernate.Test.NHSpecificTest.NH3488.Domain
1+
namespace NHibernate.Test.LinqBulkManipulation.Domain
22
{
33
public class DomesticAnimal: Mammal
44
{

src/NHibernate.Test/NHSpecificTest/NH3488/Domain/EntityWithCrazyCompositeKey.cs renamed to src/NHibernate.Test/LinqBulkManipulation/Domain/EntityWithCrazyCompositeKey.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace NHibernate.Test.NHSpecificTest.NH3488.Domain
1+
namespace NHibernate.Test.LinqBulkManipulation.Domain
22
{
33
public class EntityWithCrazyCompositeKey
44
{
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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.LinqBulkManipulation.Domain">
5+
6+
<class name="EntityWithCrazyCompositeKey">
7+
<composite-id name="Id" class="CrazyCompositeKey">
8+
<key-property name="Id" column="id"/>
9+
<key-property name="OtherId" column="other_id"/>
10+
</composite-id>
11+
<property name="Name"/>
12+
</class>
13+
14+
<!-- Oracle and Firebird does not allow names longer than 30 characters.-->
15+
<class name="EntityReferencingEntityWithCrazyCompositeKey" table="EntRefEntWithCrazyCompKey">
16+
<id type="long" name="Id">
17+
<generator class="native" />
18+
</id>
19+
<property name="Name"/>
20+
<many-to-one name="Parent" class="EntityWithCrazyCompositeKey" foreign-key="none">
21+
<column name="Parent_id"/>
22+
<column name="Parent_OtherId"/>
23+
</many-to-one>
24+
</class>
25+
26+
</hibernate-mapping>

0 commit comments

Comments
 (0)