开发者

Google App Engine datastore - how to save a one-to-one relationship

I am having trouble understanding how the entity groups and relationships work with GAE using JDO.

The scenario I run is basically:

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 );

The output of the above code is an exception:

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.

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.

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.

My classes are annotated as follow:

Player.java

@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;
}

And Ranking.java

@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;
}

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.

[EDIT]

I have put together a test case to reproduce the issue.

[/EDIT]

Sébastien

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜