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

  • SEARCH
  • Home
  • 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 3941980
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:36:29+00:00 2026-05-20T00:36:29+00:00

I’m trying to write a fairly complex JPQL statement that sorts results based on

  • 0

I’m trying to write a fairly complex JPQL statement that sorts results based on a value in another table. I know what the MySQL would look like, but I need help turning it into JPQL. Here’s the equivalent SQL:

SELECT o.* FROM Observation o
LEFT JOIN Obs_Event p
ON p.Event_ID = o.Event_ID
LEFT JOIN Event_Set ppp
ON ppp.Event_ID = o.Event_ID AND ppp.Event_Set_ID = o.Event_Set_ID
WHERE o.Individual_ID = <some id>
AND o.Observation_Date = <a date value>
ORDER BY ppp.Seq_Num ASC

Any help on how to do this would be much appreciated. Specifically I seem to having trouble with this part:

ON ppp.Event_ID = o.Event_ID AND ppp.Event_Set_ID = o.Event_Set_ID

So, far I’ve tried this:

        Query q=  em.createQuery("select o " +
                " from Observation as o " +
                " join o.eventID p" +
                " join p.eventSetCollection ppp " +
                " where o.individualID = :indiv " +
                " AND o.observationDate = :d " +
                " AND o.eventID = ppp.event " +
                "";

If I try to add something like

WHERE ...
 AND o.eventSetID = ppp.eventSet 

… it simply doesn’t work. If I look at the generated SQL, it looks like ppp.eventSet is simply ignored.

Suggestions?

  • 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-05-20T00:36:29+00:00Added an answer on May 20, 2026 at 12:36 am

    So, first thanks to the poster that commented, asking for my entity relationships. That forced me to take a closer look at things and realize what was wrong.

    As it turns out, back when I was even worse at JPA than I am now, I set the o.eventID property to an actual Integer, as in the primary key of the record/object, instead of an actual object. Hence, in the JPQL above, I’m actually trying to map an object to an integer in the following statement:

    AND o.eventID = ppp.event

    turns out the correct statement is:

            Query q=  em.createQuery("select o " +
                    " from Observation as o " +
                    " join o.eventID p" +
                    " join p.eventSetCollection ppp " +
                    " where o.individualID = :indiv " +
                    " AND o.observationDate = :d " +
                    " AND o.eventID= ppp.event.eventID" +
                    " order by ppp.seqNum ASC" +
                    "");
    

    This works in a pinch, although I should eventually change that relationship in the Observation class to actually go to an object.

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

Sidebar

Related Questions

No related questions found

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.