How do I test if an Instance is persisted in the Database with GORM?
Asked Answered
V

3

10

When I save an instance as:

test.save() 

the save could fail. I could do

if (!test.save(flush:true) {
  // save failed
} 

Consider that case that I got an instance from another function and could not do this check because I will not save the instance again.

Is there a way to check if an instance is still persisted in the data base or has the unsaved state?

Vigilantism answered 28/7, 2014 at 13:23 Comment(0)
E
7

I know this is an old question but for future reference I think the appropriate answer would be this:

Given the OP's original question

Is there a way to check if an instance is still persisted in the data base or has the unsaved state?

Yes, there is. Call the exists() method of the domain class of your instance.

Suppose we have a Test domain class and a test object instance:

def bPersists = (  test.id != null  &&  Test.exists(test.id)  ); // Check test.id for null because a id of zero is a valid index, also the id might not be an integer.

Notice the check if test.id equals to null instead of just checking if it evaluates to true. That is because an id equal to zero would evaluate to false but it is actually a valid id number. Also the id could be a string or other data type.

Ecumenism answered 5/11, 2015 at 20:28 Comment(1)
Won't work for a composite keyDuple
W
4

Original answer:

Ultimately you want to save the object, so if its already saved

test.save(flush:true)

will automatically save/update the object. Also, hibernate ensures that there is only one persisted instance of each object, if there are more than one such instances you'll get error while fetching the other one.

Edited Answer:

Since you don't want to save the object you'll have to check two things:

  1. Object is attached to hibernate session or not.
  2. Another object with same id exists or not.

Following code should do it:

def exists = test.id?Test.get(test.id):false
def isPersisted = false

if(test.isAttached() || exists){
    isPersisted = true
}

I hope it solves your problem.

Wentworth answered 28/7, 2014 at 13:30 Comment(5)
I said that I do not want to save the object again.Vigilantism
@confile then you should detach the object and try working on it then then. Aa far as i understood your code is trying to check for validation errors, and you don't want to save just call validate() to do that. If you mean something else please explain in little detail.Wentworth
I do not check if there are any errors I want to check if the object has been persisted.Vigilantism
Why do I have to check isAttached and exists?Vigilantism
I think its pretty straight forward, attached tells you if the object is attached to a hibernate session, i.e its already persisted. But if an object is in detached state it may be persisted before and then detached, that is why you need to check if it already exists in db.Wentworth
A
0

In order test whether a domain instance has an unsaved state you simply use

test.isDirty()
Allpurpose answered 28/7, 2014 at 13:55 Comment(1)
Well this does not work if you created the object in another function. Then you get always false.Vigilantism

© 2022 - 2024 — McMap. All rights reserved.