Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Objectify's annotation when sharing common abstract models through inheritance?

Currently, I have some abstract model classes which have common behaviours for other concrete models. For instance, an abstract SearchableModel.class might be inherited by other concrete models.

When using Objectify, I would like to annotate the SearchableModel class, because it requires the persistance of it's own fields. However, I am not interested in doing any sort of polymorphic queries and I dont want any entities of kind "SearchableModel" in the datastore, only the kinds of concrete classes inheriting from SearchableModel. How should I proceed with the @Entity/@EntitySubClass annotations?

If I annotate both the abstract and concrete classes with @Entity, and persist/load the instance of concrete classes, will it work as expected?

I know I could test it myself, however, I didn't setup objectify yet. I am still trying to figure it out how it could be used with my current model.

Any other suggestions on how to organize this sort of dependence is appreciated as well.

Thank you.

like image 218
obelloc Avatar asked Feb 20 '13 00:02

obelloc


1 Answers

  • Don't use @EntitySubclass.
  • Do extend from your SearchableModel.
  • Do put @Entity on your (top-level) concrete classes.

This will give you want you want. You can put common fields (with Objectify annotations, including @Id/@Parent) and behaviors in the base class. There will not be any datastore polymorphism and each @Entity will have its own datastore kind.

like image 81
stickfigure Avatar answered Sep 28 '22 07:09

stickfigure