How to model entity relationships in GAEJ?

早过忘川 提交于 2019-12-05 07:46:06

Creating relationships between entities in GAE/J depends on db API that you are using:

  1. JDO: entity relationships.

  2. JPA: see docs.

  3. Objectify: single-value relationships.

  4. Low-level API: add a Key of one Entity as a property to another Entity: see property types.

Just a tip. When defining your data model think in terms of end-user queries and define your data model accordingly.

For example, let's take the example of a store renting books. In a traditional application, you would have three main entities :

--> Book

--> Client

--> Rent (to solve the many-to-many)

To display a report with which client is renting which book, you would issue a query joining on the Rent table, Book table and client table.

However, in GAE that won't work because the join operation is not supported.

The solution I found (maybe other solution) is to model with the same three tables but embedding the book and client definitions in the Rent table.

This way, displaying the list of books being rent by whom is extremely fast and inexpensive. The only drawback is that if for example the title of a book changes, I have to go through all the embedded objects. However, how often does that happen vs. read-only queries.

As a summary, think in terms of end-user queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!