Search code examples
google-app-engineforeign-keysobjectifydeclare

Objectify - How to declare a foreign key?


I am working with Google app engine and Objectify. I am unable to find that how to declare foreign key in Objectify.

Any help would be really appreciated.

Thanks.


Solution

  • Google App Engine datastore is not an RDBMS database, so the concept of the Foreign Key doesn't specifically exist. You can save a reference from an entity A to an entity B by having a Key property in A of B. For example consider the following:

    @Entity
    public class A {
        @Id Long id;
    }
    
    @Entity
    public class B {
        @Id Long id;
        Key<A> referenceToA;
    }
    

    For more information about data modeling in GAE, take a look at:

    Hope this helps!