English 中文(简体)
How to model a many-to-many relationship in App Engine?
原标题:

I have a question regarding how to model a many-to-many relationship in App Engine:

A Blogentry can have many tags, a tag can apply to many blog entries.

I see a couple of scenarios:

  1. Use a Set of Strings as an attribute on the blog entry.

    • This allows me to easily query for an entry using a tag
    • This does not allow me to fetch all tags and their weights (how many entries they apply to)
  2. Use an unowned relationship between an Entry and a Tag class (Set of keys for Tags in Entry class and vise versa)

    • This allows me to fetch all tags and their weights
    • This is much more comples to maintain
    • Are Set attributes lazyloaded, or would I fetch the entire graph of object every time? (Fetch an Entry, which fetches a number of Tags, each in turn fetching a number of Entries)
  3. use 1. but maintain data on tags and their weights seperately

    • This has synchronisation issues between the Tag data and the tags in the Entries

Any input and pointers would be appreciated. I think this is a quite common scenario but I haven t seen any good solutions yet.

问题回答

Like many other database management systems, many-to-many relationships are not natively supported in App Engine Datastore, but could be solved through a "junction table". However, since App Engine s query language does not support joins, this will be very painful to use in your application. Google s BigTable architecture in fact discourages this, because distributed joins are not efficient.

So, I suggest going with the "keep it simple stupid" rule; use the simplest thing that works. A list of strings in a "Blogentry" object sounds fairly robust. Even if it s prone to race conditions (people making updates in parallel, overwriting each other s changes), but how many people do you have editing the same blog post anyway?

I decided to go with option 3., to maintain a seperate list of tags with their weights.

This seems to work ok, although the insert/update code is a bit cluttered.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签