Class VKey<T>

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable

    public class VKey<T>
    extends ImmutableObject
    implements java.io.Serializable
    VKey is an abstraction that encapsulates the key concept.

    A VKey instance must contain both the JPA primary key for the referenced entity class and the objectify key for the object.

    See Also:
    Serialized Form
    • Method Detail

      • createSql

        public static <T> VKey<T> createSql​(java.lang.Class<T> kind,
                                            java.io.Serializable sqlKey)
        Creates a VKey which only contains the sql primary key.

        Deprecated. Create symmetric keys with create() instead.

      • createOfy

        public static <T> VKey<T> createOfy​(java.lang.Class<? extends T> kind,
                                            com.googlecode.objectify.Key<T> ofyKey)
        Creates a VKey which only contains the ofy primary key.
      • create

        public static <T> VKey<T> create​(java.lang.Class<T> kind,
                                         java.io.Serializable sqlKey,
                                         com.googlecode.objectify.Key<T> ofyKey)
        Creates a VKey which only contains both sql and ofy primary key.
      • create

        public static <T> VKey<T> create​(java.lang.Class<T> kind,
                                         long id)
        Creates a symmetric VKey in which both sql and ofy keys are id.

        IMPORTANT USAGE NOTE: Datastore entities that are not roots of entity groups (i.e. those that do not have a null parent in their Objectify keys) require the full entity group inheritance chain to be specified and thus cannot use this create method. You need to use create(Class, Serializable, Key) instead and pass in the full, valid parent field in the Datastore key.

      • create

        public static <T> VKey<T> create​(java.lang.Class<T> kind,
                                         java.lang.String name)
        Creates a symmetric VKey in which both sql and ofy keys are name.

        IMPORTANT USAGE NOTE: Datastore entities that are not roots of entity groups (i.e. those that do not have a null parent in their Objectify keys) require the full entity group inheritance chain to be specified and thus cannot use this create method. You need to use create(Class, Serializable, Key) instead and pass in the full, valid parent field in the Datastore key.

      • restoreOfy

        public VKey<T> restoreOfy​(java.lang.Object... ancestors)
        Returns a clone with an ofy key restored from ancestors.

        The arguments should generally consist of pairs of Class and value, where the Class is the kind of the ancestor key and the value is either a String or a Long.

        For example, to restore the objectify key for DomainBase("COM-1234")/HistoryEntry(123)/PollEvent(567), one might use:

        
         pollEvent.restoreOfy(DomainBase.class, "COM-1234", HistoryEntry.class, 567)
         

        The final key id or name is obtained from the SQL key. It is assumed that this value must be either a long integer or a String and that this proper identifier for the objectify key.

        As a special case, an objectify Key may be used as the first ancestor instead of a Class, value pair.

      • restoreOfyFrom

        public static <T> VKey<T> restoreOfyFrom​(@Nullable
                                                 VKey<T> key,
                                                 java.lang.Object... ancestors)
        Returns a clone of key with an ofy key restored from ancestors.

        This is the static form of the method restoreOfy() above. If key is null, it returns null.

      • getKind

        public java.lang.Class<? extends T> getKind()
        Returns the type of the entity.
      • getSqlKey

        public java.io.Serializable getSqlKey()
        Returns the SQL primary key.
      • maybeGetSqlKey

        public java.util.Optional<java.lang.Object> maybeGetSqlKey()
        Returns the SQL primary key if it exists.
      • getOfyKey

        public com.googlecode.objectify.Key<T> getOfyKey()
        Returns the objectify key.
      • maybeGetOfyKey

        public java.util.Optional<com.googlecode.objectify.Key<T>> maybeGetOfyKey()
        Returns the objectify key if it exists.
      • from

        @Nullable
        public static <T> VKey<T> from​(com.googlecode.objectify.Key<T> key)
        Convenience method to construct a VKey from an objectify Key.
      • fromWebsafeKey

        @Nullable
        public static <T> VKey<T> fromWebsafeKey​(java.lang.String ofyKeyRepr)
        Construct a VKey from the string representation of an ofy key.

        TODO(b/184350590): After migration, we'll want remove the ofy key dependency from this.