A datastore GUID. A Key instance uniquely identifies an entity across all apps, and includes
all information necessary to fetch the entity from the datastore with
DatastoreService.get(Key).
You can also retrieve the Key automatically created when you create a new Entity, or serialize Key objects, or use KeyFactory to convert them to and from
websafe String values.
See Also: KeyFactory
Compares two Key objects. The algorithm proceeds as follows: Turn each Key into
an iterator where the first element returned is the top-most ancestor, the next element is the
child of the previous element, and so on. The last element will be the Key we started
with. Once we have assembled these two iterators (one for 'this' and one for the Key
we're comparing to), consume them in parallel, comparing the next element from each iterator.
If at any point the comparison of these two elements yields a non-zero result, return that as
the result of the overall comparison. If we exhaust the iterator built from 'this' before we
exhaust the iterator built from the other Key, we return less than. An example:
The relationship between individual Key Keys is performed by comparing app followed
by kind followed by id. If both keys are assigned names rather than ids, compares names instead
of ids. If neither key has an id or a name we return an arbitrary but consistent result.
Assuming all other components are equal, all ids are less than all names.
Compares two Key objects by comparing ids, kinds, parent and appIdNamespace. If both
keys are assigned names rather than ids, compares names instead of ids. If neither key has an
id or a name, the keys are only equal if they reference the same object.
[[["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-08-07 UTC."],[[["\u003cp\u003eA \u003ccode\u003eKey\u003c/code\u003e uniquely identifies an entity in a datastore across all apps and is necessary for fetching entities using \u003ccode\u003eDatastoreService.get(Key)\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eKey\u003c/code\u003e objects can be created directly via \u003ccode\u003eKeyFactory#createKey\u003c/code\u003e or \u003ccode\u003e#getChild\u003c/code\u003e, or automatically when creating a new \u003ccode\u003eEntity\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eKey\u003c/code\u003e class implements \u003ccode\u003eSerializable\u003c/code\u003e and \u003ccode\u003eComparable<Key>\u003c/code\u003e, allowing for serialization and comparison of \u003ccode\u003eKey\u003c/code\u003e objects.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003ecompareTo\u003c/code\u003e method allows comparing two \u003ccode\u003eKey\u003c/code\u003e objects based on their hierarchical structure, application ID, kind, and identifier or name.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eKey\u003c/code\u003e instances can have properties such as \u003ccode\u003eAppId\u003c/code\u003e, \u003ccode\u003eKind\u003c/code\u003e, \u003ccode\u003eName\u003c/code\u003e, \u003ccode\u003eNamespace\u003c/code\u003e, and a parent \u003ccode\u003eKey\u003c/code\u003e, if applicable.\u003c/p\u003e\n"]]],[],null,["# Class Key (2.0.0)\n\n public final class Key implements Serializable, Comparable\u003cKey\u003e\n\nThe primary key for a datastore entity.\n\nA datastore GUID. A Key instance uniquely identifies an entity across all apps, and includes\nall information necessary to fetch the entity from the datastore with `\nDatastoreService.get(Key)`.\n\nYou can create `Key` objects directly by using [KeyFactory#createKey](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory#com_google_appengine_api_datastore_KeyFactory_createKey_) or [#getChild](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key#com_google_appengine_api_datastore_Key_getChild_).\n\nYou can also retrieve the `Key` automatically created when you create a new [Entity](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Entity), or serialize `Key` objects, or use [KeyFactory](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory) to convert them to and from\nwebsafe String values.\nSee Also: [KeyFactory](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory) \n\nInheritance\n-----------\n\n[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \\\u003e Key \n\nImplements\n----------\n\n[Serializable](https://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html), [Comparable\\\u003cKey\\\u003e](https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable\u003ccom/google/appengine/api/datastore/Key\u003e.html) \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\nMethods\n-------\n\n### compareTo(Key other)\n\n public int compareTo(Key other)\n\nCompares two `Key` objects. The algorithm proceeds as follows: Turn each `Key` into\nan iterator where the first element returned is the top-most ancestor, the next element is the\nchild of the previous element, and so on. The last element will be the `Key` we started\nwith. Once we have assembled these two iterators (one for 'this' and one for the `Key`\nwe're comparing to), consume them in parallel, comparing the next element from each iterator.\nIf at any point the comparison of these two elements yields a non-zero result, return that as\nthe result of the overall comparison. If we exhaust the iterator built from 'this' before we\nexhaust the iterator built from the other `Key`, we return less than. An example:\n\n`app1.type1.4.app1.type2.9 \u003c app1.type1.4.app1.type2.9.app1.type3.2`\n\nIf we exhaust the iterator built from the other `Key` before we exhaust the iterator\nbuilt from 'this', we return greater than. An example:\n\n`app1.type1.4.app1.type2.9.app1.type3.2 \u003e app1.type1.4.app1.type2.9`\n\nThe relationship between individual `Key Keys` is performed by comparing app followed\nby kind followed by id. If both keys are assigned names rather than ids, compares names instead\nof ids. If neither key has an id or a name we return an arbitrary but consistent result.\nAssuming all other components are equal, all ids are less than all names.\n\n### equals(@Nullable Object object)\n\n public boolean equals(@Nullable Object object)\n\nCompares two `Key` objects by comparing ids, kinds, parent and appIdNamespace. If both\nkeys are assigned names rather than ids, compares names instead of ids. If neither key has an\nid or a name, the keys are only equal if they reference the same object.\n\n**Overrides** \n[Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-)\n\n### getAppId()\n\n public String getAppId()\n\nReturns the appId for this Key.\n\n### getChild(String kind, String name)\n\n public Key getChild(String kind, String name)\n\nCreates a new key having `this` as parent and the given name. The parent key must be\ncomplete.\n\n### getChild(String kind, long id)\n\n public Key getChild(String kind, long id)\n\nCreates a new key having `this` as parent and the given numeric identifier. The parent\nkey must be complete.\n\n### getId()\n\n public long getId()\n\nReturns the numeric identifier of this `Key`.\n\n### getKind()\n\n public String getKind()\n\nReturns the kind of the `Entity` represented by this `Key`.\n\n### getName()\n\n public @Nullable String getName()\n\nReturns the name of this `Key`.\n\n### getNamespace()\n\n public String getNamespace()\n\nReturns the namespace for this Key.\n\n### getParent()\n\n public @Nullable Key getParent()\n\nIf this `Key` has a parent, return a `Key` that represents it. If not, simply\nreturn null.\n\n### hashCode()\n\n public int hashCode()\n\n**Overrides** \n[Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--)\n\n### isComplete()\n\n public boolean isComplete()\n\nReturns true if this Key has a name specified or has been assigned an identifier.\n\n### toString()\n\n public String toString()\n\n**Overrides** \n[Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--)"]]