Could not update the existing JDO object and persist the new updated data

39 Views Asked by At

I have been trying solutions from multiple threads but of no luck yet.

Anyone who can get a quick harp on what is happening in by code? I have shared my update code below. Appreciate quick assistance.

@ApiMethod(name="updateContact", path = "updateContact", httpMethod = "GET")
public OpinionModel updateEmployeeTitle(@Named("id") Long id,@Named("selectedOpinionIndex") int opinionIndex) {

    PersistenceManager pm = PMF.get().getPersistenceManager();
    javax.jdo.Transaction tx =  pm.currentTransaction();
    pm.currentTransaction().begin(); // <-------

    OpinionModel e;

    try {
        e = pm.getObjectById(OpinionModel.class,id);

        if (opinionIndex == 1) {
            int foo = Integer.parseInt(e.opinion1ResultCount);
            foo++;
            e.opinion1ResultCount = Integer.toString(foo);
            JDOHelper.makeDirty(e, e.opinion1ResultCount);

        }else if (opinionIndex == 2) {

            int foo = Integer.parseInt(e.opinion2ResultCount);
            foo++;
            e.opinion2ResultCount = Integer.toString(foo);
            JDOHelper.makeDirty(e, e.opinion2ResultCount);

        }
        else if (opinionIndex == 3) {

            int foo = Integer.parseInt(e.opinion3ResultCount);
            foo++;
            e.opinion3ResultCount = Integer.toString(foo);
            JDOHelper.makeDirty(e, e.opinion3ResultCount);

        }
        else if (opinionIndex == 4) {

            int foo = Integer.parseInt(e.opinion4ResultCount);
            foo++;
            e.opinion4ResultCount = Integer.toString(foo);
            JDOHelper.makeDirty(e, e.opinion4ResultCount);
        }
        else {
            System.out.println("Invalid selection");
        }

        pm.currentTransaction().commit(); // <-------
        pm.makePersistent(e);

    } finally {

        if (tx.isActive())
        {
            // Error occurred so rollback the transaction
            tx.rollback();
        }

        pm.close();
    }

    ApiResponse resp = new ApiResponse();
    resp.responsecode = "SUCCESS";
    resp.responseMessage = "Contact Updated ";

    return e;
}
0

There are 0 best solutions below