2012-03-06 62 views
0

假設我有兩個名爲User and Authority的類。Grails雙向查詢具有多個關聯

這兩個類的規格是:

User{ 
    Integer id; 
    String userCode; 
    String password; 
    boolean active; 

    static hasMany = [authorities : Authority, userGroups : UserGroup] 

    static mapping = { 
     table("security_user") 
    } 
} 


Authority{ 
    Integer id 
    String roleTitle 
    String description 

    static hasMany = [features : Feature, users : User] 

    static belongsTo = User; 


} 

在查詢級別我怎麼可以得到所有的這些部門與用戶的一個特定的對象映射?

一樣,我曾嘗試在下面的辦法:

user = User.findByUserCodeAndPassword(userCode,password); 
Set<User> users = new HashSet<User>(); 
users.add(user); 

List<Authority> authority = Authority.findAllByUsers(users); 

但上面的代碼是給運行Grails的例外。我該如何解決這個問題?

回答

2

只要使用user.authorities獲取用戶對象後不需要做所有這些事情。