I have special case of domain classes structure where three classes presents and they are connected to chain: Event <- Room <- Projector. (All relation ships are one-to-one)
The implementation looks like:
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)
}
}
When I want to create Event with new Room I expect that the Projector will be created by default:
class TestController {
def index() {
Room room = new Room()
Event event = new Event(room: room)
event.save(flush: true, failOnError: true)
render event
}
}
I receive following exception
| 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
It seems that cascading over three classes doesn't save default values. Is there any solution for that? Or what am I doing wrong?
Projector projector = new Projector()
to justProjector projector
and changeRoom room = new Room()
toRoom room = new Room(projector:new Projector())
There are some methods that get meta-programmed onto domain classes. It could be that by directly assigning a value for Projector inside the domain class, you are bypassing the hook that handles cascading saves. – profluxthis.projector = new Projector()
in the constructor. – proflux