Skip to content

Commit a21b93c

Browse files
fredericDelaportehazzik
authored andcommitted
NH-3488 - Documentation, modernizing tests, minor refactoring
* C#7 * White-spaces * Use of new ReflectionCache * ...
1 parent d15f3cb commit a21b93c

Some content is hidden

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

53 files changed

+1590
-1643
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

0 commit comments

Comments
 (0)