apache / apache/grails-core

Checking Null on Relationship/nested Node using Criteria doesn't generate the correct CYPHER query

Open
#14,512 0 comments 0 reactions 0 assignees View on GitHub
relates-to: grails-data-hibernate5
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

Hi everyone,

I am having this issue when I try to check Null on Relationship/nested Node using Criteria.
I want my query returns the list of AssessmentSubControls where completedBy is null.
AssessmentSubControls and User are my domain objects using many to one relationship.

Here is AssessmentSubControls (I just show the relevant code):

```
class AssessmentSubControl {

static belongsTo = [
// AssessmentSubControl -> [IS_COMPLETED_BY] -> User
completedBy: User
...
]
static mapping = {
completedBy type: "IS_COMPLETED_BY", cascade: 'save-update'
...
}
static constraints = {
completedBy nullable: true
...
}
...
```

Nothing special on User domain.

Here is my criteria with the isNull check:
```
def assignedTasks = AssessmentSubControl.createCriteria().listDistinct() {
eq('assignedTo', user)
assessmentControl{
assessment{
eq('id', currentAssessment.id)
}
}
isNull('completedBy')
```
It generates the following CYPHER which is not correct because ```n.completedBy IS NULL``` expression checks on the field of ASSESSES_SUBCONTROL Node (which doesn't exist) when it should actually check the relationship.

```
MATCH (n:AssessmentSubControl), (n)-[:IS_ASSIGNED_TO]->(m_0:User), (n)<-[:ASSESSES_SUBCONTROL]-(m_1:AssessmentControl), (m_1)<-[:ASSESSES_CONTROL]-(m_2:Assessment) WHERE ( ID(m_0)=2403 AND n.completedBy IS NULL AND ( ( ID(m_2)=2414 ) ) )
OPTIONAL MATCH(n)-[r4:IS_COMPLETED_BY]->(completedByNode:User) WITH n, assignedByIds, assignedToIds, collect(DISTINCT ID(completedByNode)) as completedByIds
OPTIONAL MATCH(n)-[r6:IS_VALIDATED_BY]->(validatedByNode:User) WITH n, assignedByIds, assignedToIds, completedByIds, collect(DISTINCT ID(validatedByNode)) as validatedByIds
RETURN n as data, completedByIds, validatedByIds

```

Here is the query that gives me the expected result, using Not Exists on the relationship:
```
MATCH (asc:AssessmentSubControl),
(asc)<-[:ASSESSES_SUBCONTROL]-(ac:AssessmentControl),
(ac)<-[:ASSESSES_CONTROL]-(assessment:Assessment)
WHERE (
Not Exists((asc)-[:IS_COMPLETED_BY]->(:User))
)
RETURN asc,ac,assessment
```
Am I supposed to use notExists method instead of IsNull in the criteria? Not sure how my criteria query will look.

Let me know if you need more details.

Thanks,
Cyril

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the AssessmentSubControl.createCriteria() query and comparing its generated Cypher with the expected Not Exists relationship query shown in the issue. Trace the Criteria handling of isNull('completedBy') for nullable relationships; done when the generated query checks relationship absence rather than a nonexistent node property, with regression coverage for this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.