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 3750902
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T08:59:52+00:00 2026-05-19T08:59:52+00:00

I have two entities : Items(id, user_id, title, price); Purchases(id, item_id, user_id, date); Using

  • 0

I have two entities :

Items(id, user_id, title, price);
Purchases(id, item_id, user_id, date);

Using JPA, I’d like to list all the items that have been purchased more than X times, ordered by their purchased times (the first being the most purchased).

I managed to have the correct SQL request, but how can I do that in JPA (possibly without using createQuery or something equivalent) :

SELECT i.title, COUNT(p.id) as rank FROM items AS i LEFT OUTER JOIN purchases AS p ON p.item_id = i.id WHERE rank > X GROUP BY i.title ORDER BY rank DESC;
// of course, X is an int!

Thank you for your help! 🙂

Update:
I indicated to avoid createQuery but I didn’t explained why.
The thing is, I made a class dedicated to generating the query, it looks like :

public class Arguments {
    protected HashSet allowedOrders = new HashSet();
    protected Collection args = new ArrayList();

    // constructor and some other methods

    // this one works great
    public void setPriceMin(int price) {
        query += " AND price > ?";
        args.put(price);
    }

    // sames method for setPrice (= ?), and setPriceMax (= <)

    // this one doesn't :/
    public void setSalesMin(int sales) {
        // here's my problem
    }
}

But It’s (really) possible that my methods isn’t good. And since you bring up the Criteria, maybe I should take a look at it, even for “setPriceMin” and all the other methods.

How’d you do?

  • 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-19T08:59:53+00:00Added an answer on May 19, 2026 at 8:59 am

    You can’t do it without a Query.

    Either rewrite the Query as JPQL (very similar, but you will have to replace ids with objects and do joins on properties, not on tables), or use the JPA 2 CriteriaQuery API. (Or use your SQL as a native Query, but this is usually a bad idea)

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

Sidebar

Related Questions

I have two entities like this: public class Service { public virtual int ServiceId
I am creating an iOS 5 app using Core Data. I have two entities
I have two entities @Entity public class Tabulka{ @OneToMany(mappedBy = tabulka) private List<VysledkyHraca> vysledkyHraca;
I'm using Hibernate (3.3.x) and I have two entities: public class FtChargeAcctPkgDtl { private
I have two entities: Question and Multiple_Choice_Question . A Question object has a list
I'm doing some tests using NHibernate and I currently have two mapped entities, Orders
I have two entities: Recipe and Ingredient. Entites: public class Ingredient { public int
I have two entities A and B. public class A{ @Id @GeneratedValue private Integer
I have two entities Visita and Cliente that implements respectively two interfaces IVisita and
A have two entities. For example timing settings and orders. @Entity public class TimingSettings{

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.