Grails具有belongsTo和默认值的多个域类

时间:2012-07-11 14:32:59

标签: grails gorm grails-domain-class grails-2.0

我有域类结构的特殊情况,其中三个类呈现并且它们连接到链:事件< - 房间< - 投影仪。 (所有关系都是一对一的)

实现如下:

class Event {
    Room room
    static constraints = {
        room(nullable:false)
    }
}
class Room {
    Projector projector = new Projector()
    static belongsTo = [event: Event]
    static constraints = {
        projector(nullable:false)
    }
}
class Projector {
    String something = "Something"
    static belongsTo = [room: Room]
    static constraints = {
        room(nullable:false)
    }
}

当我想用新房间创建活动时,我希望默认情况下会创建投影仪:

class TestController {
    def index() {
        Room room = new Room()
        Event event = new Event(room: room)
        event.save(flush: true, failOnError: true)
        render event
    }
}

我收到以下异常

| Error 2012-07-11 16:09:12,541 [http-bio-8080-exec-3] ERROR errors.GrailsExceptionResolver  - TransientObjectException occurred when processing request: [GET] /Test/room/index
object references an unsaved transient instance - save the transient instance before flushing: Projector. Stacktrace follows:
Message: object references an unsaved transient instance - save the transient instance before flushing: Projector
    Line | Method
->>   46 | onApplicationEvent in org.grails.datastore.mapping.engine.event.AbstractPersistenceEventListener
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     18 | index              in RoomController
|   1110 | runWorker . . . .  in java.util.concurrent.ThreadPoolExecutor
|    603 | run                in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run . . . . . . .  in java.lang.Thread

似乎级联三个类不会保存默认值。那有什么解决方案吗?或者我做错了什么?

1 个答案:

答案 0 :(得分:0)

http://jira.grails.org/browse/GRAILS-4613?attachmentOrder=desc

为您的问题提供了一个公开的jira

我个人会在控制器中处理对象的创建,而不是在域中进行。你需要能够调用projector.save(flush:true)。