开发者

Implementing tagging in JDO

I am implementing a tagging system for a website that uses JDO . I would like to use this method.

However I am new to relationships in JDO. To keep it simple, what I have looks like this:

@PersistentCapable
class Post {
@Persistent String title;
@Persistent String body;
}

@PersistentC开发者_运维知识库apable
class Tag {
@Persistent String name;
}

What kind of JDO relationships do I need and how to implement them? I want to be able to list all Tags that belong to a Post, and also be able to list all Posts that have a given Tag. So in the end I would like to have something like this:

Table: Post
Columns: PostID, Title, Body

Table: Tag
Columns: TagID, name

Table: PostTag
Columns: PostID, TagID


You should look at: http://code.google.com/appengine/docs/java/datastore/relationships.html#Unowned_Relationships

Basically you create a Class PostTag which appart from it's primary key also has two Key fields for each of the relationships:

@PersistentCapable
class PostTag {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key; 
    /*you should add this to Post and Tag as well,
    since this is what the fields in PostTag will reference
    you can rename them to ClassnameID if you like, I believe*/

    @Persistent
    private Key post;

    @Persistent
    private Key tag;
}

Then when you create PostTag you should do something like this

PostTag pt = new PostTag();
pt.setPost(post.getKey());
pt.setTag(tag.getKey());
// persist pt here;

I used getters/setters here, since you usually set the fields to private and access them through accessor methods, but that's your call; also 'post' and 'tag' in the above snippet are supposed to be the already persisted objects that you want to link.

Edit: you should probably also take a look at: http://code.google.com/appengine/docs/java/datastore/usingjdo.html#Unsupported_Features_of_JDO since app engine only partially implements JDO (I believe it's because of the different way the datastore works, compared to traditional databases). They may add support for missing features in future though.


You want a many-to-many relationship, which JDO supports. However, App Engine doesn't fully support JDO, so I don't know if this is possible.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜