Skip to content

Fix NoViableAltException in delete query #2691

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

Merged
merged 1 commit into from
Feb 28, 2021
Merged
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
17 changes: 17 additions & 0 deletions src/NHibernate.Test/Async/Hql/EntityJoinHqlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//------------------------------------------------------------------------------


using System;
using System.Text.RegularExpressions;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
Expand Down Expand Up @@ -233,6 +234,21 @@ public async Task NullablePropRefWhereIdEntityNotNullShouldAddJoinAsync()
}
}

[Test(Description = "GH-2688")]
public async Task NullableManyToOneDeleteQueryAsync()
{
using (var session = OpenSession())
{
await (session
.CreateQuery(
"delete "
+ "from NullableOwner ex "
+ "where ex.ManyToOne.id = :id"
).SetParameter("id", Guid.Empty)
.ExecuteUpdateAsync());
}
}

[Test]
public async Task NullableOneToOneFetchQueryIsNotAffectedAsync()
{
Expand Down Expand Up @@ -515,6 +531,7 @@ protected override HbmMapping GetMappings()
m.ForeignKey("none");
m.NotFound(NotFoundMode.Ignore);
});
rc.ManyToOne(e => e.ManyToOne, m => m.NotFound(NotFoundMode.Ignore));
});


Expand Down
19 changes: 18 additions & 1 deletion src/NHibernate.Test/Hql/EntityJoinHqlTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using System;
using System.Text.RegularExpressions;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NHibernate.Test.Hql.EntityJoinHqlTestEntities;
Expand Down Expand Up @@ -222,6 +223,21 @@ public void NullablePropRefWhereIdEntityNotNullShouldAddJoin()
}
}

[Test(Description = "GH-2688")]
public void NullableManyToOneDeleteQuery()
{
using (var session = OpenSession())
{
session
.CreateQuery(
"delete "
+ "from NullableOwner ex "
+ "where ex.ManyToOne.id = :id"
).SetParameter("id", Guid.Empty)
.ExecuteUpdate();
}
}

[Test]
public void NullableOneToOneFetchQueryIsNotAffected()
{
Expand Down Expand Up @@ -520,6 +536,7 @@ protected override HbmMapping GetMappings()
m.ForeignKey("none");
m.NotFound(NotFoundMode.Ignore);
});
rc.ManyToOne(e => e.ManyToOne, m => m.NotFound(NotFoundMode.Ignore));
});


Expand Down
1 change: 1 addition & 0 deletions src/NHibernate.Test/Hql/EntityJoinHqlTestEntities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class NullableOwner
public virtual string Name { get; set; }
public virtual OneToOneEntity OneToOne { get; set; }
public virtual PropRefEntity PropRef { get; set; }
public virtual OneToOneEntity ManyToOne { get; set; }
}

public class EntityWithCompositeId
Expand Down
6 changes: 4 additions & 2 deletions src/NHibernate/Hql/Ast/ANTLR/Tree/DotNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,9 @@ private void DereferenceEntity(EntityType entityType, bool implicitJoin, string
string property = _propertyName;
bool joinIsNeeded;

//For nullable entity comparisons we always need to add join (like not constrained one-to-one or not-found ignore associations)
//For nullable entity comparisons we always need to add join (like not constrained one-to-one or not-found ignore associations)
//NOTE: This fix is not fully correct. It doesn't work for comparisons with null (where e.OneToOneProp is null)
// as by default implicit join is generated and to work propelry left join is required (see GH-2611)
bool comparisonWithNullableEntity = false;

if ( IsDotNode( parent ) )
Expand All @@ -396,7 +398,7 @@ private void DereferenceEntity(EntityType entityType, bool implicitJoin, string
// entity's PK (because 'our' table would know the FK).
parentAsDotNode = ( DotNode ) parent;
property = parentAsDotNode._propertyName;
joinIsNeeded = generateJoin && (entityType.IsNullable || !IsReferenceToPrimaryKey( parentAsDotNode._propertyName, entityType ));
joinIsNeeded = generateJoin && ((Walker.IsSelectStatement && entityType.IsNullable) || !IsReferenceToPrimaryKey( parentAsDotNode._propertyName, entityType ));
}
else if ( ! Walker.IsSelectStatement )
{
Expand Down