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.

      • create

        public static <T> VKey<T> create​(java.lang.String keyString)
        Constructs a VKey from the string representation of a vkey.

        There are two types of string representations: 1) existing ofy key string handled by fromWebsafeKey() and 2) string encoded via stringify() where @ separates the substrings and each of the substrings contains a look up key, ":", and its corresponding value. The key info is encoded via Base64. The string begins with "kind:" and it must contains at least ofy key or sql key.

        Example of a Vkey string by fromWebsafeKey(): "agR0ZXN0chYLEgpEb21haW5CYXNlIgZST0lELTEM"

        Example of a vkey string by stringify(): "kind:TestObject@sql:rO0ABXQAA2Zvbw" + "@ofy:agR0ZXN0cjELEg9FbnRpdHlHcm91cFJvb3QiCWNyb3NzLXRsZAwLEgpUZXN0T2JqZWN0IgNmb28M", where sql key and ofy key values are encoded in Base64.

      • 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.

      • stringify

        public java.lang.String stringify()
        Constructs the string representation of a VKey.

        The string representation of a vkey contains its kind, and sql key or ofy key, or both. Each of the keys is first serialized into a byte array then encoded via Base64 into a web safe string.

        The string representation of a vkey contains key values pairs separated by delimiter "@". Another delimiter ":" is put in between each key and value. The following is the complete format of the string: "kind:class_name@sql:encoded_sqlKey@ofy:encoded_ofyKey", where kind is required. The string representation may contain an encoded ofy key, or an encoded sql key, or both.

      • toString

        public java.lang.String toString()
        Constructs the readable string representation of a VKey.

        This readable string representation of a vkey contains its kind and its sql key or ofy key, or both.

        Overrides:
        toString in class ImmutableObject