Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to model entity relationships in GAEJ?

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.

like image 528
M.ES Avatar asked Jun 21 '12 08:06

M.ES


2 Answers

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.

like image 83
Peter Knego Avatar answered Oct 14 '22 08:10

Peter Knego


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

like image 32
Hugues Avatar answered Oct 14 '22 09:10

Hugues