Note that there are some explanatory texts on larger screens.

plurals
  1. POGoogle App Engine datastore - how to save a one-to-one relationship
    primarykey
    data
    text
    <p>I am having trouble understanding how the entity groups and relationships work with GAE using JDO.</p> <p>The scenario I run is basically:</p> <pre><code>PersistenceManager pm = this.pmf.get(); Player player = new Player(); player.setRanking(new Ranking()); Player persistent = pm.makePersistent(player); // Here the detached copy is returned, and contains // all the persistent fields (including the Ranking) Player detached = pm.detachCopy(persistent); // In the real code, a lot of processing goes here and manipulates // The detached copy // The outcome is basically an updating ranking. What I want to do is // to assign this new ranking to the player, and persist // it in the datastore detached.setRanking(new Ranking()); // An exception is thrown here pm.makePersistent(detached ); </code></pre> <p>The output of the above code is an exception:</p> <p><code>Detected attempt to establish Player(10) as the parent of Ranking(12) but the entity identified by Ranking(12) has already been persisted without a parent. A parent cannot be established or changed once an object has been persisted.</code></p> <p>What I understand from that is that the Ranking entity is first persisted (with no parent, so as a root entity), and later on the player is persisted as the parent of the first Ranking entity. Since a parent cannot be changed, this results in an exception being thrown.</p> <p>However I would like the application to work on the detached copy and manipulate it as it sees fit, and have the entities properly created when the Player entity is persisted in the datastore.</p> <p>My classes are annotated as follow:</p> <p>Player.java</p> <pre><code>@PersistenceCapable(detachable = "true") @Inheritance(customStrategy = "complete-table") public class Player implements Serializable, IPlayer { @PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true") private String id; @Persistent(defaultFetchGroup="true", dependent="true") private Ranking ranking; } </code></pre> <p>And Ranking.java</p> <pre><code>@PersistenceCapable(detachable = "true") @Inheritance(customStrategy = "complete-table") public class Ranking implements Serializable { @PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true") private String id; // If I could I'd rather get rid of this reference to the parent @Persistent(mappedBy = "ranking", defaultFetchGroup="true") private Player player; } </code></pre> <p>There is something in the entity / entity group paradigm that I obviously don't get, and would be happy to get any hint you may be able to offer on this.</p> <p>[EDIT]<br> I have put together <a href="http://www.2shared.com/file/h6c02bzD/testCase.html" rel="nofollow">a test case</a> to reproduce the issue.<br> [/EDIT]</p> <p>Sébastien</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload