In Grails 2.5.4, I am having issues using a disjunction in Subqueries. If I have a query like the following:
DomainObj.createCriteria().list {
def criteria = new DetachedCriteria(DomainObj2).build {
or {
eq('prop1', someVal)
eq('prop2', someVal)
eq('prop3', someVal)
}
projections {
distinct('id')
}
}
inList('prop', criteria)
}
The 'or' part of the query fails with a null pointer exception. The reason seems to be in AbstractHibernateCriterionAdapter the code is looking for a PersistentEntity for the DetachedCriteria which is never assigned.
The only workaround I have found is to switch the query to use more subqueries like this:
def criteria1 = new DetachedCriteria(DomainObj2).build {
eq('prop1', someVal)
projections {
distinct('id')
}
}
def criteria2 = new DetachedCriteria(DomainObj2).build {
eq('prop2', someVal)
projections {
distinct('id')
}
}
def criteria3 = new DetachedCriteria(DomainObj2).build {
eq('prop3', someVal)
projections {
distinct('id')
}
}
DomainObj.createCriteria().list {
or {
inList('prop', criteria1)
inList('prop', criteria2)
inList('prop', criteria3)
}
}
Which sidesteps the issue, and really isn't ideal. Any idea what is going wrong?
Update
So after looking around some more I found this issue on Github. What I am experiencing is a bug that was fixed in grails-data-mapping version 5.0.2. So for anyone that searches for this issue in the future it looks like the you either have to upgrade or use the crazy workaround highlighted above.
You could probably simplify your above working nest to:
private DetachedCriteria getCriteria(prop,val) {
return new DetachedCriteria(DomainObj2).build {
eq(prop,val)
projections {
distinct('id')
}
}
DomainObj.createCriteria().list {
or {
inList('prop', getCriteria('prop1','somVal'))
inList('prop', getCriteria('prop2','somVal'))
inList('prop', getCriteria('prop3','somVal'))
}
}
Personally I would probably either just do a findAll or just run an hql query, if it turns out you can't use the current method due to some limitation since am no expert on this matter itself.
//where d.domainObject2 is the binding of DomainObj2 within DomainObj
String query="select new map(d.id) from DomainObj d left join d.domainObject2 d2 where d2.field in (:someList)"
def input=[]
input.someList=[1,2,3] //The or segments
def results=DomainObj.executeQuery(query,[],[readOnly:true,timeout:15,max:-1])
Related
So I've got a couple of classes with the following relationship:
class Foo {
Bar bar
/* ... other fields ... */
}
class Bar {
String name
}
In class Foo I've got a couple of named queries:
static namedQueries = {
userFoos { user ->
/* ... get Foos for this user ... */
}
limitFoos { colname, dir ->
order(colname, dir)
}
...which I can then chain together in a controller:
def foos = Foo.userFoos(currentUser).limit(colname, dir)
Pretty straightforward so far. The problem is when I try to sort on bar; I get the error:
could not resolve property: bar.name of: package.Foo.
Now, I also got this error when the queries were Criteria that were declared in the controller. So, I went and wrote a propertyMissing handler for Foo:
def propertyMissing(String name) {
if (name.contains(".")) {
def (String propertyname, String subproperty) = name.tokenize(".")
if (this.hasProperty(propertyname) && this."$propertyname".hasProperty(subproperty)) {
return this."$propertyname"."$subproperty"
}
}
}
I don't know if this is really the best way to do it, but it did work! However, now that I've moved the query into the class as a named query, propertyMissing doesn't appear to work anymore! Is this use not supported, or am I just missing something here?
EDIT
So I tried moving the Criteria back into the controller and sure enough, the sub-property sort did not work there either! So I guess Criteria just don't support propertyMissing at all :/
To answer dmahapatro's question, I am using jQuery DataTables to present the information. Clicking on a column header does an AJAX call to a controller action with parameters to indicate which column to sort on and in which direction. Once I determine the column name, I call the named queries like so:
def foosFilteredLimited = params.sSearch ?
Foo.userFoos(currentUser).filterFoos(params.sSearch).limitFoos(offset, max, colName, sortDir).list()
: Foo.userFoos(currentUser).limitFoos(offset, max, colName, sortDir).list()
(filterFoos takes a search string and narrows the results of userFoos.)
Try modifying limitFoos namedQuery as below and it should work. There is a caveat to it though. We cannot use bar.baz.name if required. ;)
limitFoos { column, ord ->
def colStrs = column.tokenize(/./).toList()
if( colStrs?.size() > 1 ) {
"${colStrs[0]}" {
order( "${colStrs[1]}", ord )
}
} else {
order(column, ord)
}
}
I have a fairly large criteria closure in my Grails application, and I would like to reuse part of it in several places in my application. Rather than duplicating the section I need to reuse, I'd like to define this as a separate closure and reference it wherever it is needed, but I am struggling a bit with the syntax.
This is a simplified / cut down version, but essentially my criteria looks something like this:
def criteriaClosure = {
and {
// filtering criteria that I'd like to reuse in lots of places
or {
names.each { name ->
sqlRestriction(getFilteringSql(name), [someId])
}
}
if (isOrganisationChild(childDefaultGrailsDomainClass)) {
sqlRestriction(getFilteringSql(domain), [someArg])
}
// filtering criteria that's specific to this particular method
sqlRestriction(getSomeOtherSql(), [someOtherArg])
}
}
def criteria = domain.createCriteria()
def paginatedList = criteria.list([offset: offset, max: max], criteriaClosure)
I've tried defining the part of the closure I want to reuse as a variable, and referencing it in my criteria closure, however the restrictions it defines don't seem to be applied.
def reusableClosure = {
and {
or {
names.each { name ->
sqlRestriction(getFilteringSql(name), [someId])
}
}
if (isOrganisationChild(childDefaultGrailsDomainClass)) {
sqlRestriction(getFilteringSql(domain), [someArg])
}
}
}
def criteriaClosure = {
and {
reusableClosure() //this doesn't seem to work
sqlRestriction(getSomeOtherSql(), [someOtherArg])
}
}
I'm sure this must be a pretty straightforward thing to do, so apologies if it's a daft question. Any ideas?
I think you have to pass the delegate down to the reusableClosure, ie:
def criteriaClosure = {
and {
reusableClosure.delegate = delegate
reusableClosure()
sqlRestriction(getSomeOtherSql(), [someOtherArg])
}
}
i am trying to get a pagedResultList using chained named queries, but i seem to fail.
Any hints or tips on how this can be achieved?
See modified example below from the Grails documentation that should illustrate my needs
def books = Publication.recentPublications.grailsInTitle.list(params) {
or {
like 'author', 'Tony%'
like 'author', 'Phil%'
}
}
This always returns an ArrayList..
When or remove the additional criteria and use it like below it works
def books = Publication.recentPublications.grailsInTitle.list(params)
I would like to add some criteria closures, any hints or tips on how i could achieve this?
I am facing same problems with named queries. This is my solution applied to your classes. Comment if it works for you.
class Publication {
//fields, constraints, etc.
namedQueries = {
authoredLike { String authorName ->
if (authorName) {
like 'author', authorName
}
// untested, but you get the point, please experiment
authoredLikeMany { List<String> authors ->
authors.each { String authorName -> like 'author', authorName }
}
}
}
def tonyBooks = Publication.recentPublications.grailsInTitle.authoredLike('Tony%').list(params)
def tonyAndPhilBooks = Publication.recentPublications.grailsInTitle.authoredLikeMany(['Tony%', 'Phil%']).list(params)
Assume a domain class called User. User class looks like this:
class User {
List<String> values
}
The collection values contains strings like "http://example.com/x", "http://google.com/y", "http://google.com/z" and so on...
Let's say we want to build a query which gets all the users that have specific string in the collection values (e.g. "google.com"). Something like this:
def criteria = User.createCriteria()
def results = criteria.listDistinct () {
and {
user.values.each { like('someField', '%${it}%') }
}
}
Any ideas?
I have found the answer by experimentation. The solution is:
def criteria = User.createCriteria()
def results = criteria.listDistinct () {
and {
user.values.each { like('someField', '%'+it+'%') }
}
}
I am not sure what you are doing with your suggested answer.
I have never seen that usage of each in the criteria query before.
This question has been asked many times before but never given an answer.
The problem is that you are queriyng a String association, which is not a domain class. If you would make your own String domain class for example ondrej.String { String strValue } then you would be able to do :
User.withCriteria {
values { ilike("strValue", "...") }
}
The problem is not having access to the value of the String object. The value of the String class is called value, but it is a char array, so I do not believe the following will work:
User.withCriteria {
values { ilike("value", "...") }
}
You could try using :
User.withCriteria {
values { ilike("toString", "...") }
}
or something else instead of toString ... I do not have the possibility to test this right now.
Hope that helps
After a lot of trying and researching, I found this will work with Grails 2.4.0, I don't know about older versions.
Cat.withCriteria {
createAlias('nicknames', 'n')
ilike 'n.elements', '%kitty%'
}
The trick is to use 'n.elements'
I am facing a problem to get the required result from this closure
def authors{
results = Message.createCriteria().list {
projections {
author{
groupProperty('id', 'authorId') // 2nd param is alias
property('username', 'username')
}
}
and{
...
...
}
}
[authors:results]
}
I want to show this list on my gsp page
and wants to access the values using aliases
(while above criteria is returning a list of arrays)
Use resultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP).
import org.hibernate.criterion.CriteriaSpecification
Message.createCriteria().list {
resultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP)
projections {
author{
groupProperty('id', 'authorId')
property('username', 'username')
}
}
}
All projections must have aliases. Otherwise the resulting map will contain nulls.
You can try this
def authors{
results = Message.createCriteria().list {
projections {
author{
groupProperty('id')
property('username')
}
}
and{
...
...
}
}
List authors = results.collect{record -> [authorId : record[0], username:record[1]}
[authors:authors] }