0

如果我有四个这样的域类:

class Branch {
    String name
    static hasMany   = [users:Users] 
    static mappedBy  = [users:'branch']   
    static mapping = {                
        id          column: 'f_branch_id'
        name        column: 'f_name'
    }
}
class Users {
    String name
    static hasMany  = [account:Account]
    static mappedBy = [account:'user']
    static belongsTo= [branch:Branch, title:Title]
    static mapping = {
        id          column: 'f_user_id',
        name        column: 'f_name',   
        branch      column: 'k_branch_id'
    }
}
class Account {
    String username   
    static belongsTo   = [user:Users]   
    static mapping = {
        id              column: 'f_account_id'
        user            column: 'f_user_id'
        username        column: 'f_username'
    }
}
class JoinTable implements Serializable {
    Account account
    Role role    
    static mapping = {
        id              composite   : ['role', 'account']
        role            column      :'k_role_id'
        account         column      :'k_account_id'
        version false
    }    
}

如何使用条件查询从 JoinTable 获取分支我尝试此过程但因别名问题而失败

 def criteria = JoinTable.createCriteria()
     def list = criteria.list {             
         account {
                user{
                    branch{
                        eq("id", "2")
                    }
                }
         }
    }
4

1 回答 1

0

class Branch {
    String name
    static hasMany   = [users:Users]
    static mapping = {
        id          column: 'f_branch_id'
        name        column: 'f_name'
    }
}
class Title {
    ...
}
class Users {
    String name
    static hasMany  = [account:Account]
    static belongsTo= [branch:Branch, title:Title]
    static mapping = {...}
}
class Account {
    String username
    static belongsTo   = [user:Users]
    static mapping = {...}   
}
class Role {
   ...
}
class JoinTable implements Serializable {
    Account account
    Role role
    static mapping = {
        id              composite   : ['role', 'account']
        role            column      :'k_role_id'
        account         column      :'k_account_id'
        version false
    }
}

测试

@TestMixin(GrailsUnitTestMixin)
@Mock([Act, Branch, Title, Users, Account, Role, JoinTable])
class EaseTests {
void testCriteria(){
        10.times{
            def b = new Branch().save(validate:false, flush:true)
            10.times{ 
                def u = new Users(branch:b).save(validate:false, flush:true) 
                10.times{
                    def a  = new Account(user:u).save(validate:false, flush:true) 
                    def joinTableRow  = new JoinTable(account: a).save(validate:false, flush:true)
                }
            }          
        }    

        def c = JoinTable.createCriteria()
        def results = c{
            account{
                   user {
                       branch{
                           idEq(2l)
                       }
                   }
           }
        }
        assert results
    }
}
于 2013-10-08T07:10:05.650 回答