简体   繁体   中英

How to associate new domain class using .properties?

I have the following classes:

class Employer {
    static hasMany = [employees: Employee]
}

class Employee {
    String name
    static belongsTo = [employer: Employer]
}

I try to save some JSON from the front end (the actual code is a bit more dynamic):

params = {
    employer: 1,
    name: 'Test'
}

def save = {
    def employee = new Employee()
    employee.properties = params;
    employee.save()
}

However, the save fails because the employer cannot be set from the id. (Failed to convert property value of type 'java.lang.Integer' to required type 'Employer') Is there a way to make that work?

The structure of your JSON is incorrect. It would need to be something more like:

'employer':{
  'class':'Employer','id':1
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM