Skip to content

ISession.IsDirty() should not throw exception on new ManyToOne object in session #1416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions src/NHibernate/Async/Type/ManyToOneType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,37 +135,49 @@ private Task<object> AssembleIdAsync(object oid, ISessionImplementor session, Ca
}
}

public override async Task<bool> IsDirtyAsync(object old, object current, ISessionImplementor session, CancellationToken cancellationToken)
public override Task<bool> IsDirtyAsync(object old, object current, ISessionImplementor session, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (IsSame(old, current))
if (cancellationToken.IsCancellationRequested)
{
return false;
return Task.FromCanceled<bool>(cancellationToken);
}
return IsDirtyManyToOneAsync(old, current, null, session, cancellationToken);
}

object oldid = await (GetIdentifierAsync(old, session, cancellationToken)).ConfigureAwait(false);
object newid = await (GetIdentifierAsync(current, session, cancellationToken)).ConfigureAwait(false);
return await (GetIdentifierType(session).IsDirtyAsync(oldid, newid, session, cancellationToken)).ConfigureAwait(false);
public override Task<bool> IsDirtyAsync(object old, object current, bool[] checkable, ISessionImplementor session, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<bool>(cancellationToken);
}
return IsDirtyManyToOneAsync(old, current, IsAlwaysDirtyChecked ? null : checkable, session, cancellationToken);
}

public override async Task<bool> IsDirtyAsync(object old, object current, bool[] checkable, ISessionImplementor session, CancellationToken cancellationToken)
private async Task<bool> IsDirtyManyToOneAsync(object old, object current, bool[] checkable, ISessionImplementor session, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (IsAlwaysDirtyChecked)
if (IsSame(old, current))
{
return false;
}

if (old == null || current == null)
{
return await (IsDirtyAsync(old, current, session, cancellationToken)).ConfigureAwait(false);
return true;
}
else

if (await (ForeignKeys.IsTransientFastAsync(GetAssociatedEntityName(), current, session, cancellationToken)).ConfigureAwait(false) == true)
{
if (IsSame(old, current))
{
return false;
}

object oldid = await (GetIdentifierAsync(old, session, cancellationToken)).ConfigureAwait(false);
object newid = await (GetIdentifierAsync(current, session, cancellationToken)).ConfigureAwait(false);
return await (GetIdentifierType(session).IsDirtyAsync(oldid, newid, checkable, session, cancellationToken)).ConfigureAwait(false);
return true;
}

object oldid = await (GetIdentifierAsync(old, session, cancellationToken)).ConfigureAwait(false);
object newid = await (GetIdentifierAsync(current, session, cancellationToken)).ConfigureAwait(false);
IType identifierType = GetIdentifierType(session);

return checkable == null
? await (identifierType.IsDirtyAsync(oldid, newid, session, cancellationToken)).ConfigureAwait(false)
: await (identifierType.IsDirtyAsync(old, current, checkable, session, cancellationToken)).ConfigureAwait(false);
}
}
}
52 changes: 29 additions & 23 deletions src/NHibernate/Type/ManyToOneType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,35 +181,15 @@ public override bool IsAlwaysDirtyChecked

public override bool IsDirty(object old, object current, ISessionImplementor session)
{
if (IsSame(old, current))
{
return false;
}

object oldid = GetIdentifier(old, session);
object newid = GetIdentifier(current, session);
return GetIdentifierType(session).IsDirty(oldid, newid, session);
return IsDirtyManyToOne(old, current, null, session);
}

public override bool IsDirty(object old, object current, bool[] checkable, ISessionImplementor session)
{
if (IsAlwaysDirtyChecked)
{
return IsDirty(old, current, session);
}
else
{
if (IsSame(old, current))
{
return false;
}

object oldid = GetIdentifier(old, session);
object newid = GetIdentifier(current, session);
return GetIdentifierType(session).IsDirty(oldid, newid, checkable, session);
}
return IsDirtyManyToOne(old, current, IsAlwaysDirtyChecked ? null : checkable, session);
}


public override bool IsNullable
{
get { return ignoreNotFound; }
Expand All @@ -224,5 +204,31 @@ public override bool[] ToColumnNullness(object value, IMapping mapping)
}
return result;
}

private bool IsDirtyManyToOne(object old, object current, bool[] checkable, ISessionImplementor session)
{
if (IsSame(old, current))
{
return false;
}

if (old == null || current == null)
{
return true;
}

if (ForeignKeys.IsTransientFast(GetAssociatedEntityName(), current, session) == true)
{
return true;
}

object oldid = GetIdentifier(old, session);
object newid = GetIdentifier(current, session);
IType identifierType = GetIdentifierType(session);

return checkable == null
? identifierType.IsDirty(oldid, newid, session)
: identifierType.IsDirty(old, current, checkable, session);
}
}
}