forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathISession.cs
1105 lines (1007 loc) · 46.2 KB
/
ISession.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using NHibernate.Engine;
using NHibernate.Event;
using NHibernate.Event.Default;
using NHibernate.Impl;
using NHibernate.Multi;
using NHibernate.Stat;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate
{
// 6.0 TODO: Convert most of these extensions to interface methods
public static partial class SessionExtensions
{
/// <summary>
/// Obtain a <see cref="IStatelessSession"/> builder with the ability to grab certain information from
/// this session. The built <c>IStatelessSession</c> will require its own disposal.
/// </summary>
/// <param name="session">The session from which to build a stateless session.</param>
/// <returns>The session builder.</returns>
public static ISharedStatelessSessionBuilder StatelessSessionWithOptions(this ISession session)
{
var impl = session as SessionImpl ?? throw new NotSupportedException("Only SessionImpl sessions are supported.");
return impl.StatelessSessionWithOptions();
}
/// <summary>
/// Creates a <see cref="IQueryBatch"/> for the session. Batch extension methods are available in the
/// <c>NHibernate.Multi</c> namespace.
/// </summary>
/// <param name="session">The session.</param>
/// <returns>A query batch.</returns>
public static IQueryBatch CreateQueryBatch(this ISession session)
{
return ReflectHelper.CastOrThrow<AbstractSessionImpl>(session, "query batch").CreateQueryBatch();
}
// 6.0 TODO: consider if it should be added as a property on ISession then obsolete this, or if it should stay here as an extension method.
/// <summary>
/// Get the current transaction if any is ongoing, else <see langword="null" />.
/// </summary>
/// <param name="session">The session.</param>
/// <returns>The current transaction or <see langword="null" />..</returns>
public static ITransaction GetCurrentTransaction(this ISession session)
=> session.GetSessionImplementation().ConnectionManager.CurrentTransaction;
/// <summary>
/// Return the persistent instance of the given entity class with the given identifier, or null
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
/// already associated with the session, return that instance or proxy.)
/// </summary>
/// <param name="session">The session.</param>
/// <param name="entityName">The entity name.</param>
/// <param name="id">The entity identifier.</param>
/// <param name="lockMode">The lock mode to use for getting the entity.</param>
/// <returns>A persistent instance, or <see langword="null" />.</returns>
public static object Get(this ISession session, string entityName, object id, LockMode lockMode)
{
return
ReflectHelper
.CastOrThrow<SessionImpl>(session, "Get with entityName and lockMode")
.Get(entityName, id, lockMode);
}
//NOTE: Keep it as extension
/// <summary>
/// Return the persistent instance of the given entity name with the given identifier, or null
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
/// already associated with the session, return that instance or proxy.)
/// </summary>
/// <typeparam name="T">The entity class.</typeparam>
/// <param name="session">The session.</param>
/// <param name="entityName">The entity name.</param>
/// <param name="id">The entity identifier.</param>
/// <param name="lockMode">The lock mode to use for getting the entity.</param>
/// <returns>A persistent instance, or <see langword="null" />.</returns>
public static T Get<T>(this ISession session, string entityName, object id, LockMode lockMode)
{
return (T) session.Get(entityName, id, lockMode);
}
//NOTE: Keep it as extension
/// <summary>
/// Return the persistent instance of the given entity name with the given identifier, or null
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
/// already associated with the session, return that instance or proxy.)
/// </summary>
/// <typeparam name="T">The entity class.</typeparam>
/// <param name="session">The session.</param>
/// <param name="entityName">The entity name.</param>
/// <param name="id">The entity identifier.</param>
/// <returns>A persistent instance, or <see langword="null" />.</returns>
public static T Get<T>(this ISession session, string entityName, object id)
{
return (T) session.Get(entityName, id);
}
}
/// <summary>
/// The main runtime interface between a .NET application and NHibernate. This is the central
/// API class abstracting the notion of a persistence service.
/// </summary>
/// <remarks>
/// <para>
/// The lifecycle of a <c>ISession</c> is bounded by the beginning and end of a logical
/// transaction. (Long transactions might span several database transactions.)
/// </para>
/// <para>
/// The main function of the <c>ISession</c> is to offer create, find, update, and delete operations
/// for instances of mapped entity classes. Instances may exist in one of two states:
/// <list type="bullet">
/// <item>transient: not associated with any <c>ISession</c></item>
/// <item>persistent: associated with a <c>ISession</c></item>
/// </list>
/// </para>
/// <para>
/// Transient instances may be made persistent by calling <c>Save()</c>, <c>Insert()</c>,
/// or <c>Update()</c>. Persistent instances may be made transient by calling <c>Delete()</c>.
/// Any instance returned by a <c>List()</c>, <c>Enumerable()</c>, <c>Load()</c>, or <c>Create()</c>
/// method is persistent.
/// </para>
/// <para>
/// <c>Save()</c> results in an SQL <c>INSERT</c>, <c>Delete()</c>
/// in an SQL <c>DELETE</c> and <c>Update()</c> in an SQL <c>UPDATE</c>. Changes to
/// <em>persistent</em> instances are detected at flush time and also result in an SQL
/// <c>UPDATE</c>.
/// </para>
/// <para>
/// It is not intended that implementors be threadsafe. Instead each thread/transaction should obtain
/// its own instance from an <c>ISessionFactory</c>.
/// </para>
/// <para>
/// A <c>ISession</c> instance is serializable if its persistent classes are serializable
/// </para>
/// <para>
/// A typical transaction should use the following idiom:
/// <code>
/// using (ISession session = factory.OpenSession())
/// using (ITransaction tx = session.BeginTransaction())
/// {
/// try
/// {
/// // do some work
/// ...
/// tx.Commit();
/// }
/// catch (Exception e)
/// {
/// if (tx != null) tx.Rollback();
/// throw;
/// }
/// }
/// </code>
/// </para>
/// <para>
/// If the <c>ISession</c> throws an exception, the transaction must be rolled back and the session
/// discarded. The internal state of the <c>ISession</c> might not be consistent with the database
/// after the exception occurs.
/// </para>
/// <seealso cref="ISessionFactory"/>
/// </remarks>
public partial interface ISession : IDisposable
{
/// <summary>
/// Obtain a <see cref="ISession"/> builder with the ability to grab certain information from
/// this session. The built <c>ISession</c> will require its own flushes and disposal.
/// </summary>
/// <returns>The session builder.</returns>
ISharedSessionBuilder SessionWithOptions();
/// <summary>
/// Force the <c>ISession</c> to flush.
/// </summary>
/// <remarks>
/// Must be called at the end of a unit of work, before committing the transaction and closing
/// the session (<c>Transaction.Commit()</c> calls this method). <i>Flushing</i> is the process
/// of synchronizing the underlying persistent store with persistable state held in memory.
/// </remarks>
void Flush();
/// <summary>
/// Determines at which points Hibernate automatically flushes the session.
/// </summary>
/// <remarks>
/// For a readonly session, it is reasonable to set the flush mode to <c>FlushMode.Never</c>
/// at the start of the session (in order to achieve some extra performance).
/// </remarks>
FlushMode FlushMode { get; set; }
/// <summary> The current cache mode. </summary>
/// <remarks>
/// Cache mode determines the manner in which this session can interact with
/// the second level cache.
/// </remarks>
CacheMode CacheMode { get; set; }
/// <summary>
/// Get the <see cref="ISessionFactory" /> that created this instance.
/// </summary>
ISessionFactory SessionFactory { get; }
/// <summary>
/// Gets the ADO.NET connection.
/// </summary>
/// <remarks>
/// Applications are responsible for calling commit/rollback upon the connection before
/// closing the <c>ISession</c>.
/// </remarks>
DbConnection Connection { get; }
/// <summary>
/// Disconnect the <c>ISession</c> from the current ADO.NET connection.
/// </summary>
/// <remarks>
/// If the connection was obtained by Hibernate, close it or return it to the connection
/// pool. Otherwise return it to the application. This is used by applications which require
/// long transactions.
/// </remarks>
/// <returns>The connection provided by the application or <see langword="null" /></returns>
DbConnection Disconnect();
/// <summary>
/// Obtain a new ADO.NET connection.
/// </summary>
/// <remarks>
/// This is used by applications which require long transactions
/// </remarks>
void Reconnect();
/// <summary>
/// Reconnect to the given ADO.NET connection.
/// </summary>
/// <remarks>This is used by applications which require long transactions</remarks>
/// <param name="connection">An ADO.NET connection</param>
void Reconnect(DbConnection connection);
/// <summary>
/// End the <c>ISession</c> by disconnecting from the ADO.NET connection and cleaning up.
/// </summary>
/// <remarks>
/// It is not strictly necessary to <c>Close()</c> the <c>ISession</c> but you must
/// at least <c>Disconnect()</c> it.
/// </remarks>
/// <returns>The connection provided by the application or <see langword="null" /></returns>
DbConnection Close();
/// <summary>
/// Cancel execution of the current query.
/// </summary>
/// <remarks>
/// May be called from one thread to stop execution of a query in another thread.
/// Use with care!
/// </remarks>
void CancelQuery();
/// <summary>
/// Is the <c>ISession</c> still open?
/// </summary>
bool IsOpen { get; }
/// <summary>
/// Is the session connected?
/// </summary>
/// <value>
/// <see langword="true" /> if the session is connected.
/// </value>
/// <remarks>
/// A session is considered connected if there is a <see cref="DbConnection"/> (regardless
/// of its state) or if the field <c>connect</c> is true. Meaning that it will connect
/// at the next operation that requires a connection.
/// </remarks>
bool IsConnected { get; }
/// <summary>
/// Does this <c>ISession</c> contain any changes which must be
/// synchronized with the database? Would any SQL be executed if
/// we flushed this session? May trigger save cascades, which could
/// cause themselves some SQL to be executed, especially if the
/// <c>identity</c> id generator is used.
/// </summary>
/// <remarks>
/// <para>
/// The default implementation first checks if it contains saved or deleted entities to be flushed. If not, it
/// then delegate the check to its <see cref="IDirtyCheckEventListener" />, which by default is
/// <see cref="DefaultDirtyCheckEventListener" />.
/// </para>
/// <para>
/// <see cref="DefaultDirtyCheckEventListener" /> replicates all the beginning of the flush process, checking
/// dirtiness of entities loaded in the session and triggering their pending cascade operations in order to
/// detect new and removed children. This can have the side effect of performing the <see cref="Save(object)"/>
/// of children, causing their id to be generated. Depending on their id generator, this can trigger calls to
/// the database and even actually insert them if using an <c>identity</c> generator.
/// </para>
/// </remarks>
bool IsDirty();
/// <summary>
/// Is the specified entity (or proxy) read-only?
/// </summary>
/// <remarks>
/// Facade for <see cref="IPersistenceContext.IsReadOnly(object)" />.
/// </remarks>
/// <param name="entityOrProxy">An entity (or <see cref="NHibernate.Proxy.INHibernateProxy" />)</param>
/// <returns>
/// <c>true</c> if the entity (or proxy) is read-only, otherwise <c>false</c>.
/// </returns>
/// <seealso cref="ISession.DefaultReadOnly" />
/// <seealso cref="ISession.SetReadOnly(object, bool)" />
bool IsReadOnly(object entityOrProxy);
/// <summary>
/// Change the read-only status of an entity (or proxy).
/// </summary>
/// <remarks>
/// <para>
/// Read-only entities can be modified, but changes are not persisted. They are not dirty-checked
/// and snapshots of persistent state are not maintained.
/// </para>
/// <para>
/// Immutable entities cannot be made read-only.
/// </para>
/// <para>
/// To set the <em>default</em> read-only setting for entities and proxies that are loaded
/// into the session, see <see cref="ISession.DefaultReadOnly" />.
/// </para>
/// <para>
/// This method a facade for <see cref="IPersistenceContext.SetReadOnly(object, bool)" />.
/// </para>
/// </remarks>
/// <param name="entityOrProxy">An entity (or <see cref="NHibernate.Proxy.INHibernateProxy" />).</param>
/// <param name="readOnly">If <c>true</c>, the entity or proxy is made read-only; if <c>false</c>, it is made modifiable.</param>
/// <seealso cref="ISession.DefaultReadOnly" />
/// <seealso cref="ISession.IsReadOnly(object)" />
void SetReadOnly(object entityOrProxy, bool readOnly);
/// <summary>
/// The read-only status for entities (and proxies) loaded into this Session.
/// </summary>
/// <remarks>
/// <para>
/// When a proxy is initialized, the loaded entity will have the same read-only setting
/// as the uninitialized proxy, regardless of the session's current setting.
/// </para>
/// <para>
/// To change the read-only setting for a particular entity or proxy that is already in
/// this session, see <see cref="ISession.SetReadOnly(object, bool)" />.
/// </para>
/// <para>
/// To override this session's read-only setting for entities and proxies loaded by a query,
/// see <see cref="IQuery.SetReadOnly(bool)" />.
/// </para>
/// <para>
/// This method is a facade for <see cref="IPersistenceContext.DefaultReadOnly" />.
/// </para>
/// </remarks>
/// <seealso cref="ISession.IsReadOnly(object)" />
/// <seealso cref="ISession.SetReadOnly(object, bool)" />
bool DefaultReadOnly { get; set; }
/// <summary>
/// Return the identifier of an entity instance cached by the <c>ISession</c>
/// </summary>
/// <remarks>
/// Throws an exception if the instance is transient or associated with a different
/// <c>ISession</c>
/// </remarks>
/// <param name="obj">a persistent instance</param>
/// <returns>the identifier</returns>
object GetIdentifier(object obj);
/// <summary>
/// Is this instance associated with this Session?
/// </summary>
/// <param name="obj">an instance of a persistent class</param>
/// <returns>true if the given instance is associated with this Session</returns>
bool Contains(object obj);
/// <summary>
/// Remove this instance from the session cache.
/// </summary>
/// <remarks>
/// Changes to the instance will not be synchronized with the database.
/// This operation cascades to associated instances if the association is mapped
/// with <c>cascade="all"</c> or <c>cascade="all-delete-orphan"</c>.
/// </remarks>
/// <param name="obj">a persistent instance</param>
void Evict(Object obj);
/// <summary>
/// Return the persistent instance of the given entity class with the given identifier,
/// obtaining the specified lock mode.
/// </summary>
/// <param name="theType">A persistent class</param>
/// <param name="id">A valid identifier of an existing persistent instance of the class</param>
/// <param name="lockMode">The lock level</param>
/// <returns>the persistent instance</returns>
object Load(System.Type theType, object id, LockMode lockMode);
/// <summary>
/// Return the persistent instance of the given entity class with the given identifier,
/// obtaining the specified lock mode, assuming the instance exists.
/// </summary>
/// <param name="entityName">The entity-name of a persistent class</param>
/// <param name="id">a valid identifier of an existing persistent instance of the class </param>
/// <param name="lockMode">the lock level </param>
/// <returns> the persistent instance or proxy </returns>
object Load(string entityName, object id, LockMode lockMode);
/// <summary>
/// Return the persistent instance of the given entity class with the given identifier,
/// assuming that the instance exists.
/// </summary>
/// <remarks>
/// You should not use this method to determine if an instance exists (use a query or
/// <see cref="Get(System.Type, object)" /> instead). Use this only to retrieve an instance
/// that you assume exists, where non-existence would be an actual error.
/// </remarks>
/// <param name="theType">A persistent class</param>
/// <param name="id">A valid identifier of an existing persistent instance of the class</param>
/// <returns>The persistent instance or proxy</returns>
object Load(System.Type theType, object id);
/// <summary>
/// Return the persistent instance of the given entity class with the given identifier,
/// obtaining the specified lock mode.
/// </summary>
/// <typeparam name="T">A persistent class</typeparam>
/// <param name="id">A valid identifier of an existing persistent instance of the class</param>
/// <param name="lockMode">The lock level</param>
/// <returns>the persistent instance</returns>
T Load<T>(object id, LockMode lockMode);
/// <summary>
/// Return the persistent instance of the given entity class with the given identifier,
/// assuming that the instance exists.
/// </summary>
/// <remarks>
/// You should not use this method to determine if an instance exists (use a query or
/// <see cref="Get{T}(object)" /> instead). Use this only to retrieve an instance that you
/// assume exists, where non-existence would be an actual error.
/// </remarks>
/// <typeparam name="T">A persistent class</typeparam>
/// <param name="id">A valid identifier of an existing persistent instance of the class</param>
/// <returns>The persistent instance or proxy</returns>
T Load<T>(object id);
/// <summary>
/// Return the persistent instance of the given <paramref name="entityName"/> with the given identifier,
/// assuming that the instance exists.
/// </summary>
/// <param name="entityName">The entity-name of a persistent class</param>
/// <param name="id">a valid identifier of an existing persistent instance of the class </param>
/// <returns> The persistent instance or proxy </returns>
/// <remarks>
/// You should not use this method to determine if an instance exists (use <see cref="Get(string,object)"/>
/// instead). Use this only to retrieve an instance that you assume exists, where non-existence
/// would be an actual error.
/// </remarks>
object Load(string entityName, object id);
/// <summary>
/// Read the persistent state associated with the given identifier into the given transient
/// instance.
/// </summary>
/// <param name="obj">An "empty" instance of the persistent class</param>
/// <param name="id">A valid identifier of an existing persistent instance of the class</param>
void Load(object obj, object id);
/// <summary>
/// Persist all reachable transient objects, reusing the current identifier
/// values. Note that this will not trigger the Interceptor of the Session.
/// </summary>
/// <param name="obj">a detached instance of a persistent class</param>
/// <param name="replicationMode"></param>
void Replicate(object obj, ReplicationMode replicationMode);
/// <summary>
/// Persist the state of the given detached instance, reusing the current
/// identifier value. This operation cascades to associated instances if
/// the association is mapped with <tt>cascade="replicate"</tt>.
/// </summary>
/// <param name="entityName"></param>
/// <param name="obj">a detached instance of a persistent class </param>
/// <param name="replicationMode"></param>
void Replicate(string entityName, object obj, ReplicationMode replicationMode);
/// <summary>
/// Persist the given transient instance, first assigning a generated identifier.
/// </summary>
/// <remarks>
/// Save will use the current value of the identifier property if the <c>Assigned</c>
/// generator is used.
/// </remarks>
/// <param name="obj">A transient instance of a persistent class</param>
/// <returns>The generated identifier</returns>
object Save(object obj);
/// <summary>
/// Persist the given transient instance, using the given identifier.
/// </summary>
/// <param name="obj">A transient instance of a persistent class</param>
/// <param name="id">An unused valid identifier</param>
void Save(object obj, object id);
/// <summary>
/// Persist the given transient instance, first assigning a generated identifier. (Or
/// using the current value of the identifier property if the <tt>assigned</tt>
/// generator is used.)
/// </summary>
/// <param name="entityName">The Entity name.</param>
/// <param name="obj">a transient instance of a persistent class </param>
/// <returns> the generated identifier </returns>
/// <remarks>
/// This operation cascades to associated instances if the
/// association is mapped with <tt>cascade="save-update"</tt>.
/// </remarks>
object Save(string entityName, object obj);
/// <summary>
/// Persist the given transient instance, using the given identifier.
/// </summary>
/// <param name="entityName">The Entity name.</param>
/// <param name="obj">a transient instance of a persistent class </param>
/// <param name="id">An unused valid identifier</param>
/// <remarks>
/// This operation cascades to associated instances if the
/// association is mapped with <tt>cascade="save-update"</tt>.
/// </remarks>
void Save(string entityName, object obj, object id);
/// <summary>
/// Either <c>Save()</c> or <c>Update()</c> the given instance, depending upon the value of
/// its identifier property.
/// </summary>
/// <remarks>
/// By default the instance is always saved. This behaviour may be adjusted by specifying
/// an <c>unsaved-value</c> attribute of the identifier property mapping
/// </remarks>
/// <param name="obj">A transient instance containing new or updated state</param>
void SaveOrUpdate(object obj);
/// <summary>
/// Either <see cref="Save(String,Object)"/> or <see cref="Update(String,Object)"/>
/// the given instance, depending upon resolution of the unsaved-value checks
/// (see the manual for discussion of unsaved-value checking).
/// </summary>
/// <param name="entityName">The name of the entity </param>
/// <param name="obj">a transient or detached instance containing new or updated state </param>
/// <seealso cref="ISession.Save(String,Object)"/>
/// <seealso cref="ISession.Update(String,Object)"/>
/// <remarks>
/// This operation cascades to associated instances if the association is mapped
/// with <tt>cascade="save-update"</tt>.
/// </remarks>
void SaveOrUpdate(string entityName, object obj);
/// <summary>
/// Either <c>Save()</c> or <c>Update()</c> the given instance, depending upon the value of
/// its identifier property.
/// </summary>
/// <remarks>
/// By default the instance is always saved. This behaviour may be adjusted by specifying
/// an <c>unsaved-value</c> attribute of the identifier property mapping
/// </remarks>
/// <param name="entityName">The name of the entity</param>
/// <param name="obj">A transient instance containing new or updated state</param>
/// <param name="id">Identifier of persistent instance</param>
void SaveOrUpdate(string entityName, object obj, object id);
/// <summary>
/// Update the persistent instance with the identifier of the given transient instance.
/// </summary>
/// <remarks>
/// If there is a persistent instance with the same identifier, an exception is thrown. If
/// the given transient instance has a <see langword="null" /> identifier, an exception will be thrown.
/// </remarks>
/// <param name="obj">A transient instance containing updated state</param>
void Update(object obj);
/// <summary>
/// Update the persistent state associated with the given identifier.
/// </summary>
/// <remarks>
/// An exception is thrown if there is a persistent instance with the same identifier
/// in the current session.
/// </remarks>
/// <param name="obj">A transient instance containing updated state</param>
/// <param name="id">Identifier of persistent instance</param>
void Update(object obj, object id);
/// <summary>
/// Update the persistent instance with the identifier of the given detached
/// instance.
/// </summary>
/// <param name="entityName">The Entity name.</param>
/// <param name="obj">a detached instance containing updated state </param>
/// <remarks>
/// If there is a persistent instance with the same identifier,
/// an exception is thrown. This operation cascades to associated instances
/// if the association is mapped with <tt>cascade="save-update"</tt>.
/// </remarks>
void Update(string entityName, object obj);
/// <summary>
/// Update the persistent instance associated with the given identifier.
/// </summary>
/// <param name="entityName">The Entity name.</param>
/// <param name="obj">a detached instance containing updated state </param>
/// <param name="id">Identifier of persistent instance</param>
/// <remarks>
/// If there is a persistent instance with the same identifier,
/// an exception is thrown. This operation cascades to associated instances
/// if the association is mapped with <tt>cascade="save-update"</tt>.
/// </remarks>
void Update(string entityName, object obj, object id);
/// <summary>
/// Copy the state of the given object onto the persistent object with the same
/// identifier. If there is no persistent instance currently associated with
/// the session, it will be loaded. Return the persistent instance. If the
/// given instance is unsaved, save a copy of and return it as a newly persistent
/// instance. The given instance does not become associated with the session.
/// This operation cascades to associated instances if the association is mapped
/// with <tt>cascade="merge"</tt>.<br/>
/// The semantics of this method are defined by JSR-220.
/// </summary>
/// <param name="obj">a detached instance with state to be copied </param>
/// <returns> an updated persistent instance </returns>
object Merge(object obj);
/// <summary>
/// Copy the state of the given object onto the persistent object with the same
/// identifier. If there is no persistent instance currently associated with
/// the session, it will be loaded. Return the persistent instance. If the
/// given instance is unsaved, save a copy of and return it as a newly persistent
/// instance. The given instance does not become associated with the session.
/// This operation cascades to associated instances if the association is mapped
/// with <tt>cascade="merge"</tt>.<br/>
/// The semantics of this method are defined by JSR-220.
/// </summary>
/// <param name="entityName">Name of the entity.</param>
/// <param name="obj">a detached instance with state to be copied </param>
/// <returns> an updated persistent instance </returns>
object Merge(string entityName, object obj);
/// <summary>
/// Copy the state of the given object onto the persistent object with the same
/// identifier. If there is no persistent instance currently associated with
/// the session, it will be loaded. Return the persistent instance. If the
/// given instance is unsaved, save a copy of and return it as a newly persistent
/// instance. The given instance does not become associated with the session.
/// This operation cascades to associated instances if the association is mapped
/// with <tt>cascade="merge"</tt>.<br/>
/// The semantics of this method are defined by JSR-220.
/// </summary>
/// <param name="entity">a detached instance with state to be copied </param>
/// <returns> an updated persistent instance </returns>
T Merge<T>(T entity) where T : class;
/// <summary>
/// Copy the state of the given object onto the persistent object with the same
/// identifier. If there is no persistent instance currently associated with
/// the session, it will be loaded. Return the persistent instance. If the
/// given instance is unsaved, save a copy of and return it as a newly persistent
/// instance. The given instance does not become associated with the session.
/// This operation cascades to associated instances if the association is mapped
/// with <tt>cascade="merge"</tt>.<br/>
/// The semantics of this method are defined by JSR-220.
/// </summary>
/// <param name="entityName">Name of the entity.</param>
/// <param name="entity">a detached instance with state to be copied </param>
/// <returns> an updated persistent instance </returns>
T Merge<T>(string entityName, T entity) where T : class;
/// <summary>
/// Make a transient instance persistent. This operation cascades to associated
/// instances if the association is mapped with <tt>cascade="persist"</tt>.<br/>
/// The semantics of this method are defined by JSR-220.
/// </summary>
/// <param name="obj">a transient instance to be made persistent </param>
void Persist(object obj);
/// <summary>
/// Make a transient instance persistent. This operation cascades to associated
/// instances if the association is mapped with <tt>cascade="persist"</tt>.<br/>
/// The semantics of this method are defined by JSR-220.
/// </summary>
/// <param name="entityName">Name of the entity.</param>
/// <param name="obj">a transient instance to be made persistent</param>
void Persist(string entityName, object obj);
/// <summary>
/// Remove a persistent instance from the datastore.
/// </summary>
/// <remarks>
/// The argument may be an instance associated with the receiving <c>ISession</c> or a
/// transient instance with an identifier associated with existing persistent state.
/// </remarks>
/// <param name="obj">The instance to be removed</param>
void Delete(object obj);
/// <summary>
/// Remove a persistent instance from the datastore. The <b>object</b> argument may be
/// an instance associated with the receiving <see cref="ISession"/> or a transient
/// instance with an identifier associated with existing persistent state.
/// This operation cascades to associated instances if the association is mapped
/// with <tt>cascade="delete"</tt>.
/// </summary>
/// <param name="entityName">The entity name for the instance to be removed. </param>
/// <param name="obj">the instance to be removed </param>
void Delete(string entityName, object obj);
/// <summary>
/// Delete all objects returned by the query.
/// </summary>
/// <param name="query">The query string</param>
/// <returns>Returns the number of objects deleted.</returns>
int Delete(string query);
/// <summary>
/// Delete all objects returned by the query.
/// </summary>
/// <param name="query">The query string</param>
/// <param name="value">A value to be written to a "?" placeholer in the query</param>
/// <param name="type">The hibernate type of value.</param>
/// <returns>The number of instances deleted</returns>
int Delete(string query, object value, IType type);
/// <summary>
/// Delete all objects returned by the query.
/// </summary>
/// <param name="query">The query string</param>
/// <param name="values">A list of values to be written to "?" placeholders in the query</param>
/// <param name="types">A list of Hibernate types of the values</param>
/// <returns>The number of instances deleted</returns>
int Delete(string query, object[] values, IType[] types);
/// <summary>
/// Obtain the specified lock level upon the given object.
/// </summary>
/// <param name="obj">A persistent instance</param>
/// <param name="lockMode">The lock level</param>
void Lock(object obj, LockMode lockMode);
/// <summary>
/// Obtain the specified lock level upon the given object.
/// </summary>
/// <param name="entityName">The Entity name.</param>
/// <param name="obj">a persistent or transient instance </param>
/// <param name="lockMode">the lock level </param>
/// <remarks>
/// This may be used to perform a version check (<see cref="LockMode.Read"/>), to upgrade to a pessimistic
/// lock (<see cref="LockMode.Upgrade"/>), or to simply reassociate a transient instance
/// with a session (<see cref="LockMode.None"/>). This operation cascades to associated
/// instances if the association is mapped with <tt>cascade="lock"</tt>.
/// </remarks>
void Lock(string entityName, object obj, LockMode lockMode);
/// <summary>
/// Re-read the state of the given instance from the underlying database.
/// </summary>
/// <remarks>
/// <para>
/// It is inadvisable to use this to implement long-running sessions that span many
/// business tasks. This method is, however, useful in certain special circumstances.
/// </para>
/// <para>
/// For example,
/// <list>
/// <item>Where a database trigger alters the object state upon insert or update</item>
/// <item>After executing direct SQL (eg. a mass update) in the same session</item>
/// <item>After inserting a <c>Blob</c> or <c>Clob</c></item>
/// </list>
/// </para>
/// </remarks>
/// <param name="obj">A persistent instance</param>
void Refresh(object obj);
/// <summary>
/// Re-read the state of the given instance from the underlying database, with
/// the given <c>LockMode</c>.
/// </summary>
/// <remarks>
/// It is inadvisable to use this to implement long-running sessions that span many
/// business tasks. This method is, however, useful in certain special circumstances.
/// </remarks>
/// <param name="obj">a persistent or transient instance</param>
/// <param name="lockMode">the lock mode to use</param>
void Refresh(object obj, LockMode lockMode);
/// <summary>
/// Determine the current lock mode of the given object
/// </summary>
/// <param name="obj">A persistent instance</param>
/// <returns>The current lock mode</returns>
LockMode GetCurrentLockMode(object obj);
/// <summary>
/// Begin a unit of work and return the associated <c>ITransaction</c> object.
/// </summary>
/// <remarks>
/// If a new underlying transaction is required, begin the transaction. Otherwise
/// continue the new work in the context of the existing underlying transaction.
/// The class of the returned <see cref="ITransaction" /> object is determined by
/// the property <c>transaction_factory</c>
/// </remarks>
/// <returns>A transaction instance</returns>
ITransaction BeginTransaction();
/// <summary>
/// Begin a transaction with the specified <c>isolationLevel</c>
/// </summary>
/// <param name="isolationLevel">Isolation level for the new transaction</param>
/// <returns>A transaction instance having the specified isolation level</returns>
ITransaction BeginTransaction(IsolationLevel isolationLevel);
/// <summary>
/// Get the current Unit of Work and return the associated <c>ITransaction</c> object.
/// </summary>
// Since v5.3
[Obsolete("Use GetCurrentTransaction extension method instead, and check for null.")]
ITransaction Transaction { get; }
/// <summary>
/// Join the <see cref="System.Transactions.Transaction.Current"/> system transaction.
/// </summary>
/// <remarks>
/// <para>
/// Sessions auto-join current transaction by default on their first usage within a scope.
/// This can be disabled with <see cref="ISessionBuilder{T}.AutoJoinTransaction(bool)"/> from
/// a session builder obtained with <see cref="ISessionFactory.WithOptions()"/>, or with the
/// auto-join transaction configuration setting.
/// </para>
/// <para>
/// This method allows to explicitly join the current transaction. It does nothing if it is already
/// joined.
/// </para>
/// </remarks>
/// <exception cref="HibernateException">Thrown if there is no current transaction.</exception>
void JoinTransaction();
/// <summary>
/// Creates a new <c>Criteria</c> for the entity class.
/// </summary>
/// <typeparam name="T">The entity class</typeparam>
/// <returns>An ICriteria object</returns>
ICriteria CreateCriteria<T>() where T : class;
/// <summary>
/// Creates a new <c>Criteria</c> for the entity class with a specific alias
/// </summary>
/// <typeparam name="T">The entity class</typeparam>
/// <param name="alias">The alias of the entity</param>
/// <returns>An ICriteria object</returns>
ICriteria CreateCriteria<T>(string alias) where T : class;
/// <summary>
/// Creates a new <c>Criteria</c> for the entity class.
/// </summary>
/// <param name="persistentClass">The class to Query</param>
/// <returns>An ICriteria object</returns>
ICriteria CreateCriteria(System.Type persistentClass);
/// <summary>
/// Creates a new <c>Criteria</c> for the entity class with a specific alias
/// </summary>
/// <param name="persistentClass">The class to Query</param>
/// <param name="alias">The alias of the entity</param>
/// <returns>An ICriteria object</returns>
ICriteria CreateCriteria(System.Type persistentClass, string alias);
/// <summary>
/// Create a new <c>Criteria</c> instance, for the given entity name.
/// </summary>
/// <param name="entityName">The name of the entity to Query</param>
/// <returns>An ICriteria object</returns>
ICriteria CreateCriteria(string entityName);
/// <summary>
/// Create a new <c>Criteria</c> instance, for the given entity name,
/// with the given alias.
/// </summary>
/// <param name="entityName">The name of the entity to Query</param>
/// <param name="alias">The alias of the entity</param>
/// <returns>An ICriteria object</returns>
ICriteria CreateCriteria(string entityName, string alias);
/// <summary>
/// Creates a new <c>IQueryOver<T></c> for the entity class.
/// </summary>
/// <typeparam name="T">The entity class</typeparam>
/// <returns>An IQueryOver<T> object</returns>
IQueryOver<T, T> QueryOver<T>() where T : class;
/// <summary>
/// Creates a new <c>IQueryOver<T></c> for the entity class.
/// </summary>
/// <typeparam name="T">The entity class</typeparam>
/// <param name="alias">The alias of the entity</param>
/// <returns>An IQueryOver<T> object</returns>
IQueryOver<T, T> QueryOver<T>(Expression<Func<T>> alias) where T : class;
/// <summary>
/// Creates a new <c>IQueryOver{T};</c> for the entity class.
/// </summary>
/// <typeparam name="T">The entity class</typeparam>
/// <param name="entityName">The name of the entity to Query</param>
/// <returns>An IQueryOver{T} object</returns>
IQueryOver<T, T> QueryOver<T>(string entityName) where T : class;
/// <summary>
/// Creates a new <c>IQueryOver{T}</c> for the entity class.
/// </summary>
/// <typeparam name="T">The entity class</typeparam>
/// <param name="entityName">The name of the entity to Query</param>
/// <param name="alias">The alias of the entity</param>
/// <returns>An IQueryOver{T} object</returns>
IQueryOver<T, T> QueryOver<T>(string entityName, Expression<Func<T>> alias) where T : class;
/// <summary>
/// Create a new instance of <c>Query</c> for the given query string
/// </summary>
/// <param name="queryString">A hibernate query string</param>
/// <returns>The query</returns>
IQuery CreateQuery(string queryString);
/// <summary>
/// Create a new instance of <c>Query</c> for the given collection and filter string
/// </summary>
/// <param name="collection">A persistent collection</param>
/// <param name="queryString">A hibernate query</param>
/// <returns>A query</returns>
IQuery CreateFilter(object collection, string queryString);
/// <summary>
/// Obtain an instance of <see cref="IQuery" /> for a named query string defined in the
/// mapping file.
/// </summary>
/// <param name="queryName">The name of a query defined externally.</param>
/// <returns>An <see cref="IQuery"/> from a named query string.</returns>
/// <remarks>
/// The query can be either in <c>HQL</c> or <c>SQL</c> format.
/// </remarks>
IQuery GetNamedQuery(string queryName);
/// <summary>
/// Create a new instance of <see cref="ISQLQuery" /> for the given SQL query string.
/// </summary>
/// <param name="queryString">a query expressed in SQL</param>
/// <returns>An <see cref="ISQLQuery"/> from the SQL string</returns>
ISQLQuery CreateSQLQuery(string queryString);
/// <summary>
/// Completely clear the session. Evict all loaded instances and cancel all pending
/// saves, updates and deletions. Do not close open enumerables or instances of
/// <c>ScrollableResults</c>.
/// </summary>
void Clear();
/// <summary>
/// Return the persistent instance of the given entity class with the given identifier, or null
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
/// already associated with the session, return that instance or proxy.)
/// </summary>
/// <param name="clazz">a persistent class</param>
/// <param name="id">an identifier</param>
/// <returns>a persistent instance or null</returns>
object Get(System.Type clazz, object id);
/// <summary>
/// Return the persistent instance of the given entity class with the given identifier, or null
/// if there is no such persistent instance. Obtain the specified lock mode if the instance
/// exists.
/// </summary>
/// <param name="clazz">a persistent class</param>
/// <param name="id">an identifier</param>
/// <param name="lockMode">the lock mode</param>
/// <returns>a persistent instance or null</returns>
object Get(System.Type clazz, object id, LockMode lockMode);
/// <summary>
/// Return the persistent instance of the given named entity with the given identifier,
/// or null if there is no such persistent instance. (If the instance, or a proxy for the
/// instance, is already associated with the session, return that instance or proxy.)
/// </summary>
/// <param name="entityName">the entity name </param>
/// <param name="id">an identifier </param>
/// <returns> a persistent instance or null </returns>
object Get(string entityName, object id);
/// <summary>
/// Strongly-typed version of <see cref="Get(System.Type, object)" />
/// </summary>
T Get<T>(object id);