grails « Query « JPA Q&A

1. How to count in Grails/Hibernate: Message.countBy

How do I count the number of messages where my body length is between 0 and 25 characters long?

Message.countBy('from Message m where m.body.length <= 25')
Unfortunately for me, countBy does not take ...

2. How To Call A Named Query In Grails

Given the following example from Hibernate Make Easy, how can I call the named query, user.findByLoginName in Grails?

package com.examscam.model;
import javax.persistence.*;
import org.hibernate.Session;
import com.examscam.HibernateUtil;
@Table(name = "user", schema = "examscam")

3. Grails: Adding sorting and paging to a custom HQL query

I have a list method that uses HQL. how do I add the paging and sorting parameters to this query?

   def list = {

4. How to query a date in HQL (Hibernate) with Joda Time?

I am sure that someone familiar with HQL (I am myself a newbie) can easily answer this question. In my Grails application, I have the following domain class.

class Book {
  org.joda.time.DateTime ...

5. HQL Insert Query in Grails

I want to write an insert query in Grails. I have tried all possible combinations but cant get the syntax correct. Can anybody please help?

class Person {
    int ...

6. How to get entries from the second level query cache?

In my grails application, I want to display all the current entries of the second-level cache from all regions. My code is as following :

def getCacheStats() {
  StatisticsImpl stats ...

7. Grails 1.3.1: Improved Query Caching

Improved Query Caching The findAll query method now supports taking advantage of the 2nd level cache.
Book.findAll("from Book as b where", [author:'Dan ...

8. Criteria for SELECT bla1, bla2 FROM foo

I want a criteria that only selects bla1 and bla2 from all attributes of a given domain Foo.findAll() or

def c = Foo.createCriteria()
def results = c {}
results = List ...

9. Grails (Hibernate) Criteria query

I have a domain class in my Grails app that looks like this:

class Event {
    Date date
I want to write a criteria query that selects events that occurred ...

10. Groovy criteria query

I have a domain model that looks like this

Category 1 ------- * Type 1 ------- * Expense
Or in English "An expense has a type, and each type belongs to a category". ...

11. SQL\HQL queries repository

While working on some java projects i've saw some sort of SQL repository. The idea was to place all queries in one(or few) xml files and retrieve them when needed by name. ...

12. Grails/Hibernate: how to order by isnull(property) to get NULLs last?

Normally when ordering ascending by a field, you get the NULL values first, and then the more interesting values. Often, you want NULL values last. In MySQL, you can do this ...

13. How to Group property in Order Clause using Grails Criteria

i am using Grails Criteria to pull results for a specific domaini want to ask whether is it possible to have group property in order clause or in other words, how ...

14. Querying database values from a column in Grails with Hibernate

Im new to grails and I try to get a database entry. The database connection works without problems. In the database Ive got a class named "Correct" which contains correct answers (booleans), so ...

15. Is it possible to query like this?


class Member{
         string name          


16. findAllByPropertyInList results in wrong result, JOIN related or hibernate Distinct/paging problem?

I have some strange results of findAllByPropertyInList() and think this is a bug in grails. See [1], the result is not what I expect it to be and what other queries say. Could it ...

17. [Query]Grails getLast entry for each groupBy

Query : I like to know how one can get latest entry of a domain entity in Grails. Problem domain:

 class Parent {

    String name

18. Grails 1.3.7 maven plugin cannot find hibernate dependency

I'm a little new to Grails, but here's the scenario: I'm using Grails 1.3.7 and I have a custom plugin with a dependency on Hibernate. In the MyPluginGrailsPlugin.groovy I have:

def dependsOn ...

19. HQL query in grails for LIKE

I want to know if HQL can handle a query such as the following "find the values LIKE "abc" or "def" or ... in either column1 or column2 or Table1" So I have ...

20. Grails - Criteria and SQL injections

I have a classical criteria query that uses some params entered by the user, such as:

def query = MyTable.createCriteria()
def myQueryResult = query.list() {
   if (params.minToInvestMin)