1
I’m having a hard time finding good examples of how to create a query criteria using Inner Join. I created the query below that would be what I would like to do with the criteria.
SELECT DISTINCT *
FROM DT_DOCUMENT as document
INNER JOIN DT_TRANSLATION as translation
ON translation.language_id IN(1, 2, 3)
WHERE document.id = translation.document_id
AND document.title LIKE '%documento%';
This will return all documents with the title "Document" and that are associated with translations with id 1, 2 and 3.
I managed to split this query into 2 different selects using the criteria and are returning the results, but I need the Inner Join to make the necessary filter.
Translation criteria query
CriteriaQuery<Translation> translationQuery = builder.createQuery(Translation.class);
Root<Translation> translation = translationQuery.from(Translation.class);
List<Long> ids = new ArrayList<>();
ids.add(1);
ids.add(2);
ids.add(3);
Predicate idPredicate = translation.in(ids);
translationQuery.where(idPredicate);
translationQuery.distinct(true);
TypedQuery<Translation> query =
this.entityManager.createQuery(translationQuery);
query.getResultList();
Returns all translations with id 1, 2 and 3;
Document criteria query
CriteriaQuery<Document> documentQuery = builder.createQuery(Document.class);
Root<Document> document = documentQuery.from(Document.class);
Predicate titlePredicate = builder.like(document.get("title"), "%documento%");
documentQuery.where(titlePredicate);
TypedQuery<Document> query = this.entityManager.createQuery(documentQuery);
query.getResultList();
Returns all documents with the title "Document".
Thank you for any contribution, thank you.
What would be the value of this Translation_.?
– Sabrina
It would be the reference of the "document_id", mapped in the Translation class.
– Paulo Gonçalves
Well I couldn’t apply this model of Oracle, however I managed to get ideas to complement mine, thank you.
– Sabrina