Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8640695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:20:34+00:00 2026-06-12T11:20:34+00:00

I am stuck trying to build the following SQL query in JPA (still new

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:20:36+00:00Added an answer on June 12, 2026 at 11:20 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes I still get stuck trying to translate procedural code into functional code. Is
I'm stuck for a full afternoon now trying to get python to build in
I'm stuck trying to understand the following: %w[a b c d][4,20] => [] %w[a
I'm stuck trying to get a WinInet HTTP POST via SSL using ONLY C.
I'm stuck trying to get a regex to match a filetype in a sorting
I'm really stuck trying to use java wrapper library for opencv's cvMatchTemplate. See this
I'm trying to populate a DataTable, to build a LocalReport, using the following: MySqlCommand
I've been trying to build my first rails app and have gotten stuck on
I'm new to MEF and am trying to use it to build a plug-in
I'm trying to build a category/subcategory class with php & MySQL. I am stuck

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.