Pat
Pat

Reputation: 167

Update in domain class is not stored into the database

I've got a little problem in storing modified domain classes into the database. I have got a job that has two subjobs (start and end). I would like to set a flag in these subjobs and my masterjob (myFlag) to true and save these values into the embedded h2 database. Somehow only the masterjob and the second subjob (end) is stored correctly into the database, but not the subjob called start.
Here is the code written in the service:

def markJob(masterjob) {

        def startjob= masterjob.startjob
        startjob.myFlag = true
        startjob.save flush:true

        def endjob = masterjob.endjob 
        endjob.myFlag = true
        endjob.save flush:true

        masterjob.myFlag = true
        masterjob.save flush:true

        println masterjob.myFlag + ' ' + startjob.myFlag + ' ' + endjob.myFlag
}

All of these domains have such a boolean variable called myFlag, which are set to false by default. If I check the database afterwards both the endjob and masterjob have the flag set to true, but not the startjob. The console log will be 'true true true'. I also tried to not save the jobs, but merge them instead, which did not work either.

I call this method via the controller:

@Transactional
def delete(Job jobInstance) {
    jobService.markJob(jobInstance)
}

The controller is called within the view:

<g:link action="delete" id="${jobInstance.id}"> .... </g:link>

Does anybody have an idea considering this issue?

Every help will be appreciated, thanks in advance !

SOLVED

The answer of kiview did it. I changed the service method like this:

def markJob(masterjob) {
        masterjob.startjob.myFlag = true
        masterjob.endjob.myFlag = true
        masterjob.myFlag = true
        masterjob.save flush:true
}

Upvotes: 0

Views: 273

Answers (1)

Kevin Wittek
Kevin Wittek

Reputation: 1572

Have you considered adding a cascading save to the master job? I think what you want is the belongsTo-relationship:

http://grails.org/doc/latest/ref/Domain%20Classes/belongsTo.html

Also you should remove the @Transactional of your controller and use the transaction provided by the service method (it's the default service behavior if not configured otherwise).

Upvotes: 1

Related Questions