HQL Hibernate INNER JOIN

Joins can only be used when there is an association between entities. Your Employee entity should not have a field named id_team, of type int, mapped to a column. It should have a ManyToOne association with the Team entity, mapped as a JoinColumn: @ManyToOne @JoinColumn(name=”ID_TEAM”) private Team team; Then, the following query will work flawlessly: … Read more

IN-clause in HQL or Java Persistence Query Language

Are you using Hibernate’s Query object, or JPA? For JPA, it should work fine: String jpql = “from A where name in (:names)”; Query q = em.createQuery(jpql); q.setParameter(“names”, l); For Hibernate’s, you’ll need to use the setParameterList: String hql = “from A where name in (:names)”; Query q = s.createQuery(hql); q.setParameterList(“names”, l);

How to escape reserved words in Hibernate’s HQL

You could achieve it by a workaround using your custom “alias to map” transformer, so your code would change to something like this Query q = mySession.createQuery( “SELECT u.id AS id, u.name AS text, u AS obj FROM User u”) .setResultTransformer( AliasToMapTransformer.renameAlias(“obj”, “object”).build() ); And then using this class: public class AliasToMapTransformer extends BasicTransformerAdapter { … Read more

How do you create a Distinct query in HQL

Here’s a snippet of hql that we use. (Names have been changed to protect identities) String queryString = “select distinct f from Foo f inner join foo.bars as b” + ” where f.creationDate >= ? and f.creationDate < ? and b.bar = ?”; return getHibernateTemplate().find(queryString, new Object[] {startDate, endDate, bar});