Saving twice don't update my object in JDO

Posted by Javi on Stack Overflow See other posts from Stack Overflow or by Javi
Published on 2010-12-21T08:33:12Z Indexed on 2010/12/21 11:54 UTC
Read the original article Hit count: 355

Hello

I have an object persisted in the GAE datastore using JDO. The object looks like this:

public class MyObject implements Serializable, StoreCallback {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    private String id;

    @Persistent
    private String firstId;

    ...
}

As usually when the object is stored for the first time a new id value is generated for the identifier. I need that if I don't provide a value for firstId it sets the same value as the id. I don't want to solve it with a special getter which checks for null value in firstId and then return the id value because I want to make queries relating on firstId.

I can do it in this way by saving the object twice (Probably there's a better way to do this, but I'll do it in this way until I find a better one). But it is not working. when I debug it I can see that result.firstId is set with the id value and it seems to be persisted, but when I go into the datastore I see that firstId is null (as it was saved the first time). This save method is in my DAO and it is called in another save method in the service annotated with @Transactional. Does anyone have any idea why the second object in not persisted properly?

@Override
public MyObject save(MyObject obj) { 
    PersistenceManager pm = JDOHelper.getPersistenceManagerFactory("transactions-optional");

    MyObject result = pm.makePersistent(obj);
    if(result.getFirstId() == null){
         result.setFirstId(result.getId());
         result = pm.makePersistent(result);
    }
    return result;
}

Thanks.

© Stack Overflow or respective owner

Related posts about java

Related posts about spring