开发者

Adding objects to existing entity groups (Java App Engine)

开发者 https://www.devze.com 2023-02-15 14:25 出处:网络
I have two objects, lets call them \"User\" and \"Skill\". When a person signs up to my site they are saved as a \"User\". A user may have

I have two objects, lets call them "User" and "Skill". When a person signs up to my site they are saved as a "User". A user may have multiple skills, these are stored as a list within the User object, e.g.

@Persistent @Element(dependent = "true") 
private List<Skill&g开发者_StackOverflow社区t; skills;

Now what I need to determine is how to add a Skill object to the User entity group when I create it, as I need to modify both objects within a single transaction. When I create Skill objects do I have to supply the User key to the new Skill object somehow? I'd appreciate any help I can get with this, thanks!


You don't need to do anything special, except creating the two objects in the same transaction, and make the user persistent. Read http://code.google.com/intl/fr/appengine/docs/java/datastore/jdo/relationships.html#Relationships_Entity_Groups_and_Transactions


(for JDO) If it's an owned relationship your skill should contain a member of type user lets say

@Persistent
private User owner;

and User contains

@Persistent(mappedBy="owner")
private List<Skill> skills;

so when you create a new Skill pass the owning user in the constructor new Skill(someUser) and assign in to the owner member. Once you'll persist this entity, the mapping is done and now accessing the user's skills list will produce all skills "owner" is this user.

There are some issues with JDO relationship implementation on app engine as JDO is more relational DB oriented. You can find more about it in the reference link and plain ol' google.

0

精彩评论

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