Stay organized with collections
Save and categorize content based on your preferences.
publicclassKeyFactory
This class enables direct creation of Key objects, both in the root entity group (no
parent) and as the child of a given parent. Keys can also be created indirectly by putting named
Entities into the datastore, which will allocate a new key. See Entity#Entity(String,
String) for details.
This class also has methods for encoding and decoding Key objects to and from strings.
Clients should not make any assumptions about the encoding format, except that it is a websafe
string that does not need to be quoted when used in HTML or in URLs.
See Also: Entity
A websafe String representation of the Key that was created
keyToString(Key key)
publicstaticStringkeyToString(Keykey)
Converts a Key into a websafe string. For example, this string can safely be used as an
URL parameter embedded in a HTML document. Note that
key.equals(KeyFactory.stringToKey(KeyFactory.keyToString(key))
should evaluate to true.
A websafe String representation of the provided String.
stringToKey(String encoded)
publicstaticKeystringToKey(Stringencoded)
Converts a String-representation of a Key into the Key instance it
represents. Note that
str.equals(KeyFactory.keyToString(KeyFactory.stringToKey(str))
should evaluate to true for all strings returned by KeyFactory#keyToString.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-03 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eKeyFactory\u003c/code\u003e class facilitates the creation of \u003ccode\u003eKey\u003c/code\u003e objects, either with a parent or as a root entity with no parent, directly or indirectly through named entities.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eKeyFactory\u003c/code\u003e can encode \u003ccode\u003eKey\u003c/code\u003e objects into websafe strings suitable for HTML and URLs, and it can also decode these strings back into \u003ccode\u003eKey\u003c/code\u003e objects.\u003c/p\u003e\n"],["\u003cp\u003eYou can create a \u003ccode\u003eKey\u003c/code\u003e using either a name or a numeric ID, which must be unique for the given kind and parent, with methods for both.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eKeyFactory\u003c/code\u003e includes static methods to generate string representations of keys via \u003ccode\u003ecreateKeyString\u003c/code\u003e, which are shorthand for combining \u003ccode\u003ecreateKey\u003c/code\u003e and \u003ccode\u003ekeyToString\u003c/code\u003e, and to convert string keys back to key objects through \u003ccode\u003estringToKey\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["# Class KeyFactory (2.0.0)\n\n public class KeyFactory\n\nThis class enables direct creation of `Key` objects, both in the root entity group (no\nparent) and as the child of a given parent. Keys can also be created indirectly by putting named\nEntities into the datastore, which will allocate a new key. See Entity#Entity(String, String) for details.\n\nThis class also has methods for encoding and decoding `Key` objects to and from strings.\nClients should not make any assumptions about the encoding format, except that it is a websafe\nstring that does not need to be quoted when used in HTML or in URLs.\nSee Also: [Entity](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Entity) \n\nInheritance\n-----------\n\n[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \\\u003e KeyFactory \n\nInherited Members\n-----------------\n\n[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) \n[Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) \n[Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) \n[Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) \n[Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) \n[Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) \n[Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) \n[Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) \n[Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) \n[Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) \n[Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)\n\nStatic Methods\n--------------\n\n### createKey(@Nullable Key parent, String kind, String name)\n\n public static Key createKey(@Nullable Key parent, String kind, String name)\n\nCreates a new `Key` with the provided parent from its kind and name.\n\n### createKey(@Nullable Key parent, String kind, long id)\n\n public static Key createKey(@Nullable Key parent, String kind, long id)\n\nCreates a new `Key` with the provided parent from its kind and ID.\n\n### createKey(String kind, String name)\n\n public static Key createKey(String kind, String name)\n\nCreates a new `Key` having no parent from its kind and name.\n\n### createKey(String kind, long id)\n\n public static Key createKey(String kind, long id)\n\nCreates a new `Key` having no parent from its kind and ID.\n\n### createKeyString(@Nullable Key parent, String kind, String name)\n\n public static String createKeyString(@Nullable Key parent, String kind, String name)\n\nShorthand for invoking [#keyToString(Key)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory#com_google_appengine_api_datastore_KeyFactory_keyToString_com_google_appengine_api_datastore_Key_) on the result of #createKey(Key, String, String)\n\n### createKeyString(@Nullable Key parent, String kind, long id)\n\n public static String createKeyString(@Nullable Key parent, String kind, long id)\n\nShorthand for invoking [#keyToString(Key)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory#com_google_appengine_api_datastore_KeyFactory_keyToString_com_google_appengine_api_datastore_Key_) on the result of #createKey(Key, String, long)\n\n### createKeyString(String kind, String name)\n\n public static String createKeyString(String kind, String name)\n\nShorthand for invoking [#keyToString(Key)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory#com_google_appengine_api_datastore_KeyFactory_keyToString_com_google_appengine_api_datastore_Key_) on the result of #createKey(String, String)\n\n### createKeyString(String kind, long id)\n\n public static String createKeyString(String kind, long id)\n\nShorthand for invoking [#keyToString(Key)](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory#com_google_appengine_api_datastore_KeyFactory_keyToString_com_google_appengine_api_datastore_Key_) on the result of #createKey(String, long)\n\n### keyToString(Key key)\n\n public static String keyToString(Key key)\n\nConverts a `Key` into a websafe string. For example, this string can safely be used as an\nURL parameter embedded in a HTML document. Note that `\nkey.equals(KeyFactory.stringToKey(KeyFactory.keyToString(key))\n` should evaluate to `true`.\n\n### stringToKey(String encoded)\n\n public static Key stringToKey(String encoded)\n\nConverts a `String`-representation of a `Key` into the `Key` instance it\nrepresents. Note that `\nstr.equals(KeyFactory.keyToString(KeyFactory.stringToKey(str))\n` should evaluate to `true` for all strings returned by [KeyFactory#keyToString](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory#com_google_appengine_api_datastore_KeyFactory_keyToString_)."]]