Noampz
Noampz

Reputation: 1205

HQL and hasMany property

I have this objects:

class Employee {

    String firstName
    String lastName
    String username
    String password
    String email

and:

class HappyHourGroup {

    enum EventEnum {
        HAPPY_HOUR, RESTAURANT_CLOSE, OTHER
    }

    String name
    Employee creator
    Date date
    Double recieveBack
    Double totalCost
    EventEnum event

    static hasMany = [employees: Employee]

I want to get for a given employee object, the latest group (date) he is in the employees of this group. I tried few HQL but it wasn't work at all. what can I do to achieve this?

Thanks

Upvotes: 2

Views: 140

Answers (1)

Mr. Cat
Mr. Cat

Reputation: 3552

If I understood you right, you need many-to-many relation, so add

static hasMany = [groups: HappyHourGroup]
static belongTo = HappyHourGroup

to Employee. Note, that belongTo may go to HappyHourGroup? that depends on what cascade behaviour you want. To get the list of groups try this code:

Employee employee = employees.findBy.....
HappyHourGroup.withCriteria {
  employees {
    eq('id',employee)
  }
  maxResults(1)
  order("date", "desc")
}

See criteria for more info.

Upvotes: 1

Related Questions