Stay organized with collections
Save and categorize content based on your preferences.
publicstaticfinalclassKeyFactory.Builder
Helper class that aids in the construction of Keys with ancestors. Initialize the
Builder with the topmost ancestor in your key path and then add children using the
#addChild overload that best suits your needs. When finished adding children, call
#getKey() to retrieve your Key or #getString() to retrieve your Key encoded as a websafe String.
[[["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\u003e\u003ccode\u003eKeyFactory.Builder\u003c/code\u003e is a helper class designed to construct \u003ccode\u003eKey\u003c/code\u003e objects with ancestors.\u003c/p\u003e\n"],["\u003cp\u003eYou can initialize the \u003ccode\u003eBuilder\u003c/code\u003e with the topmost ancestor and add children using the \u003ccode\u003eaddChild\u003c/code\u003e method, either with a name or a numeric ID.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003egetKey()\u003c/code\u003e method retrieves the constructed \u003ccode\u003eKey\u003c/code\u003e object.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003egetString()\u003c/code\u003e method returns the constructed \u003ccode\u003eKey\u003c/code\u003e encoded as a websafe string.\u003c/p\u003e\n"],["\u003cp\u003eThe class can be constructed using a predefined Key, or a kind and either a name or ID.\u003c/p\u003e\n"]]],[],null,["# Class KeyFactory.Builder (2.0.0)\n\n public static final class KeyFactory.Builder\n\nHelper class that aids in the construction of [Keys](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) with ancestors. Initialize the\n`Builder` with the topmost ancestor in your key path and then add children using the\n[#addChild](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory.Builder#com_google_appengine_api_datastore_KeyFactory_Builder_addChild_) overload that best suits your needs. When finished adding children, call\n[#getKey()](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory.Builder#com_google_appengine_api_datastore_KeyFactory_Builder_getKey__) to retrieve your [Key](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) or [#getString()](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.KeyFactory.Builder#com_google_appengine_api_datastore_KeyFactory_Builder_getString__) to retrieve your [Key](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) encoded as a websafe [String](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html).\n\nExamples: \n\n\n import com.google.appengine.api.datastore.KeyFactory.Builder;\n\n ...\n\n Key key = new Builder(\"Person\", 88).addChild(\"Address\", 24).getKey();\n String keyStr = new Builder(\"Photo Album\", \"Vacation\").addChild(\"Photo\", 1424).getString();\n \nInheritance\n-----------\n\n[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \\\u003e KeyFactory.Builder \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\nConstructors\n------------\n\n### Builder(Key key)\n\n public Builder(Key key)\n\nCreate a `Builder`, establishing the provided [Key](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) as the topmost ancestor.\n\n### Builder(String kind, String name)\n\n public Builder(String kind, String name)\n\nCreate a `Builder`, establishing a [Key](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) constructed from the provided kind and\nname as the topmost ancestor.\n\n### Builder(String kind, long id)\n\n public Builder(String kind, long id)\n\nCreate a `Builder`, establishing a [Key](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) constructed from the provided kind and\nid as the topmost ancestor.\n\nMethods\n-------\n\n### addChild(String kind, String name)\n\n public KeyFactory.Builder addChild(String kind, String name)\n\nAdd a [Key](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) constructed from the provided kind and name as the child of the [Key](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key)\nmost recently added to the `Builder`.\n\n### addChild(String kind, long id)\n\n public KeyFactory.Builder addChild(String kind, long id)\n\nAdd a [Key](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key) constructed from the provided kind and id as the child of the [Key](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key)\nmost recently added to the `Builder`.\n\n### getKey()\n\n public Key getKey()\n\nReturns the most recently added [Key](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key).\n\n### getString()\n\n public String getString()\n\nReturns the most recently added [Key](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key), encoded as a websafe [String](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html)."]]