0

I want to implement JPA query using this SQL query:

String hql = "SELECT DATE(date) AS Date, SUM(volume) AS amount, COUNT(*) AS number_of_transactions " + 
                " FROM " + PaymentTransactionsDailyFacts.class.getName() + " WHERE (date BETWEEN :start_date AND :end_date )" + 
                " GROUP BY DATE(date)";

I tried this:

String hql = "SELECT DATE(e.date) AS Date, SUM(e.volume) AS amount, COUNT(e.*) AS count " + 
                " FROM " + PaymentTransactionsDailyFacts.class.getName() + " e WHERE (date BETWEEN :start_date AND :end_date )" + 
                " GROUP BY DATE(date)";

But I get error:

expecting IDENT, found '*' near line 1, column 63 [SELECT DATE(e.date) AS Date, SUM(e.volume) AS amount, COUNT(e.*) AS count  FROM .......PaymentTransactionsDailyFacts e WHERE (date BETWEEN :start_date AND :end_date ) GROUP BY DATE(date)]

What is the proper way to implement this query into JPA?

2
  • Is there any answer you got? because this question's answers isn’t accepted yet, and you didn't respond too. I need about this information too please Commented Jan 20, 2021 at 14:23
  • See the below one. Commented Jan 20, 2021 at 14:24

1 Answer 1

1

JPQL doesn't know what * means. You'd instead just say e, not e.*.

String hql = "SELECT DATE(e.date) AS Date, SUM(e.volume) AS amount, COUNT(e) AS count " 
              + " FROM " + PaymentTransactionsDailyFacts.class.getName()
              + " e WHERE (date BETWEEN :start_date AND :end_date )" 
              + " GROUP BY DATE(date)";

This is because JPQL operates on the Java entities. Note that you are saying from a class name, not a table. Objects don't have wildcards saying any field in the class. Instead, you specify that the object itself is not null to say that a row is there.

The e.* syntax in SQL is essentially (although perhaps not literally) saying that there exists at least one non-null column on that row. Because it doesn't have any more direct way of saying that a row exists. But JPQL does have a way to say that an object exists. That's a basic Java not-null check. So in JPQL, you do it the Java way (albeit in JPQL syntax) rather than the SQL way.

See also

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.