我有三个域类User
,Employee
和UserEmployee
.
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
来声明密钥。