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?

saw303
  • 8,051
  • 7
  • 50
  • 90
Michael
  • 32,527
  • 49
  • 210
  • 370

3 Answers3

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.

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.

  • I said that I do not want to save the object again. – Michael Jul 28 '14 at 14:40
  • @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. – CrashOverload Jul 28 '14 at 14:56
  • I do not check if there are any errors I want to check if the object has been persisted. – Michael Jul 28 '14 at 15:04
  • Why do I have to check isAttached and exists? – Michael Jul 29 '14 at 15:25
  • 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. – CrashOverload Jul 30 '14 at 06:46
0

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

test.isDirty()
saw303
  • 8,051
  • 7
  • 50
  • 90
  • Well this does not work if you created the object in another function. Then you get always false. – Michael Jul 28 '14 at 14:39