Skip to content

Commit 1d8fb9c

Browse files
authoredMay 25, 2020
Add missing ISession.Get(entityName, id, lockMode) (#2378)
1 parent 40a6d0d commit 1d8fb9c

File tree

10 files changed

+300
-125
lines changed

10 files changed

+300
-125
lines changed
 

‎src/NHibernate.Test/Async/NHSpecificTest/NH1275/Fixture.cs

+7
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ public async Task RetrievingAsync()
5353
string sql = sqlLogSpy.Appender.GetEvents()[0].RenderedMessage;
5454
Assert.That(sql.IndexOf(Dialect.ForUpdateString, StringComparison.Ordinal), Is.GreaterThan(0));
5555
}
56+
s.Clear();
57+
using (SqlLogSpy sqlLogSpy = new SqlLogSpy())
58+
{
59+
await (s.GetAsync<A>(typeof(A).FullName, savedId, LockMode.Upgrade));
60+
string sql = sqlLogSpy.Appender.GetEvents()[0].RenderedMessage;
61+
Assert.That(sql.IndexOf(Dialect.ForUpdateString, StringComparison.Ordinal), Is.GreaterThan(0));
62+
}
5663
using (SqlLogSpy sqlLogSpy = new SqlLogSpy())
5764
{
5865
await (s.CreateQuery("from A a where a.Id= :pid").SetLockMode("a", LockMode.Upgrade).SetParameter("pid", savedId).

‎src/NHibernate.Test/NHSpecificTest/NH1275/Fixture.cs

+7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ public void Retrieving()
4242
string sql = sqlLogSpy.Appender.GetEvents()[0].RenderedMessage;
4343
Assert.That(sql.IndexOf(Dialect.ForUpdateString, StringComparison.Ordinal), Is.GreaterThan(0));
4444
}
45+
s.Clear();
46+
using (SqlLogSpy sqlLogSpy = new SqlLogSpy())
47+
{
48+
s.Get<A>(typeof(A).FullName, savedId, LockMode.Upgrade);
49+
string sql = sqlLogSpy.Appender.GetEvents()[0].RenderedMessage;
50+
Assert.That(sql.IndexOf(Dialect.ForUpdateString, StringComparison.Ordinal), Is.GreaterThan(0));
51+
}
4552
using (SqlLogSpy sqlLogSpy = new SqlLogSpy())
4653
{
4754
s.CreateQuery("from A a where a.Id= :pid").SetLockMode("a", LockMode.Upgrade).SetParameter("pid", savedId).

‎src/NHibernate/Async/ISession.cs

+70
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,76 @@ namespace NHibernate
2626
{
2727
using System.Threading.Tasks;
2828
using System.Threading;
29+
public static partial class SessionExtensions
30+
{
31+
32+
/// <summary>
33+
/// Return the persistent instance of the given entity class with the given identifier, or null
34+
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
35+
/// already associated with the session, return that instance or proxy.)
36+
/// </summary>
37+
/// <param name="session">The session.</param>
38+
/// <param name="entityName">The entity name.</param>
39+
/// <param name="id">The entity identifier.</param>
40+
/// <param name="lockMode">The lock mode to use for getting the entity.</param>
41+
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
42+
/// <returns>A persistent instance, or <see langword="null" />.</returns>
43+
public static Task<object> GetAsync(this ISession session, string entityName, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
44+
{
45+
if (cancellationToken.IsCancellationRequested)
46+
{
47+
return Task.FromCanceled<object>(cancellationToken);
48+
}
49+
try
50+
{
51+
return
52+
ReflectHelper
53+
.CastOrThrow<SessionImpl>(session, "Get with entityName and lockMode")
54+
.GetAsync(entityName, id, lockMode, cancellationToken);
55+
}
56+
catch (Exception ex)
57+
{
58+
return Task.FromException<object>(ex);
59+
}
60+
}
61+
62+
//NOTE: Keep it as extension
63+
/// <summary>
64+
/// Return the persistent instance of the given entity name with the given identifier, or null
65+
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
66+
/// already associated with the session, return that instance or proxy.)
67+
/// </summary>
68+
/// <typeparam name="T">The entity class.</typeparam>
69+
/// <param name="session">The session.</param>
70+
/// <param name="entityName">The entity name.</param>
71+
/// <param name="id">The entity identifier.</param>
72+
/// <param name="lockMode">The lock mode to use for getting the entity.</param>
73+
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
74+
/// <returns>A persistent instance, or <see langword="null" />.</returns>
75+
public static async Task<T> GetAsync<T>(this ISession session, string entityName, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
76+
{
77+
cancellationToken.ThrowIfCancellationRequested();
78+
return (T) await (session.GetAsync(entityName, id, lockMode, cancellationToken)).ConfigureAwait(false);
79+
}
80+
81+
//NOTE: Keep it as extension
82+
/// <summary>
83+
/// Return the persistent instance of the given entity name with the given identifier, or null
84+
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
85+
/// already associated with the session, return that instance or proxy.)
86+
/// </summary>
87+
/// <typeparam name="T">The entity class.</typeparam>
88+
/// <param name="session">The session.</param>
89+
/// <param name="entityName">The entity name.</param>
90+
/// <param name="id">The entity identifier.</param>
91+
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
92+
/// <returns>A persistent instance, or <see langword="null" />.</returns>
93+
public static async Task<T> GetAsync<T>(this ISession session, string entityName, object id, CancellationToken cancellationToken = default(CancellationToken))
94+
{
95+
cancellationToken.ThrowIfCancellationRequested();
96+
return (T) await (session.GetAsync(entityName, id, cancellationToken)).ConfigureAwait(false);
97+
}
98+
}
2999

30100
public partial interface ISession : IDisposable
31101
{

‎src/NHibernate/Async/IStatelessSession.cs

+40
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,46 @@ namespace NHibernate
2222
{
2323
using System.Threading.Tasks;
2424
using System.Threading;
25+
public static partial class StatelessSessionExtensions
26+
{
27+
28+
//NOTE: Keep it as extension
29+
/// <summary>
30+
/// Return the persistent instance of the given entity name with the given identifier, or null
31+
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
32+
/// already associated with the session, return that instance or proxy.)
33+
/// </summary>
34+
/// <typeparam name="T">The entity class.</typeparam>
35+
/// <param name="session">The session.</param>
36+
/// <param name="entityName">The entity name.</param>
37+
/// <param name="id">The entity identifier.</param>
38+
/// <param name="lockMode">The lock mode to use for getting the entity.</param>
39+
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
40+
/// <returns>A persistent instance, or <see langword="null" />.</returns>
41+
public static async Task<T> GetAsync<T>(this IStatelessSession session, string entityName, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
42+
{
43+
cancellationToken.ThrowIfCancellationRequested();
44+
return (T) await (session.GetAsync(entityName, id, lockMode, cancellationToken)).ConfigureAwait(false);
45+
}
46+
47+
//NOTE: Keep it as extension
48+
/// <summary>
49+
/// Return the persistent instance of the given entity name with the given identifier, or null
50+
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
51+
/// already associated with the session, return that instance or proxy.)
52+
/// </summary>
53+
/// <typeparam name="T">The entity class.</typeparam>
54+
/// <param name="session">The session.</param>
55+
/// <param name="entityName">The entity name.</param>
56+
/// <param name="id">The entity identifier.</param>
57+
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
58+
/// <returns>A persistent instance, or <see langword="null" />.</returns>
59+
public static async Task<T> GetAsync<T>(this IStatelessSession session, string entityName, object id, CancellationToken cancellationToken = default(CancellationToken))
60+
{
61+
cancellationToken.ThrowIfCancellationRequested();
62+
return (T) await (session.GetAsync(entityName, id, cancellationToken)).ConfigureAwait(false);
63+
}
64+
}
2565

2666
public partial interface IStatelessSession : IDisposable
2767
{

‎src/NHibernate/Async/Impl/SessionImpl.cs

+40-42
Original file line numberDiff line numberDiff line change
@@ -803,24 +803,21 @@ public override async Task<bool> AutoFlushIfRequiredAsync(ISet<string> querySpac
803803
return LoadAsync(entityClass.FullName, id, cancellationToken);
804804
}
805805

806+
/// <inheritdoc />
806807
public async Task<T> GetAsync<T>(object id, CancellationToken cancellationToken = default(CancellationToken))
807808
{
808809
cancellationToken.ThrowIfCancellationRequested();
809-
using (BeginProcess())
810-
{
811-
return (T)await (GetAsync(typeof(T), id, cancellationToken)).ConfigureAwait(false);
812-
}
810+
return (T) await (GetAsync(typeof(T), id, cancellationToken)).ConfigureAwait(false);
813811
}
814812

813+
/// <inheritdoc />
815814
public async Task<T> GetAsync<T>(object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
816815
{
817816
cancellationToken.ThrowIfCancellationRequested();
818-
using (BeginProcess())
819-
{
820-
return (T)await (GetAsync(typeof(T), id, lockMode, cancellationToken)).ConfigureAwait(false);
821-
}
817+
return (T) await (GetAsync(typeof(T), id, lockMode, cancellationToken)).ConfigureAwait(false);
822818
}
823819

820+
/// <inheritdoc />
824821
public Task<object> GetAsync(System.Type entityClass, object id, CancellationToken cancellationToken = default(CancellationToken))
825822
{
826823
if (cancellationToken.IsCancellationRequested)
@@ -830,29 +827,50 @@ public override async Task<bool> AutoFlushIfRequiredAsync(ISet<string> querySpac
830827
return GetAsync(entityClass.FullName, id, cancellationToken);
831828
}
832829

833-
/// <summary>
834-
/// Load the data for the object with the specified id into a newly created object
835-
/// using "for update", if supported. A new key will be assigned to the object.
836-
/// This should return an existing proxy where appropriate.
837-
///
838-
/// If the object does not exist in the database, null is returned.
839-
/// </summary>
840-
/// <param name="clazz"></param>
841-
/// <param name="id"></param>
842-
/// <param name="lockMode"></param>
843-
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
844-
/// <returns></returns>
845-
public async Task<object> GetAsync(System.Type clazz, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
830+
/// <inheritdoc />
831+
public Task<object> GetAsync(System.Type clazz, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
832+
{
833+
if (cancellationToken.IsCancellationRequested)
834+
{
835+
return Task.FromCanceled<object>(cancellationToken);
836+
}
837+
return GetAsync(clazz.FullName, id, lockMode, cancellationToken);
838+
}
839+
840+
/// <inheritdoc />
841+
public async Task<object> GetAsync(string entityName, object id, LockMode lockMode, CancellationToken cancellationToken)
846842
{
847843
cancellationToken.ThrowIfCancellationRequested();
848844
using (BeginProcess())
849845
{
850-
LoadEvent loadEvent = new LoadEvent(id, clazz.FullName, lockMode, this);
846+
LoadEvent loadEvent = new LoadEvent(id, entityName, lockMode, this);
851847
await (FireLoadAsync(loadEvent, LoadEventListener.Get, cancellationToken)).ConfigureAwait(false);
848+
//Note: AfterOperation call is skipped to avoid releasing the lock when outside of a transaction.
852849
return loadEvent.Result;
853850
}
854851
}
855852

853+
/// <inheritdoc />
854+
public async Task<object> GetAsync(string entityName, object id, CancellationToken cancellationToken = default(CancellationToken))
855+
{
856+
cancellationToken.ThrowIfCancellationRequested();
857+
using (BeginProcess())
858+
{
859+
LoadEvent loadEvent = new LoadEvent(id, entityName, null, this);
860+
bool success = false;
861+
try
862+
{
863+
await (FireLoadAsync(loadEvent, LoadEventListener.Get, cancellationToken)).ConfigureAwait(false);
864+
success = true;
865+
return loadEvent.Result;
866+
}
867+
finally
868+
{
869+
await (AfterOperationAsync(success, cancellationToken)).ConfigureAwait(false);
870+
}
871+
}
872+
}
873+
856874
public async Task<string> GetEntityNameAsync(object obj, CancellationToken cancellationToken = default(CancellationToken))
857875
{
858876
cancellationToken.ThrowIfCancellationRequested();
@@ -882,26 +900,6 @@ public override async Task<bool> AutoFlushIfRequiredAsync(ISet<string> querySpac
882900
}
883901
}
884902

885-
public async Task<object> GetAsync(string entityName, object id, CancellationToken cancellationToken = default(CancellationToken))
886-
{
887-
cancellationToken.ThrowIfCancellationRequested();
888-
using (BeginProcess())
889-
{
890-
LoadEvent loadEvent = new LoadEvent(id, entityName, false, this);
891-
bool success = false;
892-
try
893-
{
894-
await (FireLoadAsync(loadEvent, LoadEventListener.Get, cancellationToken)).ConfigureAwait(false);
895-
success = true;
896-
return loadEvent.Result;
897-
}
898-
finally
899-
{
900-
await (AfterOperationAsync(success, cancellationToken)).ConfigureAwait(false);
901-
}
902-
}
903-
}
904-
905903
/// <summary>
906904
/// Load the data for the object with the specified id into a newly created object.
907905
/// This is only called when lazily initializing a proxy.

‎src/NHibernate/Async/Impl/StatelessSessionImpl.cs

+8-23
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ public async Task ManagedFlushAsync(CancellationToken cancellationToken)
439439
}
440440
}
441441

442-
/// <summary> Retrieve a entity. </summary>
442+
/// <summary> Retrieve an entity. </summary>
443443
/// <returns> a detached entity instance </returns>
444444
public Task<object> GetAsync(string entityName, object id, CancellationToken cancellationToken = default(CancellationToken))
445445
{
@@ -450,39 +450,27 @@ public async Task ManagedFlushAsync(CancellationToken cancellationToken)
450450
return GetAsync(entityName, id, LockMode.None, cancellationToken);
451451
}
452452

453-
/// <summary> Retrieve a entity.
454-
///
453+
/// <summary>
454+
/// Retrieve an entity.
455455
/// </summary>
456456
/// <returns> a detached entity instance
457457
/// </returns>
458458
public async Task<T> GetAsync<T>(object id, CancellationToken cancellationToken = default(CancellationToken))
459459
{
460460
cancellationToken.ThrowIfCancellationRequested();
461-
using (BeginProcess())
462-
{
463-
return (T)await (GetAsync(typeof(T), id, cancellationToken)).ConfigureAwait(false);
464-
}
465-
}
466-
467-
private Task<object> GetAsync(System.Type persistentClass, object id, CancellationToken cancellationToken)
468-
{
469-
if (cancellationToken.IsCancellationRequested)
470-
{
471-
return Task.FromCanceled<object>(cancellationToken);
472-
}
473-
return GetAsync(persistentClass.FullName, id, cancellationToken);
461+
return (T) await (GetAsync(typeof(T).FullName, id, cancellationToken)).ConfigureAwait(false);
474462
}
475463

476464
/// <summary>
477-
/// Retrieve a entity, obtaining the specified lock mode.
465+
/// Retrieve an entity, obtaining the specified lock mode.
478466
/// </summary>
479467
/// <returns> a detached entity instance </returns>
480468
public async Task<object> GetAsync(string entityName, object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
481469
{
482470
cancellationToken.ThrowIfCancellationRequested();
483471
using (BeginProcess())
484472
{
485-
object result = await (Factory.GetEntityPersister(entityName).LoadAsync(id, null, lockMode, this, cancellationToken)).ConfigureAwait(false);
473+
object result = await (Factory.GetEntityPersister(entityName).LoadAsync(id, null, lockMode ?? LockMode.None, this, cancellationToken)).ConfigureAwait(false);
486474
if (temporaryPersistenceContext.IsLoadFinished)
487475
{
488476
temporaryPersistenceContext.Clear();
@@ -492,16 +480,13 @@ private Task<object> GetAsync(System.Type persistentClass, object id, Cancellati
492480
}
493481

494482
/// <summary>
495-
/// Retrieve a entity, obtaining the specified lock mode.
483+
/// Retrieve an entity, obtaining the specified lock mode.
496484
/// </summary>
497485
/// <returns> a detached entity instance </returns>
498486
public async Task<T> GetAsync<T>(object id, LockMode lockMode, CancellationToken cancellationToken = default(CancellationToken))
499487
{
500488
cancellationToken.ThrowIfCancellationRequested();
501-
using (BeginProcess())
502-
{
503-
return (T)await (GetAsync(typeof(T).FullName, id, lockMode, cancellationToken)).ConfigureAwait(false);
504-
}
489+
return (T) await (GetAsync(typeof(T).FullName, id, lockMode, cancellationToken)).ConfigureAwait(false);
505490
}
506491

507492
/// <summary>

‎src/NHibernate/ISession.cs

+52-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace NHibernate
1616
{
1717
// 6.0 TODO: Convert most of these extensions to interface methods
18-
public static class SessionExtensions
18+
public static partial class SessionExtensions
1919
{
2020
/// <summary>
2121
/// Obtain a <see cref="IStatelessSession"/> builder with the ability to grab certain information from
@@ -48,6 +48,57 @@ public static IQueryBatch CreateQueryBatch(this ISession session)
4848
/// <returns>The current transaction or <see langword="null" />..</returns>
4949
public static ITransaction GetCurrentTransaction(this ISession session)
5050
=> session.GetSessionImplementation().ConnectionManager.CurrentTransaction;
51+
52+
/// <summary>
53+
/// Return the persistent instance of the given entity class with the given identifier, or null
54+
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
55+
/// already associated with the session, return that instance or proxy.)
56+
/// </summary>
57+
/// <param name="session">The session.</param>
58+
/// <param name="entityName">The entity name.</param>
59+
/// <param name="id">The entity identifier.</param>
60+
/// <param name="lockMode">The lock mode to use for getting the entity.</param>
61+
/// <returns>A persistent instance, or <see langword="null" />.</returns>
62+
public static object Get(this ISession session, string entityName, object id, LockMode lockMode)
63+
{
64+
return
65+
ReflectHelper
66+
.CastOrThrow<SessionImpl>(session, "Get with entityName and lockMode")
67+
.Get(entityName, id, lockMode);
68+
}
69+
70+
//NOTE: Keep it as extension
71+
/// <summary>
72+
/// Return the persistent instance of the given entity name with the given identifier, or null
73+
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
74+
/// already associated with the session, return that instance or proxy.)
75+
/// </summary>
76+
/// <typeparam name="T">The entity class.</typeparam>
77+
/// <param name="session">The session.</param>
78+
/// <param name="entityName">The entity name.</param>
79+
/// <param name="id">The entity identifier.</param>
80+
/// <param name="lockMode">The lock mode to use for getting the entity.</param>
81+
/// <returns>A persistent instance, or <see langword="null" />.</returns>
82+
public static T Get<T>(this ISession session, string entityName, object id, LockMode lockMode)
83+
{
84+
return (T) session.Get(entityName, id, lockMode);
85+
}
86+
87+
//NOTE: Keep it as extension
88+
/// <summary>
89+
/// Return the persistent instance of the given entity name with the given identifier, or null
90+
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
91+
/// already associated with the session, return that instance or proxy.)
92+
/// </summary>
93+
/// <typeparam name="T">The entity class.</typeparam>
94+
/// <param name="session">The session.</param>
95+
/// <param name="entityName">The entity name.</param>
96+
/// <param name="id">The entity identifier.</param>
97+
/// <returns>A persistent instance, or <see langword="null" />.</returns>
98+
public static T Get<T>(this ISession session, string entityName, object id)
99+
{
100+
return (T) session.Get(entityName, id);
101+
}
51102
}
52103

53104
/// <summary>

‎src/NHibernate/IStatelessSession.cs

+34-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace NHibernate
1212
{
1313
// 6.0 TODO: Convert most of these extensions to interface methods
14-
public static class StatelessSessionExtensions
14+
public static partial class StatelessSessionExtensions
1515
{
1616
/// <summary>
1717
/// Creates a <see cref="IQueryBatch"/> for the session.
@@ -31,6 +31,39 @@ public static IQueryBatch CreateQueryBatch(this IStatelessSession session)
3131
/// <returns>The current transaction or <see langword="null" />..</returns>
3232
public static ITransaction GetCurrentTransaction(this IStatelessSession session)
3333
=> session.GetSessionImplementation().ConnectionManager.CurrentTransaction;
34+
35+
//NOTE: Keep it as extension
36+
/// <summary>
37+
/// Return the persistent instance of the given entity name with the given identifier, or null
38+
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
39+
/// already associated with the session, return that instance or proxy.)
40+
/// </summary>
41+
/// <typeparam name="T">The entity class.</typeparam>
42+
/// <param name="session">The session.</param>
43+
/// <param name="entityName">The entity name.</param>
44+
/// <param name="id">The entity identifier.</param>
45+
/// <param name="lockMode">The lock mode to use for getting the entity.</param>
46+
/// <returns>A persistent instance, or <see langword="null" />.</returns>
47+
public static T Get<T>(this IStatelessSession session, string entityName, object id, LockMode lockMode)
48+
{
49+
return (T) session.Get(entityName, id, lockMode);
50+
}
51+
52+
//NOTE: Keep it as extension
53+
/// <summary>
54+
/// Return the persistent instance of the given entity name with the given identifier, or null
55+
/// if there is no such persistent instance. (If the instance, or a proxy for the instance, is
56+
/// already associated with the session, return that instance or proxy.)
57+
/// </summary>
58+
/// <typeparam name="T">The entity class.</typeparam>
59+
/// <param name="session">The session.</param>
60+
/// <param name="entityName">The entity name.</param>
61+
/// <param name="id">The entity identifier.</param>
62+
/// <returns>A persistent instance, or <see langword="null" />.</returns>
63+
public static T Get<T>(this IStatelessSession session, string entityName, object id)
64+
{
65+
return (T) session.Get(entityName, id);
66+
}
3467
}
3568

3669
/// <summary>

‎src/NHibernate/Impl/SessionImpl.cs

+34-39
Original file line numberDiff line numberDiff line change
@@ -1156,48 +1156,62 @@ public object Load(System.Type entityClass, object id)
11561156
return Load(entityClass.FullName, id);
11571157
}
11581158

1159+
/// <inheritdoc />
11591160
public T Get<T>(object id)
11601161
{
1161-
using (BeginProcess())
1162-
{
1163-
return (T)Get(typeof(T), id);
1164-
}
1162+
return (T) Get(typeof(T), id);
11651163
}
11661164

1165+
/// <inheritdoc />
11671166
public T Get<T>(object id, LockMode lockMode)
11681167
{
1169-
using (BeginProcess())
1170-
{
1171-
return (T)Get(typeof(T), id, lockMode);
1172-
}
1168+
return (T) Get(typeof(T), id, lockMode);
11731169
}
11741170

1171+
/// <inheritdoc />
11751172
public object Get(System.Type entityClass, object id)
11761173
{
11771174
return Get(entityClass.FullName, id);
11781175
}
11791176

1180-
/// <summary>
1181-
/// Load the data for the object with the specified id into a newly created object
1182-
/// using "for update", if supported. A new key will be assigned to the object.
1183-
/// This should return an existing proxy where appropriate.
1184-
///
1185-
/// If the object does not exist in the database, null is returned.
1186-
/// </summary>
1187-
/// <param name="clazz"></param>
1188-
/// <param name="id"></param>
1189-
/// <param name="lockMode"></param>
1190-
/// <returns></returns>
1177+
/// <inheritdoc />
11911178
public object Get(System.Type clazz, object id, LockMode lockMode)
1179+
{
1180+
return Get(clazz.FullName, id, lockMode);
1181+
}
1182+
1183+
/// <inheritdoc />
1184+
public object Get(string entityName, object id, LockMode lockMode)
11921185
{
11931186
using (BeginProcess())
11941187
{
1195-
LoadEvent loadEvent = new LoadEvent(id, clazz.FullName, lockMode, this);
1188+
LoadEvent loadEvent = new LoadEvent(id, entityName, lockMode, this);
11961189
FireLoad(loadEvent, LoadEventListener.Get);
1190+
//Note: AfterOperation call is skipped to avoid releasing the lock when outside of a transaction.
11971191
return loadEvent.Result;
11981192
}
11991193
}
12001194

1195+
/// <inheritdoc />
1196+
public object Get(string entityName, object id)
1197+
{
1198+
using (BeginProcess())
1199+
{
1200+
LoadEvent loadEvent = new LoadEvent(id, entityName, null, this);
1201+
bool success = false;
1202+
try
1203+
{
1204+
FireLoad(loadEvent, LoadEventListener.Get);
1205+
success = true;
1206+
return loadEvent.Result;
1207+
}
1208+
finally
1209+
{
1210+
AfterOperation(success);
1211+
}
1212+
}
1213+
}
1214+
12011215
public string GetEntityName(object obj)
12021216
{
12031217
using (BeginProcess())
@@ -1226,25 +1240,6 @@ public string GetEntityName(object obj)
12261240
}
12271241
}
12281242

1229-
public object Get(string entityName, object id)
1230-
{
1231-
using (BeginProcess())
1232-
{
1233-
LoadEvent loadEvent = new LoadEvent(id, entityName, false, this);
1234-
bool success = false;
1235-
try
1236-
{
1237-
FireLoad(loadEvent, LoadEventListener.Get);
1238-
success = true;
1239-
return loadEvent.Result;
1240-
}
1241-
finally
1242-
{
1243-
AfterOperation(success);
1244-
}
1245-
}
1246-
}
1247-
12481243
/// <summary>
12491244
/// Load the data for the object with the specified id into a newly created object.
12501245
/// This is only called when lazily initializing a proxy.

‎src/NHibernate/Impl/StatelessSessionImpl.cs

+8-19
Original file line numberDiff line numberDiff line change
@@ -539,40 +539,32 @@ public void Delete(string entityName, object entity)
539539
}
540540
}
541541

542-
/// <summary> Retrieve a entity. </summary>
542+
/// <summary> Retrieve an entity. </summary>
543543
/// <returns> a detached entity instance </returns>
544544
public object Get(string entityName, object id)
545545
{
546546
return Get(entityName, id, LockMode.None);
547547
}
548548

549-
/// <summary> Retrieve a entity.
550-
///
549+
/// <summary>
550+
/// Retrieve an entity.
551551
/// </summary>
552552
/// <returns> a detached entity instance
553553
/// </returns>
554554
public T Get<T>(object id)
555555
{
556-
using (BeginProcess())
557-
{
558-
return (T)Get(typeof(T), id);
559-
}
560-
}
561-
562-
private object Get(System.Type persistentClass, object id)
563-
{
564-
return Get(persistentClass.FullName, id);
556+
return (T) Get(typeof(T).FullName, id);
565557
}
566558

567559
/// <summary>
568-
/// Retrieve a entity, obtaining the specified lock mode.
560+
/// Retrieve an entity, obtaining the specified lock mode.
569561
/// </summary>
570562
/// <returns> a detached entity instance </returns>
571563
public object Get(string entityName, object id, LockMode lockMode)
572564
{
573565
using (BeginProcess())
574566
{
575-
object result = Factory.GetEntityPersister(entityName).Load(id, null, lockMode, this);
567+
object result = Factory.GetEntityPersister(entityName).Load(id, null, lockMode ?? LockMode.None, this);
576568
if (temporaryPersistenceContext.IsLoadFinished)
577569
{
578570
temporaryPersistenceContext.Clear();
@@ -582,15 +574,12 @@ public object Get(string entityName, object id, LockMode lockMode)
582574
}
583575

584576
/// <summary>
585-
/// Retrieve a entity, obtaining the specified lock mode.
577+
/// Retrieve an entity, obtaining the specified lock mode.
586578
/// </summary>
587579
/// <returns> a detached entity instance </returns>
588580
public T Get<T>(object id, LockMode lockMode)
589581
{
590-
using (BeginProcess())
591-
{
592-
return (T)Get(typeof(T).FullName, id, lockMode);
593-
}
582+
return (T) Get(typeof(T).FullName, id, lockMode);
594583
}
595584

596585
/// <summary>

0 commit comments

Comments
 (0)
Please sign in to comment.