google-code-export / objectify-appengine

Automatically exported from code.google.com/p/objectify-appengine
MIT License
1 stars 0 forks source link

Allow Objectify to load/store/query via an Entity Interface specified by Annotation #64

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
This enhancement is based upon the discussion here: 
https://groups.google.com/d/msg/objectify-appengine/co5q9olQScM/VtQgOBcqsR4J

The idea is to allow Objectify Entities to specify an Interface that Objectify 
can use to register an Entity with.  This change would allow the following type 
of usage where "Car" is an Interface, and "CarImpl" is an Entity that 
implements "Car":

---
Key<Car> key = new Key<Car>(Car.class, 1L);
Car car = ofy().get(key);

---

To make this work, I propose defining a new class-level Annotation 
(@EntityInterface) that can be addded to an Entity like this: 

@Entity() or @Entity("Car")
@EntityInterface(clazz = Car.class)
public class CarImpl implements Car {...}

----
That annotation could look like this: 

@Retention(RetentionPolicy.RUNTIME)
@Target({
    ElementType.TYPE
})
public @interface EntityInterface
{
    Class<?> clazz();
}

-----
In ObjectifyFactory, the following function would be useful: 

/**
* @return the kind associated with a particular entity class, checking both 
@Entity annotations and defaulting to
*         the class' simplename.
* 
* @param entityClazz
*/
public Class<?> getEntityInterface(Class<?> entityClazz)
{
   com.googlecode.objectify.annotation.EntityInterface ourAnn = entityClazz.getAnnotation(com.googlecode.objectify.annotation.EntityInterface.class);
   if (ourAnn != null)
      return ourAnn.clazz();
   else
   {
      return null;
   }
}

------------------
Then, is ObjectifyFactory.register(..), I propose adding the following snippet 
right after "this.byClassName.put(clazz.getName(), meta);":

--
// Add the Interface Name to the byClassName Map
Class<?> interfaceClass = getEntityInterface(clazz);
if (interfaceClass != null)
{
     // If the interfaceClass is not null, then add it to the Class Mapping.
     this.byClassName.put(interfaceClass.getName(), meta);
}
--

---
The following is some 

Original issue reported on code.google.com by sappenin on 19 Dec 2010 at 3:39

GoogleCodeExporter commented 9 years ago

Original comment by lhori...@gmail.com on 19 Dec 2010 at 11:39