Last updated by admin 4 years ago
????????????? {excerpt:hidden=true} Domain Class Querying {excerpt}
???????????????? {excerpt:hidden=true} Querying with dynamic methods {excerpt}
{excerpt:hidden=true} There are several ways to query for domain class instances, one of them being via Grails dynamic methods, from more details see DomainClass Dynamic Methods:{excerpt}???????????????????Grails????????????????????????????DomainClass Dynamic Methods??????????def results = Book.findByTitle("The Stand")results = Book.findByTitleLike("Harry Pot%") results = Book.findByReleaseDateBetween( firstDate, secondDate ) results = Book.findByReleaseDateGreaterThan( someDate ) results = Book.findByTitleLikeOrReleaseDateLessThan( "%Something%", someDate )// find by relationship results = Book.findAllByAuthor( Author.get(1) )
?????? {excerpt:hidden=true} Querying by example{excerpt}
{excerpt:hidden=true} Just pass an example of the domain object you would like to find to the find() method. {excerpt}find()???????????????????????? def b = Book.find( new Book(title:'The Shining') )criteria builder?????? {excerpt:hidden=true} Querying with a criteria builder {excerpt}
{excerpt:hidden=true} For more advanced queries or querying across objects graphs you can use Criteria (for a full reference see the section on Builders): {excerpt}?????????Criteria??????????????????????????????????def c = Book.createCriteria()
def results = c {
like("author.name", "Stephen%")
between("releaseDate", firstDate, secondDate )
}HQL?????? {excerpt:hidden=true} Querying with HQL queries {excerpt}
{excerpt:hidden=true} Otherwise, as Grails uses Hibernate internally you can use an HQL query: {excerpt}?????Grails?Hibernate?HQL?????????????? def results = Book.find("from Book as b where b.title like 'Lord of the%'")def results = Book.find("from Book as b where b.title like ?", ["The Shi%"])



