Saving associated domain classes in Grails

亡梦爱人 提交于 2019-12-11 12:28:53

问题


I'm struggling to get association right on Grails. Let's say I have two domain classes:

class Engine {
    String name
    int numberOfCylinders = 4
    static constraints = {
        name(blank:false, nullable:false)
        numberOfCylinders(range:4..8)
    }
}

class Car {
    int year
    String brand
    Engine engine = new Engine(name:"Default Engine")
    static constraints = {
        engine(nullable:false)
        brand(blank:false, nullable:false)
        year(nullable:false)
    }
}

The idea is that users can create cars without creating an engine first, and those cars get a default engine. In the CarController I have:

def save = {
    def car = new Car(params)
    if(!car.hasErrors() && car.save()){
        flash.message = "Car saved"
        redirect(action:index)
    }else{
        render(view:'create', model:[car:car])
    }
}

When trying to save, I get a null value exception on the Car.engine field, so obviously the default engine is not created and saved. I tried to manually create the engine:

def save = {
    def car = new Car(params)
    car.engine = new Engine(name: "Default Engine")
    if(!car.hasErrors() && car.save()){
        flash.message = "Car saved"
        redirect(action:index)
    }else{
        render(view:'create', model:[car:car])
    }
}

Didn't work either. Is Grails not able to save associated classes? How could I implement such feature?


回答1:


I think you need a belongsTo in your Engine ie

static belongsTo = [car:Car]

Hope this helps.




回答2:


For what is worth, I finally nailed it.

The exception I got when trying to save a car was

not-null property references a null or transient value

It was obvious that the engine was null when trying to save, but why? Turns out you have to do:

def car = new Car(params)
car.engine = new Engine(name: "Default Engine")
car.engine.save()

Since engine doesn't belongs to a Car, you don't get cascade save/update/delete which is fine in my case. The solution is to manually save the engine and then save the car.



来源:https://stackoverflow.com/questions/1041992/saving-associated-domain-classes-in-grails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!