Search code examples
grailsgrails-ormcriteriahibernate-criteria

How to make a search using key/value pairs table?



I have 2 domain classes

class Post {
    String name
}

class PostMeta { 
    String key 
    String value 
    Post post 
}

What I want to do, is a search using only one field "query", that returns all the Posts where "value" of PostMeta match with the query. The list of Posts must not contains duplicated elements


Solution

  • Try this

    def findPostsByValue(String criteria) {
       render PostMeta.createCriteria().list {
         projections {
            distinct("post")      
         }
    
         ilike "value", "%${criteria}%"
       }*.name
    }