views:

37

answers:

2

I have the following code from Andreas Borglin's 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 fine when I create test entities, but once I want to update them, the boolean values don't change. My saved "isDone" is "true" and I want to change it to "false". That's the output:

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

Changing strings or setting 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".

A: 

Try using

@Persistent

private Boolean isDone = Boolean.True;

I have used Boolean before and it worked for me.

John
A: 

Not sure about the specific problem you're having, but I recommend using the Boolean object over the boolean primitive type. That is, use:

@Persistent
private Boolean isDone;

If you add a primitive boolean field after you've already created some entities, the Datastore has problems instantiating older objects since their values for this field will be . With Boolean, they just default to "null", which is good enough.

Also, perhaps consider not explicitly defining a true value for your boolean field, which might be the reason for this Datastore mess. You'd have to change your field to something like "isNotDone", though.

Amos