Skip to content

AnnotationReference

senik11 edited this page May 7, 2016 · 4 revisions

The annotations used by Objectify

Example

@Cache(expirationSeconds=600)
@Entity(name="Emp")
@Index // or @Unindex sets default for fields in class; if neither specified, default is @Unindex
class Employee {
   @Id long id;
   @Parent Key<Company> employer;

   @Unindex int vacationDays;

   // field value WILL be serialized (eg, with GWT) but will NOT be persisted in the datastore
   @Ignore int notPersistent;

   // field value will NOT be serialized (eg, with GWT) but WILL be persisted in the datastore
   transient int notSerialized;

   // field getting renamed
   @AlsoLoad("boss") Key<Employee> manager;

   String firstName;
   String lastName;

   // we used to store fullName, now we store first and last separately
   public void oldWay(@AlsoLoad("fullName") String fullName) {
       String[] split = fullName.split(" ");
       firstName = split[0];
       lastName = split[1];
   }

   static class Office {
      String building;
      String room;
   }

   // Allows you to query for employess in a building by filtering on "offices.building"
   Office[] offices;

   // Whatever gets put here will be serialized out and back (and cannot be filtered)
   @Serialize Object misc;

   @OnLoad void onLoad() { /* do something after load */ }
   @OnSave void onSave() { /* do something before persisting */ }
}

The Annotations

@com.googlecode.objectify.annotation.Entity

  • Required for all registered entity classes.
  • Optionally specifies the name of a kind, ie @Entity(name="Person")

@com.googlecode.objectify.annotation.Id

  • Must be placed on one and only one field of an @Entity class
  • Field must be type Long, long, or String
  • If used on type Long, null values will be autogenerated on put()

@com.googlecode.objectify.annotation.Parent

  • Placed on at most one field of type Key
  • Defines the entity group parent for an entity

@com.googlecode.objectify.annotation.Subclass

  • Placed on subclasses of polymorphic types, both @Entity and embedded
  • Exclusive with @Entity
  • See Polymorphism.

@com.googlecode.objectify.annotation.Index

  • Placed on a field or class.
  • When placed on a class, sets the default state for all fields of the class.
  • Can take If conditions which allow partial indexing.
  • @Index state is inherited by embedded classes and their fields, but can be overriden with @Unindex.
  • Indexed fields cost more space and cpu time to store, but can be queried on.
  • Translates to Entity.setProperty() at the low-level api.
  • Unless otherwise specified, all classes default to @Unindex.

@com.googlecode.objectify.annotation.Unindex

  • Placed on a field or class.
  • When placed on a class, sets the default state for all fields of the class.
  • Can take If conditions which allow partial indexing.
  • @Unindex state is inherited by embedded classes and their fields, but can be overridden with @Index.
  • Unindexed fields consume less space and require less cpu time to store, but cannot be queried for.
  • Translates to Entity.setUnindexedProperty() at the low-level api.

@com.googlecode.objectify.annotation.Ignore

  • Placed on any field
  • Makes the field opaque to storage in the datastore; the field will be ignored for both read and write.

@com.googlecode.objectify.annotation.IgnoreSave

  • Placed on any field
  • Field will be loaded from the datastore but won't be saved
  • Can take If conditions which allow some values to be saved and not others.

@com.googlecode.objectify.annotation.IgnoreLoad

  • Placed on any field
  • Field will be saved to the datastore but will be ignored during load operations

@com.googlecode.objectify.annotation.AlsoLoad

  • Placed on a field or the single parameter to a method
  • Requires one value, the name of a property in the datastore to load into the field.
  • Example: @AlsoLoad("whatThisFieldUsedToBeCalled")
  • Causes the field to be loaded from an alternate name in the underlying datastore
  • If placed on the parameter to a method that takes a single parameter, the method will be called with the datastore value
  • Can be used on multiple fields; they will all receive the loaded value.

@com.googlecode.objectify.annotation.Cache

  • Placed on an entity class
  • Stores entity data in a write-through cache for faster read performance
  • Allows you to specify the expiration of entities in the cache

@com.googlecode.objectify.annotation.Serialize

  • Placed on any entity field of Serializable type
  • Causes the object graph at that point to be stored as a serialized Blob.
  • You can optionally configure zipping and compression level.
  • See the serializing documentation.

@com.googlecode.objectify.annotation.OnLoad

  • Placed on any method that takes no parameters.
  • Called after the POJO is populated with data.
  • Can be specified on multiple methods in this class or any base class.
  • Methods are called in declared order, with superclass methods first.
  • See LifecycleCallbacks

@com.googlecode.objectify.annotation.OnSave

  • Placed on any method that takes no parameters.
  • Called before the POJO is written to the datastore Entity.
  • Can be specified on multiple methods in this class or any base class.
  • Methods are called in declared order, with superclass methods first.
  • See LifecycleCallbacks

@com.googlecode.objectify.annotation.Load

  • Placed on entity fields of type Ref<?>
  • Causes the referenced entity to be loaded automatically
  • Load groups can be specified to limit when loading occurs
  • See Relationships

@com.googlecode.objectify.annotation.Mapify

  • TODO: This needs some explanation.
  • See the javadocs for details.

@com.googlecode.objectify.annotation.Stringify

  • TODO: This needs some explanation.
  • See the javadocs for details.

@com.googlecode.objectify.annotation.Container

  • TODO: This needs some explanation.
  • See the javadocs for details.

@com.googlecode.objectify.annotation.Translate

  • TODO: This needs some explanation.
  • See the javadocs for details.
You can’t perform that action at this time.