0
votes

When using the save() without flush: true, the instance is in the hibernate session, not in the database. So is there anyway that we can search the instances in the session?

Update: I have a queue of objects and use them to either create or update another object Number:

def inst = getNextOneFromQueue()    
Number num = Number.findWhere(a:inst.a, b:ins.b)
If (num == null){
    num = new Number()   
}
num.a = inst.a
num.b = inst.b
num.save()
ins.delete(flush:true)

So the problem is i have to set flush:true in the delete or save method for each object I have in the queue. Otherwise if object num is created but not flushed to the database, there might be a possibility that an update on num could not find it in the database and create a new one (which is a duplicate).

But flushing every time is really inefficient. I'm thinking about not setting flush:true and search in the hibernate session to do the update. Is this possible? Any Suggestions? Work around?

I did a test. Create a new Number object num, save it without flush and then search for it right away:

Number num = new Number()
num.a = 234
num.save()
def temp = Number.findWhere(a:234) //Number.findByA(234)

temp is NULL. Apparently findWhere/findByA is search in the database.

1
Do you face any problem using Domain.get(id) (if id is known) or Domain.findByAbc('Test') (if property abc on Domain is known)? What have you tried yet? - dmahapatro

1 Answers

0
votes

Retry your example, set failOnError: true to see if there is any error while save()

OR Use this example instead to get a valid json response.

//Controller action
def index() {
    new Number(a: 10, b: 25).save()
    render Number.findWhere(a: 10) as JSON
}

//Number.groovy
class Number {
    Integer a
    Integer b
}

Grails 2.2.2