0

我有三个域类UserEmployeeUserEmployee.

class User {
    String username
    //more atributes omitted 

    static hasMany = [ userEmployees : UserEmployee ]
    static mapping = {
      version false
      table 'myUserTable'
       id column: 'username', name: 'username' 
    }
}

class Employee {
    long employeeCode
    //more atributes omitted 
    static hasMany = [ userEmployees : UserEmployee ]
    static mapping = {
       id column: 'myColumn', name: 'employeeCode' 
       version false
       table 'myViewHere'
    }
}

class UserEmployee implements Serializable {
    User user
    Employee employee
    static belongsTo = [ user : User,  employee : Employee ]
    static mapping = {
      version false
      table 'myRelationTable'
      id composite: ['user','employee']
      user(column: "username")
      employee(column: "myColumn")
    }
}

当我尝试查询某个用户可以访问的所有员工时,我收到 ORA-00904 错误:

println UserEmployee.withCriteria {
    projections {
       user {
           eq('username', 'SOMEUSER')
       }
    }
}

休眠输出是这样的:

Hibernate: select * from ( select this_.username as usuario27_0_, from myUserTable this_ where this_.username=? ) where rownum <= ?

Hibernate: select this_.username as usuario24_0_, this_.code_employee as cod2_24_0_ from myRelationalTable this_ where (usu_alias1x1_.username=?)

为什么要usu_alias1x1_创建别名?

PS:我更改了域类名称和字段以更好地理解。也许会在某个地方出现错字。

编辑

我正在映射一个已经存在且无法将 PK 更改为 Grails 默认值的数据库。所以我使用id column来声明密钥。

4

2 回答 2

2

您在员工和用户中使用“id cloumn:”。必须是“列”

于 2012-04-10T06:05:14.030 回答
0

好吧,我将查询更改为:

Employee.createCriteria().list() {
  projections {
    userEmployees {
      eq('user', someUser)
    }
  }
}
于 2012-04-13T13:50:21.317 回答