Im running into some problems when trying to convert a SQL query into HQL (or Criteria/Restriction). I have the following SQL query:
Select count(n), CreatedDate from (
Select count(serverId) as n, Date(Created) as CreatedDate
from mytable
group by Date(Created), serverId
) as tbl
group by CreatedDate;
So what is the HQL (or Criteria) equivalent?
Some SQL queries cannot be directly translated to HSQL but this might help: HQL Subqueries in joins
Related
SELECT recipe.name,SUM(salesdetails.quantity::integer),recipe.price As Quantity
FROM (salesinfo JOIN salesdetails ON salesinfo.sessionid=salesdetails.salesinfo_sessionid)
JOIN recipe ON salesdetails.recipe_id=recipe.id group by salesdetails.recipe_id,recipe.name,recipe.price
ORDER BY SUM(salesdetails.quantity::integer) DESC;
Can anyone give me the hql query for this?
If you are not acquainted with the HQL and want to use the same query ,then you can do it using the native query feature of the Hibernate like this :
#QUERY(value="SELECT recipe.name, SUM(salesdetails.quantity::
INTEGER),recipe.price AS Quantity
FROM (salesinfo
JOIN salesdetails ON salesinfo.sessionid=salesdetails.salesinfo_sessionid)
JOIN recipe ON salesdetails.recipe_id=recipe.id
GROUP BY salesdetails.recipe_id,recipe.name,recipe.price
ORDER BY SUM(salesdetails.quantity:: INTEGER) DESC", nativeQuery = TRUE)
But I would recommend that you first try to convert the same in into HQL query and then if any issue then you should ask it here instead of directly asking for the converted query. Meanwhile this can help.
I am try to query from and database using eclipse link using entities. when I use normal query records are returned from current date by when I use JPA zero records are returned. Where could I be doing wrong, Or how can I use to_char function in JPA my database is postgresql.
normal sql query that returns records
select *
from mytable
where to_char(transaction_date, 'YYYY-MM-DD') = '2016-01-19';
MyEntity
#Temporal(javax.persistence.TemporalType.DATE)
private Date transaction_date;
EntityManager e = EntityMgr.MyFact().createEntityManager();
e.getTransaction().begin();
Query qry = e.createQuery("from MyEntity u where u.transaction_date=?1");
qry.setParameter(1, new Date(), TemporalType.DATE);
Have you tried with qry.setParameter(1, new Date(), TemporalType.TIMESTAMP);
You can use to_char function using operator
like
OPERATOR('ToChar', transaction_date,'YYYY-MM-DD')= '2016-01-19';
If I have a group by query
Select e.field1, e.field2...
from Entity w
group by e.field1, e.field2...
How can I count the number of rows not using NativeQuery?
I need to do something like this:
select count(*) from
(Select e.field1, e.field2...
from Entity w
group by e.field1, e.field2...)
Is there any way to do with JPQL?
in oracle you can this hql
Select sum(count(*) - count(*) + 1)
from Entity e
group by e.field1, e.field2...
but dose not work in postgres
Derived tables are not supported in JPQL, so, the best way to do it is to run a native SQL query.
After all, there is a very good reason why both JPA and Hibernate offer support for SQL queries, don't you agree?
I am new to HQL and I am working on subqueries.
I have the following SQL subquery:
select * from (
select * from table order by columnname
) as subquery
where columnvalue = 'somevalue';
I want to fire the query in HQL. I wrote the below code :
Result = session.createQuery("from (from table order by columnname) as subquery where columnvalue = :somevalue")
.setParameter(/*setting all parameters*/)
.list();
I am getting this exception:
QuerySyntaxException : unexpedted token :( line 1, column 10 [from (from ...)]
My SQL query is giving me correct results. How do I write it in HQL ?
I did not think HQL could do subqueries in the from clause
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-subqueries
note the sentence:
Note that HQL subqueries can occur only in the select or where clauses.
It will be better if you excute native SQL.
In SQL I have a query like this..
SELECT * FROM userdata where date_format(startdate,'%p') = 'AM'
How can I change this query to HQL? For a data range I need only morning data.
date_format should work in hql as well. Try the same query in hql.