hello.
I have a problem when trying to update an object that has already been
saved in the dataStore....
An Employee and Contactnfo.  One employee has a contactInfo...

//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
@PersistenceCapable(detachable = "true")
public class Employee implements Serializable {

  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Key key;

  @Persistent
  private ContactInfo contactInfo = null;

  public ContactInfo getContactInfo() {
    return contactInfo;
  }

  public void setContactInfo(ContactInfo contactInfo) {
    this.contactInfo = contactInfo;
  }

  public Key getKey() {
    return key;
  }
}
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
@PersistenceCapable(detachable = "true")
public class ContactInfo implements Serializable {

  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Key key;

  @Persistent
  private String name;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Key getKey() {
    return key;
  }

  public void setKey(Key key) {
    this.key = key;
  }
}
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
my servlet:

public class GreetingServiceImpl extends RemoteServiceServlet
implements GreetingService {

  private static final PersistenceManagerFactory pmfInstance =
JDOHelper.getPersistenceManagerFactory("transactions-optional");

  public String greetServer(String input) {
    String result = "";

    //step1 create and save an employee in the datastore.
    Key key = storeEmployeeInDatastore();

    //step2 retrieve the employee from the datastore and detach it
    Employee detachedEmployee = getDetachedEmployee(key);

    //step3 modify the detached employee
    ContactInfo contactInfo2 = new ContactInfo();
    detachedEmployee.setContactInfo(contactInfo2);

    //step4 save the modified employee...
    updateEmployeeInDataStore(detachedEmployee);

    return result;
  }

  /**
   * @return the key of the employee that has been stored
   */
  private Key storeEmployeeInDatastore() {
    Employee employee = new Employee();
    ContactInfo contactInfo = new ContactInfo();
    employee.setContactInfo(contactInfo);

    //creates a new manager.
    PersistenceManager manager1 = pmfInstance.getPersistenceManager();
    Key key = null;

    try {
      manager1.makePersistent(employee);
      key = employee.getKey();
    }
    catch ( Exception e ) {
      e.printStackTrace();
    }
    finally {
      //equivalent to manager1.close()
      manager1.close();
    }
    return key;
  }

  private Employee getDetachedEmployee(Key key) {
    PersistenceManager manager2 = pmfInstance.getPersistenceManager();
    Employee attached = manager2.getObjectById(Employee.class, key);

    Employee detachedEmployee = manager2.detachCopy(attached);
    manager2.close();
    return detachedEmployee;
  }

  private void updateEmployeeInDataStore(Employee detachedEmployee) {
    PersistenceManager manager3 = pmfInstance.getPersistenceManager();
    try {

      Employee bidon = manager3.makePersistent(detachedEmployee);

      System.out.println("");
    }
    catch ( Exception e ) {
      e.printStackTrace();
    }
    finally {
      manager3.close();
    }
  }
}

///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
HERE IS THE ERROR I GET
org.datanucleus.store.appengine.DatastoreRelationFieldManager
$ChildWithoutParentException: Detected attempt to establish
Employee(4) as the parent of ContactInfo(6) but the entity identified
by ContactInfo(6) has already been persisted without a parent.  A
parent cannot be established or changed once an object has been
persisted.
        at
org.datanucleus.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:
354)
        at
org.datanucleus.jdo.JDOPersistenceManager.close(JDOPersistenceManager.java:
281)
        at
com.gae.server.GreetingServiceImpl.updateEmployeeInDataStore(GreetingServiceImpl.java:
85)
        at
com.gae.server.GreetingServiceImpl.greetServer(GreetingServiceImpl.java:
33)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at
com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:
100)
        at
com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:
562)


////////////////////////////////////////////////////////////////////////////////////////

Can Anyone explain me what's wrong... and can anyone explain to me how
I can do the 4 steps!

    //step1 create and save an employee in the datastore.

    //step2 retrieve the employee from the datastore and detach it

    //step3 modify the detached employee

    //step4 save the modified employee...

Thank you in advance.

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to