Creating criteria based queries with GORM, Hibernate or Grails
Questions tagged [createcriteria]
105 questions
30
votes
4 answers
hibernate - createCriteria or createAlias?
If I want to search those students who take class "Math" and "John" is his group:
shoud I use createCriteria or createAlias?
Criteria:
Criteria criteria = session.createCriteria(Student.class);
Criteria subquery1 =…

senzacionale
- 20,448
- 67
- 204
- 316
13
votes
3 answers
Grails queries with criteria: how to get back a map with column?
Is it possible, to query grails with criteria and receive a list of maps instead of a list of lists? I would like to have the column names in the results in order to then work with an 'associative array' rather then numeric array offsets. I…

fluxon
- 538
- 7
- 19
12
votes
3 answers
NHibernate: CreateCriteria and Exists clause
How can I write the following SQL using CreateCriteria:
SELECT * FROM FooBar fb
WHERE EXISTS (SELECT FooBarId FROM Baz b WHERE b.FooBarId = fb.Id)

cbp
- 25,252
- 29
- 125
- 205
11
votes
1 answer
Is there a 'contains' functionality on a collection property of a domain object for createCriteria?
I have an Auction domain object and a User domain object. An Auction hasMany Users.
What I'd like to do, using createCriteria, is something like this:
def c = Auction.createCriteria()
def l = c.list (max: maxVar, offset: offsetVar) {
…

Weezle
- 730
- 10
- 27
10
votes
3 answers
Grails / GORM criteria query with hasmany String
I have a domain object (Cat) like this:
class Cat {
String name
static hasMany = [
nicknames: String
]
}
(A cat has a name, and also has many nicknames (which are Strings))
And I am trying to query all the cats with certain…

McDave
- 101
- 1
- 3
8
votes
2 answers
gorm projection and loss of metainformation
When using projection on the properties, the result is returned as the list with the elements in the same sequence as that defined in the projections block. At the same time the property names are missing from the list and that is really…

rks
- 451
- 1
- 8
- 16
7
votes
1 answer
Can there be an if condition inside the Hibernate Create Criteria?
I am using HibernateCriteriaBuilder api to write my Criteria Queries. I want to know if inside Criteria we can have conditional logic, such as an if statement?
For example:
OnemonthList=it.createCriteria().list {
if (res_id!='all'){
…

pri_dev
- 11,315
- 15
- 70
- 122
6
votes
1 answer
Build createCriteria in Grails dynamically and in a DRY way?
I'm working on building a createCriteria dynamically. So far, so good:
obj is the domain object(s) I want back
rulesList is a list of maps which hold the field to be searched on, the operator to use, and the value to search against
def c =…

Weezle
- 730
- 10
- 27
5
votes
1 answer
Grails 2.x createCriteria 'or' doesn't work for nested associations
It seems that in Grails 2.x, if you have a domain class association, and you try to run a createCriteria using or on that relation + another query, the or will ignore the other query and just use the results of the nested association. I realize…

Igor
- 33,276
- 14
- 79
- 112
4
votes
1 answer
grails createCriteria how do i loop over restrictions
i can do this:
def criteria = Category.createCriteria();
def results = criteria.list{
like('categoryName', "%abc%") or
like('categoryName', "%qwe%")
};
but how i do it…

zevel80300
- 147
- 1
- 11
4
votes
1 answer
Hibernate 5 - createCriteria deprecated
I need a help to migrate the code with createCriteria for Hibernate 5, the code is this below:
public Curso comDadosIguais(Curso curso) {
return (Curso) this.session.createCriteria(Curso.class)
.add(Restrictions.eq("codigo",…

Cesar Sturion
- 147
- 1
- 9
4
votes
1 answer
GORM create criteria - Using 'having' clause of SQL
I have a dummy cafeteria project wherein I have three domain classes: User, Product and Transaction.
Here's how the classes are defined:
class User {
String name
int employeeId
long balance = 800
static constraints = {
…

SAKSHI SINGHAL
- 51
- 5
4
votes
3 answers
How do I get NHibernate to do a join?
I've used Fluent NHibernate to hook up a store and employee class where Stores can have many employees as follows:
public class Store
{
public virtual IList Employees { get; set; }
//other store properties
}
public class…

mezoid
- 28,090
- 37
- 107
- 148
3
votes
1 answer
Caused by: java.lang.NoSuchMethodError: createCriteria grails
I'm getting the NoSuchMethodError on createCriteria method in Grails. I don't know somehow it is not recognizing the method. I have the createCriteria in other projects, but it is not working here.
Domain.where { }
Above is also not working. Here…
user5441083
3
votes
1 answer
Grails Projections not returning all properties and not grouped
How to get it so i return all of the projections from the below
def c = Company.createCriteria()
def a = c.list(params){
projections{
property 'id', property 'name'
}
}
if(a.size() == 0)
render "404"
else {
render…

Tyler Evans
- 567
- 1
- 8
- 25