findById...AndXXX method derived query generates wrong statement
See original GitHub issueI am looking at some Spring Data Couchbase integration issues in JHIpster project, and one particular issue I found is that when we have a repository method to find by id and any other property, the generated query is wrong.
As an example, the method findByIdIsNotNullAndActivatedIsTrue
generates the following statement:
SELECT META(`testBucket`).id AS __id, META(`testBucket`).cas AS __cas, `testBucket`.* FROM `testBucket` WHERE `_class` = "tech.jhipster.sample.domain.User" AND id is not null and `activated`
In this scenario the Id should have meta(<bucket-name>
) prepended:
SELECT META(`testBucket`).id AS __id, META(`testBucket`).cas AS __cas, `testBucket`.* FROM `testBucket` WHERE `_class` = "tech.jhipster.sample.domain.User" AND META(`testBucket`).`id` is not null and `activated`
To support these type of query, I believe that N1qlQueryCreator.java
and QueryCriteria.java
classes needs to be modified.
QueryCriteria checks for backtics at start or end:
and if its not changed, it ends up adding backtics to the META: `META(`testBucket`).`id``
)
I did an attempt to support this type of queries here:
and even though it works, I am not sure if its the right place for that change. I can probably create a PR with the fix if needed.
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (1 by maintainers)
Edit: I see you’re way ahead of me. Yes - make a PR and I’ll merge. I had just one comment.
@mmonti - is it important/helpful to you to get quick fixes in 4.1.x? Or just before the next release (which is February 17)? We could merge a quick-fix now as long as we got in a proper fix before then.
For https://github.com/spring-projects/spring-data-couchbase/issues/1066 - the issue about always back-ticking the ‘key’ of a QueryCriteria - the fix would be to accept a N1qlExpression as the key of a QueryCriteria (as well as the current String) - and propagate that change all the way through to where the maybeBackTic() method is called (it would just be a toString() on the N1qlExpression). [the quick fix would be just to check for “META(” in the string, ignoring case]
For https://github.com/spring-projects/spring-data-couchbase/issues/1072 - The PartTree processing of META properties (id and cas and expiration). The catch is that an entity could have a property named id (or cas or expiration), that isn’t the actual id, cas or expiration. And always substituting those for META().id etc. would be incorrect. The id can be handled by isIdProperty() and CAS can be isVersionProperty() but I don’t have a solution for expiration (maybe require the entity to contain a field annotated with @\Expiration, similar to @\Id and @\Version? Someone was asking for that already). [ the quick fix that would work for your case would just to do what I did with isIdProperty() (and the same with isVersionProperty())]
https://github.com/spring-projects/spring-data-couchbase/pull/1081