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
23
votes
5 answers

NHibernate HQL's Equivalent to T-SQL's TOP Keyword

What is NHibernate HQL's Equivalent to T-SQL's TOP Keyword? Also what is the non-HQL way for saying give me the first 15 of a class?
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466
23
votes
7 answers

HQL: How to select all entities distinct by some column?

A simple question: In this example I need to retrieve all objects, but these objects must have distinct msgFrom fields. When I use List list = getHibernateTemplate().find("select distinct m.msgFrom from Message m WHERE msgTo = ? AND…
gennad
  • 5,335
  • 12
  • 44
  • 47
23
votes
4 answers

Can HQL Select on the result set of another query?

Can HQL Select on the result set of another query? For example: SELECT COUNT(*) FROM (SELECT * FROM Table) I can do it in SQL but when I tried like above in HQL, it just showed me syntax error "unexpected token: ( near line 1, column 22 ..."
Red Bit
  • 455
  • 2
  • 6
  • 15
22
votes
1 answer

HQL: Using Boolean in Named Queries

Can you please help me? I have error in querying boolean value "r.isDefault = true". In my HQL named query: SELECT r FROM RptQuery r WHERE r.code = ?1 AND r.isDefault = true …
Jemru
  • 2,091
  • 16
  • 39
  • 52
22
votes
5 answers

Hibernate Named Query Order By parameter

Can anyone point me to how we can pass an order by clause as a named parameter to HQL? Example which works: select tb from TransportBooking as tb and TIMESTAMP(tb.bookingDate, tb.bookingTime) >= current_timestamp() order by tb.bookingDate Example…
Jet Abe
  • 410
  • 1
  • 4
  • 7
22
votes
4 answers

How to write a like query in HQL

I want to perform search for a particular string that is starting with a particular alphabet. So, for example if the starting alphabet is 'A' then it should produce a result which will contain all the strings with alphabet 'A'. How do I achieve this…
Ni3
  • 349
  • 1
  • 4
  • 8
20
votes
4 answers

How to left join fetch multiple children in Hibernate?

I'm working with hibernate and I'm having troubles creating an hql query that fetches all the children of my object. For example: The Object User has a list of Cars and a list of Friends. To get a user with his cars I would use following query: from…
Bjorn Rombaut
  • 213
  • 1
  • 2
  • 4
20
votes
7 answers

Using a CASE statement in HQL select

Is there any way to do the following in HQL: SELECT case when flag = true then SUM(col1) else SUM(col2) FROM myTable
lomaxx
  • 113,627
  • 57
  • 144
  • 179
20
votes
2 answers

Hibernate: Overriding mapping's EAGER in HQL?

It's possible to override LAZY in HQL using LEFT JOIN FETCH. FROM Obj AS obj LEFT JOIN FETCH obj.otherObj WHERE obj.id = :id Is it also possible to override EAGER? How?
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
20
votes
2 answers

Hibernate HQL: Get count of results without actually returning them

I want to get the count of the results of a dynamically-generated HQL query, without actually getting the list of results. Say that the query I have is something like: select Company company LEFT OUTER JOIN FETCH products product I read in the…
Markos Fragkakis
  • 7,499
  • 18
  • 65
  • 103
20
votes
4 answers

Inner Joins Query in HQL

I'm unable to execute HQL for Inner Joins, Query is executing correct at sql but not in HQL. I don't know where i'm missing. Your help is appreciable. ***Error***: org.hibernate.hql.ast.QuerySyntaxError: unexpected token: ON near line 1, column 148…
user1395824
20
votes
4 answers

Hibernate HQL delete with and

Hibernate doesn't delete my row: public boolean deleteVote(Login user, int pid){ Session session = getSession(); try{ String hql = "delete from Vote where uid= :uid AND pid= :pid"; Query query =…
user1671980
  • 273
  • 1
  • 4
  • 10
20
votes
6 answers

Using hibernate/hql to truncate a table?

What is the recommended way to truncate a table using hibernate/hql? I've tried this: Query query = session.createQuery("truncate table MyTable"); query.executeUpdate(); But it didn't work (truncate doesn't seem do be documented anywhere in…
user149100
  • 1,089
  • 3
  • 12
  • 16
19
votes
1 answer

Hibernate query: does a Set contains a certain Object?

I have two Hibernate data object. The first is a User (with unique id, username etc.) and the second is the class Collaborateable. Between this two there is a n-to-m relation (implementet with Sets). That means, a User works on many Collaborateables…
sockeqwe
  • 15,574
  • 24
  • 88
  • 144
19
votes
2 answers

Hibernate criteria: how to order by two columns concatenated?

I have a Person table which has two columns: first_name and last_name. The Person class has two corresponding fields: firstName and lastName. Now I'm using criteria api and trying to create an order by based on these two columns concatenated. Is it…
Georgie Porgie
  • 2,100
  • 3
  • 24
  • 25