Search code examples
javaplayframeworkjpqlplayframework-1.xcriteriaquery

JPQL Criteria Query Play Framework 1x Inner Join And Where Clause Construction


I am stuck trying to build the following SQL query in JPA (still new to Java and JPA and I searched quite a bit and still don't get it, sorry if it's a noob question).

JPQL query that does almost what I want and works

SELECT s
FROM Sentence s
INNER JOIN s.words sw
WHERE s.date = :date
AND sw IN (:words)

Sentence and Word are both classes SentenceWord is a ManyToMany join between Sentence and Word. So s.words is a List that belong to a Sentence. s.Date is a Import object that stores stuff data the import :date is the single Import we want to match. :words is a List of the words we want to check in a sentence.

This query works just fine for the IN operator.

However we now have the requirement to check to see if a Sentence contains all of the words in the List we are passing (that we assemble from s.words List).

Right now I'm just manually building WHERE AND clauses using string builder and a FOR EACH loop and appending them to a String and the using Query. It works but it's super ugly and seemingly anti-Javay and I'd like to do it the right/typesafe way.

I found CriteriaBuilder and CriteriaQuery but just haven't been able to get the INNER JOIN to work. At all. And sadly, it's after I RTFM. http://docs.jboss.org/hibernate/orm/4.0/hem/en-US/html/querycriteria.html#querycriteria-from-join

For the dynamic WHERE AND query this is as far as I've gotten

q.where(
    cb.and(
        cb.equal(s.get("date"), date)

        for (Word word : words) {
            , cd.equal(s.get("words"), word)          
        }

    )
);

But that just seems all wrong and I can't test it because I can't do the join. If someone can point me in the right direction for a CriteriaQuery tutorial on joins for simple people and looping through a Collection to create AND clauses, or write out the correct query in this example, I would be very thankful!

UPDATE - QUERY EFFICIENCY

the alternative JPQL query would be:

SELECT s 
FROM Sentence s
INNER JOIN s.words sw
WHERE s.date = :date
AND sw IN (:words)
GROUP BY s
HAVING count(sw) = :numberOfWords

Group By seems to be required by JQPL but not for the same query in SQL. I just have a small data set now so both are quite quick but am curious if one is preferred to the other.

Also, I'm just using Query to set the parameters, is there a better class to use for these? I like named terms better than ?.

Query query = JPA.em().createQuery(jpql);
query.setParameter("date", date);
query.setParameter("numberOfWords", new Long(words.size()));
query.setParameter("words", words);

Solution

  • The following query should do what you want. No idea if there's a better alternative:

    select s 
    from Sentence s
    where s.date = :date
    and :numberOfWords = (select count(sw) from SentenceWord sw
                          where sw.sentence = s
                          and sw in (:words))
    

    where numberOfWords is the number of words contained into the collection of words to match. Of course, this collection must not contain any duplicate.

    To explain what the query does: it checks that the number of words in the set of words to match is equall to the number of words parts of this set and referenced by the sentence.