0

と の 3 つのドメイン クラスUserEmployeeあり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キーを宣言するために使用しています。

4

2 に答える 2

2

Employee と User で「id clumn:」を使用します。「列」である必要があります

于 2012-04-10T06:05:14.030 に答える
0

さて、クエリを次のように変更しました。

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