JPA Criteria in clause with tuple expression - jpa

Looking to translate an HQL query into it's JPA Criteria API equivalent.
In HQL I have the following which seems to work.
WHERE (x, y) IN (
... sub query which selects two columns
)
In JPA Criteria API I don't see how I can match a tuple value, only a single value. For example, I know how to convert the following to JPA Criteria.
WHERE (x) IN (
... sub query which selects single column
)
Is this even possible using the criteria api?

It's not possible in JPQL to try to match two fields with the IN.
By the way, you can't do it in JPA Criteria neither.

Related

Entity Framework core: sum of sum doesn't work

I'm trying to use this query using EF core:
entity.OrderBy(e => e.FieldOne.Sum(fo => fo.NestedField.Sum(nf => nf.SecondNestedField)));
Error:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery. Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
Is it possible to rewrite it EF core way?
It's sound not possible to call a complex expression in Sum. But you can flat the list and do a simple Sum like :
entity.OrderBy(e.FieldOne.SelectMany(fo => fo.NestedField).Sum(nf => nf.SecondNestedField));

Do I need to separate a 'whereIn' and a field value that can contain two values in firestore?

I'm querying two fields in a document. I'm using the whereIn(that's what it's called in flutter, for web it's called in) operator for one field. The other field I need to query is a string. And I want to query this value against two values. Do I need to separate the queries or is there a way to do the full query in a single query..
According to the Firestore documentation on query limitations:
You can use only one in or array-contains-any clause per query. You can't use both in and array-contains-any in the same query.
Since you need two whereIn/in clauses, you need to execute a separate query for each value in the second whereIn and then merge the results in your application code.

Query jsonb column in Postgres against multiple values

I have a column 'carInfo' of type jsonbin my PostgreSQL database and I want to be able to query against multiple values. I want the query to return only those rows that match all criteria. For example, if I had multiple columns, I would write the query like this:
select *
from car
where name = 'BMW'
AND 'year' = 2020
Since my column is of type jsonb I can make use of the containment operator (#>) like this:
select *
from car
where carInfo #> cast('{"name":"BMW"}') as jsonb
AND carInfo #> cast('{"year":"2020"}')
but I want the query to be dynamic, so that the user can query by any attributes they want.
So, they will send a list of search terms (in this case, the elements of that list would be {"name":"BMW"} and {"year":"2020"}.
Assuming, that I have the list as above, how would the query look like if I wanted to achieve the same result as when using the AND operator?
I tried it like this:
select *
from car
where carInfo #> any(array['{"name":"BMW"}', '{"year":"2020"}']::jsonb[])
but it acts the same way as when using the OR operator. I need to find those rows that contain BOTH the search terms
You could use #> ALL, but this should be better:
WHERE carInfo #> cast('{"name":"BMW", "year": "2020"}' as jsonb)

What does the distinct on clause mean in cloud datastore and how does it effect the reads?

This is what the cloud datastore doc says but I'm having a hard time understanding what exactly this means:
A projection query that does not use the distinct on clause is a small operation and counts as only a single entity read for the query itself.
Grouping
Projection queries can use the distinct on clause to ensure that only the first result for each distinct combination of values for the specified properties will be returned. This will return only the first result for entities which have the same values for the properties that are being projected.
Let's say i have a table for questions and i only want to get the question text sorted by the created date would this be counted as a single read and rest as small operations?
If your goal is to just project the date and text fields, you can create a composite index on those two fields. When you query, this is a small operation with all the results as a single read. You are not trying to de-duplicate (so no distinct/on) in this case and so it is a small operation with a single read.

Spring Data Mongo Query to query with multiple fields and return in one call

Firstly, the document schema am querying on, is as follows:
{x:10, y:"temp", z:20}
There are multiple other documents in the collection with the same schema as above.
Now, I have a list where each element contains, pair of values belonging to keys x and y. This can be pictured as:
[{10,"temp"}, {20,"temp1"}, .....]
keys -> x y x y
Now, am aware, that if I process the array in a loop and take each pair, I can construct a query like:
query.addCriteria(Criteria.where("x").is(10).and("y").is("temp"))
This will return the document if it matches the AND criteria. I can query with all the pairs in the list in such a manner. But this approach will involve a high number of calls to the data base since for each pair in the list, there is a database call.
To avoid this, Is there any way I can query for all the documents that match this AND criteria for each element in the list, in a single call, using spring data MongoDb Api? Framed differently, I want to avoid looping through the array and making multiple calls, if possible.
You could use Criteria.orOperator to return each Document that match at least one Criteria of your list.
Build your list of Criteria looping over your list
List<Criteria> criteriaList = new ArrayList<>();
for (item : yourList) {
criteriaList.add(Criteria.where("x").is(item.x).and("y").is(item.y));
}
Build your query using orOperator:
Query.query(new Criteria.orOperator(criteriaList.toArray(new Criteria[criteriaList.size()])));