How to update boolean value in GAE data warehouse?

I have the following code from a Andreas Borglin tutorial :

@Override public Model saveModel(Model model) { System.out.println("model isDone: " + ((Task)model).getDone()); PersistenceManager pm = PMF.get().getPersistenceManager(); Model savedModel = null; try { savedModel = pm.makePersistent(model); } catch (JDOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { pm.close(); } System.out.println("savedModel isDone: " + ((Task)savedModel).getDone()); System.out.println("model isDone: " + ((Task)model).getDone()); return savedModel; } 

It works great when I create test objects, but as soon as I want to update them, the boolean values ​​do not change. My saved "isDone" is "true" and I want to change it to "false". What is the conclusion:

 model isDone: false savedModel isDone: true model isDone: false 

Changing installation strings or dates works without a problem. The field is defined as:

 @Persistent private boolean isDone = true; 

I also tried:

 @Persistent private Boolean isDone; 

In this case, isDone is always false.

+4
source share
2 answers

Not sure about the specific problem you are facing, but I recommend using a Boolean object over a boolean primitive type. That is, use:

 @Persistent private Boolean isDone; 

If you add a primitive logical field after you have already created some objects, Datastore has problems creating old objects, since their values ​​for this field will be. With Boolean, they simply default to null, which is good enough.

Also, you may not need to explicitly state the true value for your boolean field, which may be the cause of this Datastore mess. However, you will need to change the field to "isNotDone".

+3
source

Try using

@Persistent

private Boolean isDone = Boolean.True;

I used Boolean before and it worked for me.

0
source

All Articles