tags:

views:

231

answers:

1

Hi,

When I write the following class, I get the following compilation error:

could not resolve property

How can I achive the following:

class Employee{
  String Name
  String Email
  Employee Manager
  static hasMany = [desginations:Designation]    

  static constraints = {
  Name(unique:true)
  Email(unique:true)
  }

Thanks, Much appreciated.

+4  A: 

GORM can be picky about following its naming convention. In particular, field names should be camelCase, starting with a lower case letter.

With the following definition:

class Employee {
    String name
    String email
    Employee manager

    static constraints = {
        name(unique:true)
        email(unique:true)
        manager(nullable:true)
    }
}

I can create an employee with a manager like so:

manager = new Employee(name: 'manager', email: '[email protected]')
manager.save()
employee = new Employee(name: 'employee', email: '[email protected]')
employee.manager = manager
employee.save()

Edit: As fabien7474 noted, the important part is the manager(nullable:true) constraint. This allows employee records to be saved without assigning a manager. In the above example, the employee named manager is employee's manager, but manager itself doesn't have a manager. This is represented by a NULL value in the manager column in the database.

ataylor
Thanks mate for your reply,However, In case if I have to Edit a employee who I assigned as Manager, in that case, Grails doesnt let me assign a null value to the manager. which leads to a problem that a Manager has to be assigned a Manager.I hope you understand what I meanthis only happens when I edit a Manager.
WaZ
Should not occur if you assign the constraints manager(nullable: true)
fabien7474
Sorry forgot to mention, I did have the manager(nullable:true) in my employee domain.I do not see an empty option in the list box of manager when I edit a Manager. I am forced to assign a manager to the one I am editing.
WaZ
The auto generated scaffold view will allow you to select blank for the manager field once the nullable constraint is added. If you've customized the views add a noSelection="${['null':'']}" to the manager g:select tags in the view gsps. If not, just run "grails generate-views Employee" to recreate the views from scratch.
ataylor
Thanks a lot mate,`noSelection` thats exactly what I was looking for.Cheers.
WaZ