2012-04-10 32 views
0

我有三個域類UserEmployeeUserEmployeeGrails錯誤的別名導致錯誤ORA-00904

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來聲明密鑰。

回答

0

好吧,我改變了查詢:

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

您在員工和用戶中使用「id cloumn:」。必須是「列」

+0

只需列映射將明白,這是我的PK?您可以看到「id列」[在文檔中引用](http://grails.org/doc/latest/guide/GORM.html#databaseIndices)。更具體地說:我正在映射一個已經存在的數據庫,並且不能將PK更改爲Grails默認值。 – 2012-04-10 13:44:11

+2

@SérgioMichels他說你有一個錯字,我想。 – AHungerArtist 2012-04-10 13:50:30

+0

@AHungerArtist哦,是的,確實有一個錯字,但錯誤仍然:( – 2012-04-10 16:14:20