Grails gorm: queries with map - grails

Having the following domain class:
class Word {
Map translations
}
And instances in BootStrap:
def word1 = new Word().with{
translations = [en:"game"]
save(failOnError: true, flush: true)
}
def word2 = new Word().with{
translations = [en:"life"]
save(failOnError: true, flush: true)
}
What is the groovy way to get all words, where translation starts with startPart in some locale? For example:
def listWordsStartsWith(startPart, locale, params){
def regexp = startPart+'%'
def query = Word.where {
//translations[locale] =~ regexp
}
def words = query.list(params)
words
}

I don't think this is possible with GORM using a collection value for the translations field. I'd suggest the following alternate mapping:
class Word {
static hasMany = [translations: Translation]
}
class Translation {
static belongsTo = Word
String key
String value
}
Then the query would be something like this:
Word.createCriteria().list {
translations {
like('key', startPart+'%')
}
}

The where wethod relies on Groovy SQL, where you have a very small subset of Groovy. It maps the SQL commands, but allow IDE controls on the properties and well formed syntax.
Unfortunately, you cannot write all the Groovy inside (no function, few operators, no map, etc.).
Check the documentation to see what is accepted.

Related

Grails createCriteria not returning any results

I'm trying to use the createCriteria in a grails application to return some rows from a DB. I'm not getting any results.
def query = {
ilike('fullName','%${params.term}%')
projections {
property('id')
property('fullName')
}
}
def plist = Patient.createCriteria().list(query)
def patientSelectList = []
plist.each {
def pMap = [:]
pMap.put("id", it[0])
pMap.put("label", it[1])
pMap.put("value", it[1])
patientSelectList.add(pMap)
}
the fields i'm looking for exist as the following snippet returns results but is very slow.
def patientList = Patient.getAll()
def patientSelectList = []
patientList.each { patient ->
if (patient.fullName.toLowerCase().contains(params.term.toLowerCase()) ) {
def pMap = [:]
pMap.put("id", patient.id)
pMap.put("label", patient.fullName)
patientSelectList.add(pMap)
}
}
return patientSelectList
thanks for anyhelp
I had my Db fields encryted with jasypt. Removing the encryption on the field I needed to query fixed the issue!
You're using a String rather than a GString in the ilike() parameter, so params.term is not being evaluated. To use a GString, use double-quotes instead.
ilike('fullName',"%${params.term}%")
Also make sure % is an appropriate wildcard character for your database (it probably is).

GORM where query on an embedded object

I have domain classes A and B as follows:
class A {
String prop1
String prop2
B prop3
static embedded = ['prop3']
}
class B {
String prop4
String prop5
}
When I want to query like this:
def q = A.where { prop3.prop4 == 'bla' }
def list = q.list()
I get the following exception:
Cannot get property 'javaClass' on null object. Stacktrace follows:
on the "def q = A.where ..." line.
Any clue what's the problem? I've checked this:
http://grails.1312388.n4.nabble.com/GORM-embedded-object-issue-td1379137.html
but how to "just call them directly" is not quite clear to me. Any other way of querying the embedded objects in GORM?
I finally gave up on the where query and went with the DetachedCriteria approach. Gives me the same flexibility as the where queries, but works with embedded domain objects:
def criteria = new DetachedCriteria(A).build {
eq 'prop1', 'bla2'
}
criteria = criteria.build {
eq 'prop3.prop4', 'bla'
}
def list = criteria.list()
What do you get if you do (assuming B is in src/groovy)
def q = A.where { prop3 == new B(prop4: 'bla') }
def list = q.list()
Embedded components are persisted inside the main domain class (owner) itself. It can be accessed directly using any dynamic finder as you do directly on a domain object.
The above can also be represented in dynamic finders as:
A.findAllByProp3(new B(prop4: 'bla'))

Grails findBy multiple columns Ilike

I have a simple Tag class with only two fields, name and value,
class Tag {
String name
String value
}
and I'm trying to render an XML where I want to search for parts of both parameters via findBy...Ilike().
def getXml = {
render Tag.findAllByNameAndValueIlike("%${params.name}%", "%${params.value}%") as XML
}
But this doesn't give my any results. If I use only one parameter, it works as I expect:
def getXml = {
render Tag.findAllByNameIlike("%${params.name}%") as XML
}
My next question is probably going to be about filtering the results, and adding other "similar" tags to the returns list, so is there a way to solve the above with something like:
def getXml = {
list = Tag.findAllByNameIlike("%${params.name}%")
list.add(Some other stuff)
list.sortBy(Some thing, maby name length)
}
For your multiple-field ilike query you can use withCriteria:
def result = Tag.withCriteria {
ilike('name', "%${params.name}%")
ilike('value', "%${params.value}%")
}
This will return a list of Tag domains whose name matches the provided name and value matches the provided value.
The Criteria DSL will probably let you do most of the filtering you need, but you can also consider using some of the Groovy collection examples here.
You have to put the restrictions(InList, NotNull, etc) on each field of a dynamic finder. If you do not, it assumes equals. Here is what you were looking for:
Tag.findAllByNameIlikeAndValueIlike("%${params.name}%", "%${params.value}%")
Both answer are good. I tried both, but I have to say I like the withcCritia the best. It seems very flexibly.
def result = Tag.withCriteria {
if(params.name != null)
ilike('name', "%${params.name}%")
if(params.value != null)
ilike('value', "%${params.value}%")
}
result.add(new Tag('name': "something"))
render result as XML

Grails Gorm MongoDB index Embedded String Map

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?

Grails criteria builder

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.

Resources