New object with HQL

I think that the section 15.6. The select clause covers what you’re trying to achieve: 15.6. The select clause … Queries can return multiple objects and/or properties as an array of type Object[]: select mother, offspr, mate.name from DomesticCat as mother inner join mother.mate as mate left outer join mother.kittens as offspr Or as a … Read more

Inserting data in one table using HQL in Hibernate

You don’t need to use hql to insert if the data is from another table. Simply get a reference to your entity, get a hold of a Hibernate session, and call save(). According to http://docs.jboss.org/hibernate/orm/4.0/devguide/en-US/html/ch04.html#d0e2116 Pseudo-syntax for INSERT statements INSERT INTO EntityName properties_list select_statement Only the INSERT INTO … SELECT … form is supported. You … Read more

HQL recursion, how do I do this?

You can’t do recursive queries with HQL. See this. And as stated there it is not even standard SQL. You have two options: write a vendor-specific recursive native SQL query make multiple queries. For example: // obtain the first node using your query while (currentNode.parent != null) { Query q = //create the query q.setParameter(“id”, … Read more

Is there a more efficient way of making pagination in Hibernate than executing select and count queries?

Baron Schwartz at MySQLPerformanceBlog.com authored a post about this. I wish there was a magic bullet for this problem, but there isn’t. Summary of the options he presented: On the first query, fetch and cache all the results. Don’t show all results. Don’t show the total count or the intermediate links to other pages. Show … Read more

Date operations in HQL

Depending on your database, this can be trivially simple. HQL supports built-in vendor-specific features and functions, it also supports the ability to extend the dialect by registering new functions if they’re not already supported by HQL. Let’s say you’re using SQLServer (or Sybase). SQLServer has a function called ‘DATEADD’ that can do what you like … Read more

HQL – row identifier for pagination

this is one situation where hibernate shines: typical solution with hql query. int elementsPerBlock = 10; int page = 2; return getSession().createQuery(“from SomeItems order by id asc”) .setFirstResult(elementsPerBlock * (page-1) + 1 ) .setMaxResults(elementsPerBlock) .list(); hibernate will translate this to a pattern that is understood by the database according to its sql dialect. on oracle … Read more