Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google App Engine java replace jpa @OneToMany/@ManyToOne with objectify

I have an existing google app engine java application runs with jpa and now i am trying to move to objectify for many different purposes.

Current application has the below representation of the entities.

Class Parent{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key key;

    @OneToMany(cascade=CascadeType.ALL, mappedBy="parent")
    private List<Child> childs = null;
}

Class Child{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key key;

    @ManyToOne  
    private Parent parent;

}

How can i get the above representation in Objectify? I am using objectify4.
I have seen @Parent, but it seems not possible to load the list of child entities directly while fetching a parent?

Thanks,

Ramesh.V

like image 835
Ramesh V Avatar asked Oct 23 '22 14:10

Ramesh V


1 Answers

Objectify uses different concepts than relational data stores and ORMs. Relations are constructed using Keys and are not loaded automatically. You can read about the modelling of relationships in Objectify here. The @Parent relationship is something special and is not the one relevant for 1:n or n:m relationships.

So basically you have

class Parent {
    Key<Child>[] childs;
}

class Child {
    Key<Parent> parent;
}

And then you would get the entities via the Objectfy service, e.g.

Parent aParent = ofy.get(aChild.parent);

There are no lazily loaded collections. If you want to construct a set of objects you need to do it explicitely.

like image 76
Hauke Ingmar Schmidt Avatar answered Oct 27 '22 09:10

Hauke Ingmar Schmidt