Questions tagged [hql]

For questions concerning "Hibernate Query Language" (HQL), a query language used by Hibernate and NHibernate. Use the tag [hiveql] for questions about Hive Query Language.

HQL, acronym for "Hibernate Query Language", is a powerful, object oriented query language used by the Hibernate and the NHibernate object relational mappers.

It is used in Java environments and is heavily inspired by SQL. Its syntax resemble SQL queries, but operates with Java Persistence API (JPA) entity objects rather than directly with database tables.

HQL can also be used on platforms such as Adobe ColdFusion

5074 questions
312
votes
22 answers

JPA and Hibernate - Criteria vs. JPQL or HQL

What are the pros and cons of using Criteria or HQL? The Criteria API is a nice object-oriented way to express queries in Hibernate, but sometimes Criteria Queries are more difficult to understand/build than HQL. When do you use Criteria and when…
cretzel
  • 19,864
  • 19
  • 58
  • 71
296
votes
6 answers

What is the difference between JOIN and JOIN FETCH when using JPA and Hibernate

Please help me understand where to use a regular JOIN and where a JOIN FETCH. For example, if we have these two queries FROM Employee emp JOIN emp.department dep and FROM Employee emp JOIN FETCH emp.department dep Is there any difference between…
abbas
  • 6,453
  • 2
  • 40
  • 36
268
votes
14 answers

How do you do a limit query in JPQL or HQL?

In Hibernate 3, is there a way to do the equivalent of the following MySQL limit in HQL? select * from a_table order by a_table_column desc limit 0, 20; I don't want to use setMaxResults if possible. This definitely was possible in the older…
stevedbrown
  • 8,862
  • 8
  • 43
  • 58
164
votes
6 answers

JpaRepository Not supported for DML operations [delete query]

I have written a query to delete some objects in my interface extending JPaRepository, but when I execute the query it throws an exception! Can anyone explain it for me? Query: public interface LimitRepository extends JpaRepository
Student
  • 1,643
  • 2
  • 8
  • 6
123
votes
6 answers

Difference between INNER JOIN and LEFT SEMI JOIN

What is the difference between an INNER JOIN and LEFT SEMI JOIN? In the scenario below, why am I getting two different results? The INNER JOIN result set is a lot larger. Can someone explain? I am trying to get the names within table_1 that only…
user3023355
  • 1,257
  • 2
  • 9
  • 6
108
votes
11 answers

How do you create a Distinct query in HQL

Is there a way to create a Distinct query in HQL. Either by using the "distinct" keyword or some other method. I am not sure if distinct is a valid keywork for HQL, but I am looking for the HQL equivalent of the SQL keyword "distinct".
Mike Pone
  • 18,705
  • 13
  • 53
  • 68
86
votes
5 answers

IN-clause in HQL or Java Persistence Query Language

I have the following parametrised JPA, or Hibernate, query: SELECT entity FROM Entity entity WHERE name IN (?) I want to pass the parameter as an ArrayList, is this possible? Hibernate current tells me, that java.lang.ClassCastException:…
Daniel
  • 27,718
  • 20
  • 89
  • 133
84
votes
3 answers

Proper way of writing a HQL in ( ... ) query

Assuming that I want to write the following HQL query: FROM Cat c WHERE c.id IN (1,2,3) what is the proper way of writing this as a parametrized query, e.g. FROM Cat c WHERE c.id IN (?)
Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278
80
votes
2 answers

org.hibernate.QueryException: illegal attempt to dereference collection

I am trying following hql query to execute SELECT count(*) FROM BillDetails as bd WHERE bd.billProductSet.product.id = 1002 AND bd.client.id = 1 But it is showing org.hibernate.QueryException: illegal attempt to dereference…
xrcwrn
  • 5,339
  • 17
  • 68
  • 129
79
votes
6 answers

Hibernate criteria: Joining table without a mapped association

I'd like to use Hibernate's criteria api to formulate a particular query that joins two entities. Let's say I have two entities, Pet and Owner with a owner having many pets, but crucially that association is not mapped in the Java annotations or…
Snukker
  • 1,963
  • 3
  • 18
  • 18
76
votes
5 answers

ORDER BY using Criteria API

When I write a HQL query Query q = session.createQuery("SELECT cat from Cat as cat ORDER BY cat.mother.kind.value"); return q.list(); Everything is fine. However, when I write a Criteria Criteria c =…
niklassaers
  • 8,480
  • 20
  • 99
  • 146
75
votes
16 answers

org.hibernate.NonUniqueResultException: query did not return a unique result: 2?

I have below code in my DAO: String sql = "SELECT COUNT(*) FROM CustomerData " + "WHERE custId = :custId AND deptId = :deptId"; Query query = session.createQuery(sql); query.setParameter("custId", custId); query.setParameter("deptId",…
user3198603
  • 5,528
  • 13
  • 65
  • 125
72
votes
5 answers

Hibernate Criteria vs HQL: which is faster?

I have been reading some anwers, but i'm still confused. ¿Why? because the differences that you have mentioned do not relate with the performance. they are related with easy use.(Objetc(criteria) and SQL(hql)). But I would like to know if "criteria"…
kcobuntu
  • 751
  • 1
  • 6
  • 4
70
votes
2 answers

Difference between LEFT JOIN and LEFT JOIN FETCH in Hibernate?

I am trying to understand the difference between LEFT JOIN and LEFT JOIN FETCH in Hibernate. Can anyone explain this? Thanks
Barry
  • 1,585
  • 3
  • 22
  • 35
68
votes
3 answers

Hibernate HQL Query : How to set a Collection as a named parameter of a Query?

Given the following HQL Query: FROM Foo WHERE Id = :id AND Bar IN (:barList) I set :id using the Query object's setInteger() method. I would like to set :barList using a List of objects, but looking at the Hibernate documentation and…
karlgrz
  • 14,485
  • 12
  • 47
  • 58
1
2 3
99 100