How to model entity relationships in GAEJ?

拈花ヽ惹草 提交于 2019-12-10 04:26:41

问题


I would like to know -an example is highly appreciated-

How to model relationships in Google App Engine for Java?

-One to Many
-Many to Many

I searched allover the web and I found nothing about Java all guides and tutorials are about Python.

I understood from this article that in Python the relationships are modeled using ReferenceProperty. However, I found nothing about this class in the Javadoc reference.

Furthermore, in this article they discussed the following:

there's currently a shortage of tools for Java users, largely due to the relative newness of the Java platform for App Engine.

However, that's was written in 2009.

At the end, I ended up modeling the relationships using the ancestor path of each entity. I discovered afterwords that this approach has problems and limit the scalability of the app.

Can you please guide me to the equivalent Java class to the Python's ReferenceProperty class? Or can you please give me an example of how to model the relationships in AppEngine using the java datastore low-level API.

Thanks in advance for your help.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/11133924/how-to-model-entity-relationships-in-gaej

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