I have the folowing:
class Store{
String name
}
class Shop{
String name
Store store
}
My criteria builder:
def c = Shop.createCriteria()
def results = c.list {
like("name", "Harrods")
like("store.name", "McDonals")
}
I'm sure this is invalid cause i'v tested it. How can i manage to use criteriaBuilder and do this: like("store.name", "McDonals")?
Looking forward to get any help,
John
Since you're querying an association, try:
def results = c.list {
like('name', 'Harrods')
store {
like('name', 'McDonals')
}
}
This will do an conjoined query between name and store.name.
Check out the documentation Looks like you need to use a % for your like clause.
Related
New to groovy, grails.
I have the following query where I want to match a few paramater exactly to as passed(eq), but for one, which I want to using a 'like'
if (params.ret_code) {
ret_cod = params.ret_code+"%"
}
def srchresults = DmnObj.where {
if (params.doc_num) { doc_num == params.doc_num.trim() } //works as expected
//How do I do this????
if (params.retn_code) { retn_code like ret_cod }
}
Tried this, but in-vain.
how do I set retn_code with a like?
Thank you!
This is how you can do this
// case sensitive like
def result = Domain.where {
fieldName ==~ "value"
}
// case insensitive like
def result = Domain.where {
fieldName =~ "value"
}
Remember to prefix, suffix or both the value with %. For more about where queries https://grails.github.io/grails-doc/latest/guide/GORM.html#whereQueries
You can do like this:
var feel []DB.Feelings
db.Where("column_name LIKE ?", "%"+yourText+"%").Limit(your_limit).Offset(your_offset).Find(&feel)
column_name is your column name
Limit and Offset are not required
You can do like this:
def result = DomainClass.where{
like('fieldName', '%'+myVariable+'%')
}
I suggest you have a look at the Grails documentation (section Domain Class Usage): you will find several interesting ways to filter your domain class objects as for example HQL queries or findAllBy* dynamic methods.
The next code works:
self query1 = DomainClassExample.findAllByPropertyInList("hi","bye")
But if I add Not, the dynamic finder does not exist (it DOES exists: check the answer):
self query2 = DomainClassExample.findAllByPropertyNotInList("hi","bye")
I want to get all the DomainClassExample which don't have the property "hi" or "bye". Is there any dynamic finder for that purpose? Should I use the Where Query. How?
First, a minor correction. It's not a method expression. It's a dynamic finder. Now, as to your actual problem, you'll need to use the criteria API for this...
def c = DomainClassExample.createCriteria()
def matchingProperties = c.list {
property {
not { 'in'(["hi","bye"]) }
}
}
You might run into an issue with the word 'property' and I haven't actually tested the code I just wrote. But that is the gist of it.
In Grails 1.3.9, given the following domain:
class User {
String username
....
}
this works in our application without any issues:
def userList = User.findAllByUsernameNotInList(["user1#testdomain.com","user2#anotherdomain.com"])
I've a scenario to pass a List of char values to the query in Grails.
When I pass the List this is what happening
def accounts = ['A', 'B']
AND acct.account_status_cd in '${accounts}
the query looks like "AND acct.account_status_cd in '[A,B]'"
but it should be "AND acct.account_status_cd in ('A','B')"
how to achieve this?
Resolved it by Passing as a String...
String s = keys.collect { "'$it'" }.join( ',' )
It gives me S = 'A','B','C'
A solution could be a NamedQuery inside your domain class, something like this:
class Account {
// code omitted
static namedQueries = {
accountsInList { accountList ->
'in'("account_status_cd", accountList)
}
}
Then you can use this namedQuery like:
Account.accountsInList(['A', 'B']).list()
Ofcourse you can also use a withCriteria.
See the docs for more info:
http://grails.org/doc/2.2.x/ref/Domain%20Classes/createCriteria.html
I wanted to make some translatable content with rest service so i decided to create collection with this structure. But I can't find BSON by value from String Map.
class LocalizableString{
static mapWith = "mongo"
ObjectId id
Map<String, String> values = new HashMap<String, String>();
}
Then i wanted to get like this. But it works like join query.
def list = LocalizableString.createCriteria().list {
values{ like('value',"%${value}%") }
}
Here is similar plain mongo example. But how can i implement it with gorm mongoDB http://www.mongodb.org/display/DOCS/Schema+Design#SchemaDesign-Example
Is there any solution for that ?
class BaseService {
def findByLocalizableString(def domainClass ,def query , def field ,def params = null) {
def q = new BasicDBObject()
def queryList = []
def allowedLanguages = ConfigurationHolder.config.grails.localizableString.allowedLanguages
allowedLanguages.each { locale ->
queryList.add(new BasicDBObject("values.${locale}", new BasicDBObject('$regex', /.*${query}.*/)))
}
q.put('$or',queryList)
def lsc = LocalizableString.collection.find(q)
def list = lsc.hasNext() ? domainClass.createCriteria().list(params) {
or {
while (lsc.hasNext()) {
def n = lsc.next()
eq("${field}",n._id)
}
}
} : null
return list
}
}
I'm not 100% on this, but I'm fairly certain the Mongo GORM plugin does not work with criteria relation traversal, which is what this looks like (despite not really being like that).
From mongoGorm website (http://blog.mongodb.org/post/18510469058/grails-in-the-land-of-mongodb):
Some of the GORM features that are not supported include:
Criteria queries on associations
HQL
Groovy SQL
So you may need to rethink the Map structure you have as a data model here :/ Anyone more experienced can weigh in?
Is this possible to convert in createCriteria()?
SELECT * FROM node WHERE (node.type = 'act' AND nid NOT IN (SELECT nid FROM snbr_act_community)) LIMIT 10
I know there's a 'in' operator and here's what I have so far:
def c = VolunteerOpportunity.createCriteria()
def matchingActs = c.list {
node {
eq('type', 'act')
}
maxResults(10)
}
Just want to see if this is possible. Otherwise, I guess this is possible in HQL right?
thanks Sammyrulez for the code. got an idea from that. tested it but it didn't work. i fixed it and here's the final working code:
def ids = [14400 as long, 14401 as long]
def c = VolunteerOpportunity.createCriteria()
def matchingActs = c.list {
node {
eq('type', 'act')
not { 'in'(ids) }
}
maxResults(10)
}
now i know how to use 'not' operator. thanks a lot!
not tried it myself but looking at the Grails doc and hibernate api you create nodes on this builder map with the static methods found in the Restrictions class of the Hibernate Criteria API 1. So something like
def c = VolunteerOpportunity.createCriteria()
def matchingActs = c.list {
node {
not(in('propertyName', ['val1','val2']))
}
maxResults(10)
}
Since you chain the in method (that returns a Criterion) with the not method (that takes a Criterion as argument and returns a negated version)
this is the solution :
def resultat=EnteteImputationBudgetaire.createCriteria().get{
between("dateDebutPeriode", dateDebut, dateFin)
and{ eq 'natureImputationBudgetaire','FONCTIONNEMENT' }
maxResults(1)
}
def resultat2=ParametragePlanBudgetaire.createCriteria().list() {
like('composantBudgetaire','6%')
if(resultat?.details) {
not {
'in'('composantBudgetaire',resultat?.details?.imputationBudgetaire)
}
}
}
According to Grails documentation about creating criteria here, you can use something like this:
not {'in'("age",[18..65])}
In this example, you have a property named "age" and you want to get rows that are NOT between 18 and 65. Of course, the [18..65] part can be substituted with any list of values or range you need.
Just remembering: in this case you don't have to use parenthesis and you can use inList, for example:
not { inList 'age',[18..65] }