【问题标题】:Grails wrong alias causing error ORA-00904Grails 错误别名导致错误 ORA-00904
【发布时间】:2012-04-10 00:35:07
【问题描述】:

我有三个域类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_

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

编辑

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

【问题讨论】:

    标签: java oracle grails grails-orm ora-00904


    【解决方案1】:

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

    【讨论】:

    • 只有列映射就会明白这是我的PK?您可以看到“id 列”is referenced in the docs。更具体地说:我正在映射一个已经存在并且不能将 PK 更改为 Grails 默认值的数据库。
    • @SérgioMichels 我想他是在说你打错字了。
    • @AHungerArtist 哦,是的,确实有错字,但错误仍然存​​在:(
    【解决方案2】:

    嗯,我把查询改成:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-04
      • 2015-02-12
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多