开发者

Grails Filtering and Ordering across Multiple Domain classes

开发者 https://www.devze.com 2023-02-11 22:05 出处:网络
I am using grails and I am having trouble finding the appropriate syntax for joining and ordering across different domains.For example, given the example below, I would like to retrieve a page of book

I am using grails and I am having trouble finding the appropriate syntax for joining and ordering across different domains. For example, given the example below, I would like to retrieve a page of book data sorted by title for all authors that come from (eg) London. I have a preference for using createCriteria, but will use another technique if needed.

class Location {
    String city
    static hasMany = [authors: Author]
}

class Author {
    String name
    static belongsTo = [location: Location]
    static hasMany = [books: Book]
}

class Book {
    String title
    static belongsTo = [author: Author]
} 

To clarify, what I want to achieve would be to get a list of book domain classes that is equivalent to something like

Select Book.title
From Book
Inner Joi开发者_Go百科n Author
On Author.name = Book.authorName
Inner Join Location
On Location.city = Author.homeCity
Where Location.city = 'London'
Order by Book.title

Thanks


For creating more complex database queries you can use [criteria objects][1] or [Hibernate Query Language (HQL)][2]. Second way is more powerful but less comfortable.

[1]: http://grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20(GORM).html#5.4.2 Criteria

[2]: http://grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20(GORM).html#5.4.3 Hibernate Query Language (HQL)

0

精彩评论

暂无评论...
验证码 换一张
取 消