Questions tagged [hql]

Use this tag 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 operate against Java Persistence API (JPA) entity objects rather than directly with database tables.

HQL can also be used on platforms such as Adobe ColdFusion

4854 questions
27
votes
3 answers

org.hibernate.QueryException: JPA-style positional param was not an integral ordinal

I have the following JPQL request; @Query(value = "select req_t " + "from TransactionRelation tr " + "inner join tr.requestTransaction req_t " + "inner join req_t.transactionStateHistory req_t_h " + "inner join…
gstackoverflow
  • 31,683
  • 83
  • 267
  • 574
25
votes
3 answers

@NamedQuery over @NamedNativeQuery

Is there any benefit of using @NamedQuery over @NamedNativeQuery in hibernate or vice verse. I cant spot the exact difference or in which context we should use @NamedQuery over @NamedNativeQuery Thanks in advance.
Abhi
  • 6,009
  • 6
  • 37
  • 52
24
votes
3 answers

"from unexpected" when createQuery

I get a error when I createQuery: 'from' unexpected My code is below: @Override public Admin findByAdmin(Admin admin) { return (Admin) sessionFactory.getCurrentSession(). createQuery("from Admin where…
244boy
  • 3,994
  • 8
  • 30
  • 72
24
votes
4 answers

Hibernate and Unexpected end of Subtree exception

I'm a newbie to Hibernate. I have an Item POJO which contains a Set consisting of labels. The labels are contained on another Database table from the Item table, so I do a join to populate the pojo. I'm trying to run a simple example query…
Scott Fines
  • 739
  • 1
  • 4
  • 10
24
votes
4 answers

How to retrieve only certain fields of an entity in JPQL or HQL? What is the equivalent of ResultSet in JPQL or HQL?

In JPQL, I can retrieve entities by : query = entityManager.createQuery("select c from Category c"); List categories = query.getResultList(); But, if I wish to retrieve the id and name fields (only) of the Category entity, I need…
Daud
  • 6,209
  • 15
  • 55
  • 108
23
votes
6 answers

Using Hibernate query : colon gets treated as parameter / escaping colon

return sessionFactory.getCurrentSession(). createQuery("FROM Weather WHERE city_id = :id AND date " + "BETWEEN now()::date AND now()::date + (:days - 1)"). setInteger("id",…
Jaanus
  • 15,013
  • 43
  • 137
  • 193
23
votes
6 answers

Hibernate - HQL pagination

This is a problem similar to: HQL - row identifier for pagination I'm trying to implement pagination using HQL. I have a PostgreSQL database. int elementsPerBlock = 10; int page = 2; //offset = 2*10 String sqlQuery = "FROM Messages AS msg " + …
iliaden
  • 3,621
  • 7
  • 35
  • 46
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
  • 64,613
  • 107
  • 281
  • 451
23
votes
6 answers

Hibernate query for selecting multiple values

In hibernate I can do following Query q = session.createQuery("from Employee as e); List emps = q.list(); Now if I want to fetch int and String how can I do it? Query q = session.createQuery(""SELECT E.firstName,E.ID FROM Employee…
user93796
  • 17,329
  • 29
  • 84
  • 133
22
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
  • 4,615
  • 8
  • 38
  • 44
22
votes
2 answers

Why does Hibernate execute multiple SELECT queries instead of one when using @Fetch(FetchMode.JOIN)

I've got the following query which I expect to run in a single select request: @NamedQuery(name=Game.GET_GAME_BY_ID1, query = "SELECT g FROM Game g " + "JOIN FETCH g.team1 t1 " + "JOIN…
maximus
  • 3,476
  • 13
  • 51
  • 96
22
votes
1 answer

How do I use order by in HQL?

I want to execute my HQL query like this: Query queryPayment=sixSession.createQuery("from Payment where vcode=:p_Vcode or (Installment_Vcode=:installmentVcode and payment_date>:pdate) order byvcode."+order +"desc") .setParameter("p_Vcode",…
AFF
  • 1,397
  • 4
  • 19
  • 34
21
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
  • 1,931
  • 15
  • 36
  • 50
21
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
  • 402
  • 1
  • 4
  • 7
21
votes
2 answers

Hibernate could not locate named parameter even if it exist

Hibernate Keeps detecting org.hibernate.QueryParameterException: could not locate named parameter [name] even though it exist. here's my hql Query query = sess().createQuery("from UserProfile where firstName LIKE '%:name%'").setParameter("name",…
user962206
  • 13,699
  • 56
  • 164
  • 259