Grails错误别名导致错误ORA-00904

时间:2012-04-10 00:35:07

标签: java oracle grails gorm ora-00904

我有三个域类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输出是这样的:

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_

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

修改

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

2 个答案:

答案 0 :(得分:2)

您在员工和用户中使用“id column:”。必须是“专栏”

答案 1 :(得分:0)

好吧,我将查询更改为:

Employee.createCriteria().list() {
  projections {
    userEmployees {
      eq('user', someUser)
    }
  }
}