Java 8 has reached end of support
and will be
deprecated
on January 31, 2026. After deprecation, you won't be able to deploy Java 8
applications, even if your organization previously used an organization policy to
re-enable deployments of legacy runtimes. Your existing Java
8 applications will continue to run and receive traffic after their
deprecation date. We recommend that
you
migrate to the latest supported version of Java.
Key (Google App Engine API for Java)
Stay organized with collections
Save and categorize content based on your preferences.
- java.lang.Object
-
- com.google.appengine.api.datastore.Key
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Comparable<Key>
public final class Key
extends java.lang.Object
implements java.io.Serializable, java.lang.Comparable<Key>
The primary key for a datastore entity.
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 create Key
objects directly by using KeyFactory.createKey(java.lang.String, long)
or getChild(java.lang.String, long)
.
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
,
Serialized Form
-
-
Method Summary
All Methods Instance Methods Concrete Methods
Modifier and Type |
Method and Description |
int |
compareTo(Key other)
Compares two Key objects.
|
boolean |
equals(java.lang.Object object)
Compares two Key objects by comparing ids, kinds, parent and appIdNamespace.
|
java.lang.String |
getAppId()
Returns the appId for this Key .
|
Key |
getChild(java.lang.String kind,
long id)
Creates a new key having this as parent and the given numeric identifier.
|
Key |
getChild(java.lang.String kind,
java.lang.String name)
Creates a new key having this as parent and the given name.
|
long |
getId()
Returns the numeric identifier of this Key .
|
java.lang.String |
getKind()
Returns the kind of the Entity represented by this Key .
|
java.lang.String |
getName()
Returns the name of this Key .
|
java.lang.String |
getNamespace()
Returns the namespace for this Key .
|
Key |
getParent()
If this Key has a parent, return a Key that represents it.
|
int |
hashCode() |
boolean |
isComplete()
Returns true if this Key has a name specified or has been assigned an identifier.
|
java.lang.String |
toString() |
-
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
-
-
Method Detail
-
hashCode
public int hashCode()
- Overrides:
hashCode
in class java.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in class java.lang.Object
-
equals
public boolean equals(java.lang.Object object)
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.
- Overrides:
equals
in class java.lang.Object
-
getAppId
public java.lang.String getAppId()
Returns the appId for this
Key
.
-
getNamespace
public java.lang.String getNamespace()
Returns the namespace for this
Key
.
-
compareTo
public int compareTo(Key other)
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:
app1.type1.4.app1.type2.9 < app1.type1.4.app1.type2.9.app1.type3.2
If we exhaust the iterator built from the other Key
before we exhaust the iterator
built from 'this', we return greater than. An example:
app1.type1.4.app1.type2.9.app1.type3.2 > app1.type1.4.app1.type2.9
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.
- Specified by:
compareTo
in interface java.lang.Comparable<Key>
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-06-16 UTC.
[[["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-06-16 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eKey\u003c/code\u003e class serves as the primary key for a datastore entity, uniquely identifying it across all applications and containing all necessary information to retrieve it.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eKey\u003c/code\u003e objects can be created directly using \u003ccode\u003eKeyFactory.createKey\u003c/code\u003e or \u003ccode\u003egetChild\u003c/code\u003e, and can also be automatically generated when creating a new \u003ccode\u003eEntity\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eKey\u003c/code\u003e class provides methods to access the key's attributes, such as \u003ccode\u003egetAppId\u003c/code\u003e, \u003ccode\u003egetNamespace\u003c/code\u003e, \u003ccode\u003egetId\u003c/code\u003e, \u003ccode\u003egetName\u003c/code\u003e, \u003ccode\u003egetKind\u003c/code\u003e, and \u003ccode\u003egetParent\u003c/code\u003e, allowing for detailed inspection.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003ecompareTo\u003c/code\u003e method allows for comparison between two \u003ccode\u003eKey\u003c/code\u003e objects, turning each into an iterator and comparing in parallel to determine the order between the keys.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eequals\u003c/code\u003e method allows for comparison between two keys by comparing their ids, kinds, parent, and appIdNamespace.\u003c/p\u003e\n"]]],[],null,["# Key (Google App Engine API for Java)\n\ncom.google.appengine.api.datastore\n\nClass Key\n---------\n\n- java.lang.Object\n-\n - com.google.appengine.api.datastore.Key\n\n-\n\n All Implemented Interfaces:\n : java.io.Serializable, java.lang.Comparable\\\u003c[Key](../../../../../com/google/appengine/api/datastore/Key.html \"class in com.google.appengine.api.datastore\")\\\u003e\n\n *** ** * ** ***\n\n \u003cbr /\u003e\n\n ```\n public final class Key\n extends java.lang.Object\n implements java.io.Serializable, java.lang.Comparable\u003cKey\u003e\n ``` \n The primary key for a datastore entity.\n\n A datastore GUID. A Key instance uniquely identifies an entity across all apps, and includes\n all information necessary to fetch the entity from the datastore with `DatastoreService.get(Key)`.\n\n You can create `Key` objects directly by using [`KeyFactory.createKey(java.lang.String, long)`](../../../../../com/google/appengine/api/datastore/KeyFactory.html#createKey-java.lang.String-long-) or [`getChild(java.lang.String, long)`](../../../../../com/google/appengine/api/datastore/Key.html#getChild-java.lang.String-long-).\n\n You can also retrieve the `Key` automatically created when you create a new [`Entity`](../../../../../com/google/appengine/api/datastore/Entity.html \"class in com.google.appengine.api.datastore\"), or serialize `Key` objects, or use [`KeyFactory`](../../../../../com/google/appengine/api/datastore/KeyFactory.html \"class in com.google.appengine.api.datastore\") to convert them to and from\n websafe String values.\n\n \u003cbr /\u003e\n\n \u003cbr /\u003e\n\n See Also:\n : [`KeyFactory`](../../../../../com/google/appengine/api/datastore/KeyFactory.html \"class in com.google.appengine.api.datastore\"),\n[Serialized Form](../../../../../serialized-form.html#com.google.appengine.api.datastore.Key) \n-\n -\n\n ### Method Summary\n\n -\n\n ### Methods inherited from class java.lang.Object\n\n`getClass, notify, notifyAll, wait, wait, wait` \n-\n -\n\n ### Method Detail\n\n\n -\n\n #### getKind\n\n ```\n public java.lang.String getKind()\n ``` \n Returns the kind of the `Entity` represented by this `Key`.\n\n\n -\n\n #### getParent\n\n ```\n public Key getParent()\n ``` \n If this `Key` has a parent, return a `Key` that represents it. If not, simply return null.\n\n\n -\n\n #### hashCode\n\n ```\n public int hashCode()\n ```\n\n Overrides:\n : `hashCode` in class `java.lang.Object`\n\n\n -\n\n #### toString\n\n ```\n public java.lang.String toString()\n ```\n\n Overrides:\n : `toString` in class `java.lang.Object`\n\n\n -\n\n #### equals\n\n ```\n public boolean equals(java.lang.Object object)\n ``` \n 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.\n\n Overrides:\n : `equals` in class `java.lang.Object`\n\n\n -\n\n #### getAppId\n\n ```\n public java.lang.String getAppId()\n ``` \n Returns the appId for this [`Key`](../../../../../com/google/appengine/api/datastore/Key.html \"class in com.google.appengine.api.datastore\").\n\n\n -\n\n #### getNamespace\n\n ```\n public java.lang.String getNamespace()\n ``` \n Returns the namespace for this [`Key`](../../../../../com/google/appengine/api/datastore/Key.html \"class in com.google.appengine.api.datastore\").\n\n\n -\n\n #### getId\n\n ```\n public long getId()\n ``` \n Returns the numeric identifier of this `Key`.\n\n\n -\n\n #### getName\n\n ```\n public java.lang.String getName()\n ``` \n Returns the name of this `Key`.\n\n\n -\n\n #### getChild\n\n ```\n public Key getChild(java.lang.String kind,\n long id)\n ``` \n Creates a new key having `this` as parent and the given numeric identifier. The parent key must be complete.\n\n Parameters:\n : `kind` - the kind of the child key to create\n : `id` - the numeric identifier of the key in `kind`, unique for this parent\n\n\n -\n\n #### getChild\n\n ```\n public Key getChild(java.lang.String kind,\n java.lang.String name)\n ``` \n Creates a new key having `this` as parent and the given name. The parent key must be complete.\n\n Parameters:\n : `kind` - the kind of the child key to create\n : `name` - the name of the key in `kind`, as an arbitrary string unique for this parent\n\n\n -\n\n #### isComplete\n\n ```\n public boolean isComplete()\n ``` \n Returns true if this Key has a name specified or has been assigned an identifier.\n\n\n -\n\n #### compareTo\n\n ```\n public int compareTo(Key other)\n ``` \n 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:\n\n `app1.type1.4.app1.type2.9 \u003c app1.type1.4.app1.type2.9.app1.type3.2`\n\n If we exhaust the iterator built from the other `Key` before we exhaust the iterator\n built 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\n The relationship between individual `Key Keys` is performed by comparing app followed\n by kind followed by id. If both keys are assigned names rather than ids, compares names instead\n of ids. If neither key has an id or a name we return an arbitrary but consistent result.\n Assuming all other components are equal, all ids are less than all names.\n\n \u003cbr /\u003e\n\n \u003cbr /\u003e\n\n \u003cbr /\u003e\n\n Specified by:\n : `compareTo` in interface `java.lang.Comparable\u003c`[Key](../../../../../com/google/appengine/api/datastore/Key.html \"class in com.google.appengine.api.datastore\")`\u003e`"]]