Question

I have three domain classes User, Employee and 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")
    }
}

When I try to query all the employees that some user can access, I get the ORA-00904 error:

println UserEmployee.withCriteria {
    projections {
       user {
           eq('username', 'SOMEUSER')
       }
    }
}

The hibernate output is something like this:

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=?)

Why is the alias usu_alias1x1_ created?

P.S.: I changed the domain classes names and fields to better understanding. Maybe will be a typo somewhere.

EDIT

I'm mapping a database that already exists and cannot have the PK's changed to the Grails default. So I'm using the id column to declare the keys.

Was it helpful?

Solution 2

Well, I changed the query to:

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

OTHER TIPS

You use "id cloumn:" in Employee and User. Must be "column"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top