/// <summary> /// Load an instance by a unique key that is not the primary key. /// </summary> /// <param name="entityName">The name of the entity to load </param> /// <param name="uniqueKeyPropertyName">The name of the property defining the unique key. </param> /// <param name="key">The unique key property value. </param> /// <param name="session">The originating session. </param> /// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param> /// <returns> The loaded entity </returns> public async Task <object> LoadByUniqueKeyAsync(string entityName, string uniqueKeyPropertyName, object key, ISessionImplementor session, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ISessionFactoryImplementor factory = session.Factory; IUniqueKeyLoadable persister = (IUniqueKeyLoadable)factory.GetEntityPersister(entityName); //TODO: implement caching?! proxies?! var keyType = GetIdentifierOrUniqueKeyType(factory) // EntityUniqueKey was doing this on the type. I suspect this was needed only for its usage in Loader, // which can work with entities as keys not yet instanciated and just represented by their identifiers. // But since removing this call from EntityUniqueKey is done for a patch and that the code path here has // no known bugs with this GetSemiResolvedType, moving its call here for avoiding altering this code // path. See GH1645. .GetSemiResolvedType(factory); EntityUniqueKey euk = new EntityUniqueKey( entityName, uniqueKeyPropertyName, key, keyType, session.Factory); IPersistenceContext persistenceContext = session.PersistenceContext; try { object result = persistenceContext.GetEntity(euk); if (result == null) { result = await(persister.LoadByUniqueKeyAsync(uniqueKeyPropertyName, key, session, cancellationToken)).ConfigureAwait(false); } return(result == null ? null : persistenceContext.ProxyFor(result)); } catch (OperationCanceledException) { throw; } catch (HibernateException) { // Do not call Convert on HibernateExceptions throw; } catch (Exception sqle) { throw ADOExceptionHelper.Convert(factory.SQLExceptionConverter, sqle, "Error performing LoadByUniqueKey"); } }
/// <summary> /// Load an instance by a unique key that is not the primary key. /// </summary> /// <param name="entityName">The name of the entity to load </param> /// <param name="uniqueKeyPropertyName">The name of the property defining the unique key. </param> /// <param name="key">The unique key property value. </param> /// <param name="session">The originating session. </param> /// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param> /// <returns> The loaded entity </returns> public async Task <object> LoadByUniqueKeyAsync(string entityName, string uniqueKeyPropertyName, object key, ISessionImplementor session, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ISessionFactoryImplementor factory = session.Factory; IUniqueKeyLoadable persister = (IUniqueKeyLoadable)factory.GetEntityPersister(entityName); //TODO: implement caching?! proxies?! var keyType = GetIdentifierOrUniqueKeyType(factory) .GetSemiResolvedType(factory); EntityUniqueKey euk = new EntityUniqueKey( entityName, uniqueKeyPropertyName, key, keyType, session.Factory); IPersistenceContext persistenceContext = session.PersistenceContext; try { object result = persistenceContext.GetEntity(euk); if (result == null) { result = await(persister.LoadByUniqueKeyAsync(uniqueKeyPropertyName, key, session, cancellationToken)).ConfigureAwait(false); if (result == null && !IsNullable) { factory.EntityNotFoundDelegate.HandleEntityNotFound(entityName, uniqueKeyPropertyName, key); } } return(result == null ? null : persistenceContext.ProxyFor(result)); } catch (OperationCanceledException) { throw; } catch (HibernateException) { // Do not call Convert on HibernateExceptions throw; } catch (Exception sqle) { throw ADOExceptionHelper.Convert(factory.SQLExceptionConverter, sqle, "Error performing LoadByUniqueKey"); } }
/// <summary> /// Load an instance by a unique key that is not the primary key. /// </summary> /// <param name="entityName">The name of the entity to load </param> /// <param name="uniqueKeyPropertyName">The name of the property defining the unique key. </param> /// <param name="key">The unique key property value. </param> /// <param name="session">The originating session. </param> /// <returns> The loaded entity </returns> public object LoadByUniqueKey(string entityName, string uniqueKeyPropertyName, object key, ISessionImplementor session) { ISessionFactoryImplementor factory = session.Factory; IUniqueKeyLoadable persister = (IUniqueKeyLoadable)factory.GetEntityPersister(entityName); //TODO: implement caching?! proxies?! var keyType = GetIdentifierOrUniqueKeyType(factory) .GetSemiResolvedType(factory); EntityUniqueKey euk = new EntityUniqueKey( entityName, uniqueKeyPropertyName, key, keyType, session.Factory); IPersistenceContext persistenceContext = session.PersistenceContext; try { object result = persistenceContext.GetEntity(euk); if (result == null) { result = persister.LoadByUniqueKey(uniqueKeyPropertyName, key, session); } return(result == null ? null : persistenceContext.ProxyFor(result)); } catch (HibernateException) { // Do not call Convert on HibernateExceptions throw; } catch (Exception sqle) { throw ADOExceptionHelper.Convert(factory.SQLExceptionConverter, sqle, "Error performing LoadByUniqueKey"); } }