Eyck
Eyck

Reputation: 53

Datanucleus Google App Engine One-To-Many relationship

I've got following code:

@Entity
public class Incident {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    private String incidentId;

    @Persistent
    @Extension(vendorName="datanucleus", key="gae.pk-id", value="true")
    private Long keyId;


    @OneToMany(mappedBy="incident")
    @OrderBy("requestId")
    @JoinColumn(name="INCIDENT_ID")
    public List<ServiceRequest> requests;

    ...
}

@Entity
public class ServiceRequest {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    private String requestId;

    @Persistent
    @Extension(vendorName="datanucleus", key="gae.pk-id", value="true")
    private Long keyId;
    private Incident incident;

    ...
}

Scenario of using it is firstly create and persist ServiceRequest and then create a Incident and add to it existing ServiceRequest - but when I tried to persist an Incident I've got following error Caused by: org.datanucleus.exceptions.NucleusUserException: Object with id "agxzbWFydGNpdHlhZ2hyFAsSDlNlcnZpY2VSZXF1ZXN0GAEM" is managed by a different Object Manager

Upvotes: 1

Views: 642

Answers (1)

Ga&#235;l Oberson
Ga&#235;l Oberson

Reputation: 603

I'll sugest you to start as follows, and then tell you to study deeply the doc regarding all the datastore, JDO Stuff. Then thank Google. :-)

You must use the same persistence manager to do all your read / create / save / update tasks at once. Meaning that in the same method or code block, you have to :

  • Get a fresh PersistenceManager
  • fetch the EntityA you need
  • do whatever you want with it, including instantiating other Entities from other Classes and have them all linked somehow
  • use the pm to persist everything
  • close the pm

The thing is, if you work with more than one Entity, you need to use the same PersistenceManager instance.

JDO doc - Google

Upvotes: 1

Related Questions