Entitäten erstellen, abrufen, aktualisieren und löschen
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Datenobjekte in Datastore werden als Entitäten bezeichnet. Jede Entität wird für Abfragen einer bestimmten Art zugewiesen. Wenn Sie beispielsweise eine Anwendung für das Personalwesen schreiben, können Sie jeden Mitarbeiter mit einer Entität der Art Employee darstellen. Die Entitätsdatenwerte liegen dabei in Form von Attributen vor.
Weitere Informationen zu Entitäten finden Sie im Konzeptdokument Entitäten, Attribute und Schlüssel.
Entitäten erstellen und Attribute festlegen
In Java erstellen Sie eine neue Entität, indem Sie eine Instanz der Klasse Entity erstellen und dabei die Art der Entität als Argument für den Konstruktor Entity() zur Verfügung stellen. Nachdem Sie gegebenenfalls Attribute für die Entität eingegeben haben, speichern Sie sie im Datenspeicher. Dazu übergeben Sie die Entität als Argument an die Methode DatastoreService.put(). Sie können den Schlüsselnamen der Entität angeben, wenn Sie ihn als zweites Argument an den Konstruktor übergeben:
Der folgende Beispielcode erstellt eine Entität der Art Employee, gibt ihre Attributwerte an und speichert die Entität im Datenspeicher:
Wenn Sie eine vorhandene Entität aktualisieren möchten, ändern Sie die Attribute des Entitätsobjekts und übergeben es dann an die Methode DatastoreService.put(). Die bestehende Entität wird mit den Objektdaten überschrieben. Bei jedem Aufruf von put() wird das ganze Objekt an Datastore gesendet.
Diese Batch-Vorgänge gruppieren alle Entitäten oder Schlüssel nach Entitätengruppe und führen dann den angeforderten Vorgang für jede Entitätengruppe parallel durch. Dies ist schneller als separate Aufrufe für jede einzelne Entität, weil nur der Aufwand für einen einzigen Dienstaufruf erforderlich ist. Wenn ein Batch-Vorgang auf mehrere Entitätengruppen angewendet wird, wird die Arbeit für alle Gruppen parallel auf der Serverseite ausgeführt.
Ein Batch put() oder delete() ist möglicherweise nur für manche Entitäten erfolgreich.
Wenn es wichtig ist, dass der Aufruf vollständig erfolgreich verläuft oder vollständig fehlschlägt, sollten Sie eine Transaktion verwenden, bei der sich allerdings alle betroffenen Entitäten in derselben Entitätengruppe befinden müssen.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-08-19 (UTC)."],[[["\u003cp\u003eThis API supports first-generation runtimes and can be used when upgrading to corresponding second-generation runtimes, with a migration guide available for App Engine Java 11/17 runtime updates.\u003c/p\u003e\n"],["\u003cp\u003eEntities are data objects in Datastore, categorized under a specific kind for queries, with properties defining their data values.\u003c/p\u003e\n"],["\u003cp\u003eEntities are created in Java using the \u003ccode\u003eEntity\u003c/code\u003e class and saved to Datastore via the \u003ccode\u003eDatastoreService.put()\u003c/code\u003e method, and can be named with a key or given an automatically generated numeric ID.\u003c/p\u003e\n"],["\u003cp\u003eTo update an existing entity, modify its attributes and use \u003ccode\u003eDatastoreService.put()\u003c/code\u003e, overwriting the existing entity, while \u003ccode\u003eDatastoreService.delete()\u003c/code\u003e is used to delete entities based on their keys.\u003c/p\u003e\n"],["\u003cp\u003eBatch operations allow multiple entities to be handled in a single Datastore call, improving efficiency by grouping entities by entity groups and performing operations in parallel.\u003c/p\u003e\n"]]],[],null,["# Creating, Retrieving, Updating, and Deleting Entities\n\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| java-gen2\n|\n| /services/access). If you are updating to the App Engine Java 11/17 runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/java-differences) to learn about your migration options for legacy bundled services.\n\n\u003cbr /\u003e\n\nData objects in Datastore are known as *entities* , each of which is categorized\nunder a particular *kind* for the purpose of queries. For\ninstance, if you are writing a human resources application you might represent\neach employee with an entity of kind `Employee`. Note that the entity data\nvalues are in the form of\n\n[properties](/appengine/docs/legacy/standard/java/datastore/entity-property-reference#properties_and_value_types).\n\n\nFor more information about entities, see the concept document\n[Entities, Properties, and Keys](/appengine/docs/legacy/standard/java/datastore/entities).\n| **Important:** Inefficient data store code can result in unnecessarily high costs! So before you write your code, you should learn about [write costs](/datastore/pricing).\n\nCreating entities and setting properties\n----------------------------------------\n\nIn Java, you create a new entity by constructing an instance of class\n[`Entity`](/appengine/docs/legacy/standard/java/javadoc/com/google/appengine/api/datastore/Entity), supplying the entity's kind as an\nargument to the [`Entity()`](/appengine/docs/legacy/standard/java/javadoc/com/google/appengine/api/datastore/Entity#Entity-java.lang.String-)\nconstructor. After populating the entity's properties if necessary, you save it\nto the datastore by passing it as an argument to the\n[`DatastoreService.put()`](/appengine/docs/legacy/standard/java/javadoc/com/google/appengine/api/datastore/DatastoreService#put-com.google.appengine.api.datastore.Entity-)\nmethod. You can specify the entity's key name by passing it as the second\nargument to the constructor.\n\nThe following example creates an entity of kind `Employee`, populates its property values, and saves it to data store: \n\n Entity employee = new Entity(\"Employee\", \"asalieri\");\n employee.setProperty(\"firstName\", \"Antonio\");\n employee.setProperty(\"lastName\", \"Salieri\");\n employee.setProperty(\"hireDate\", new Date());\n employee.setProperty(\"attendedHrTraining\", true);\n\n DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();\n datastore.put(employee);\n\nNote that if you don't provide a key name, Datastore automatically\ngenerates a numeric ID for the entity's key: \n\n Entity employee = new Entity(\"Employee\");\n // Set the entity properties.\n // ...\n datastore.put(employee);\n\nRetrieving entities\n-------------------\n\nTo retrieve an entity identified by a given key, pass the [`Key`](/appengine/docs/legacy/standard/java/javadoc/com/google/appengine/api/datastore/Key)\nobject to the [`DatastoreService.get()`](/appengine/docs/legacy/standard/java/javadoc/com/google/appengine/api/datastore/DatastoreService#get-com.google.appengine.api.datastore.Key-) method: \n\n // Key employeeKey = ...;\n Entity employee = datastore.get(employeeKey);\n\nUpdating entities\n-----------------\n\nTo update an existing entity, modify the attributes of the Entity object, then pass it to the [`DatastoreService.put()`](/appengine/docs/legacy/standard/java/javadoc/com/google/appengine/api/datastore/DatastoreService#put-com.google.appengine.api.datastore.Entity-) method. The object data overwrites the existing entity. The entire object is sent to Datastore with every call to `put()`. \n\n Entity employee = new Entity(\"Employee\", \"asalieri\");\n employee.setProperty(\"firstName\", \"Antonio\");\n employee.setProperty(\"lastName\", \"Salieri\");\n employee.setProperty(\"hireDate\", new Date());\n employee.setProperty(\"attendedHrTraining\", true);\n\n DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();\n datastore.put(employee);\n\n| **Note:** The Datastore API does not distinguish between creating a new entity and updating an existing one. If the object's key represents an entity that already exists, the `put()` method overwrites the existing entity. You can use a [transaction](/appengine/docs/legacy/standard/java/datastore/transactions) to test whether an entity with a given key exists before creating one.\n\nDeleting entities\n-----------------\n\nGiven an entity's key, you can delete the entity with the [`DatastoreService.delete()`](/appengine/docs/legacy/standard/java/javadoc/com/google/appengine/api/datastore/DatastoreService#e-com.google.appengine.api.datastore.Key...-) method: \n\n // Key employeeKey = ...;\n datastore.delete(employeeKey);\n\nDeleting entities in bulk\n-------------------------\n\nIf you need to delete a large number of entities, we recommend [using\nDataflow to delete entities in bulk](/datastore/docs/bulk-delete).\n\nUsing batch operations\n----------------------\n\nYou can use the batch operations if you want to operate on multiple\nentities in a single Datastore call.\n\nHere is an example of a batch call: \n\n Entity employee1 = new Entity(\"Employee\");\n Entity employee2 = new Entity(\"Employee\");\n Entity employee3 = new Entity(\"Employee\");\n // ...\n\n List\u003cEntity\u003e employees = Arrays.asList(employee1, employee2, employee3);\n datastore.put(employees);\n\nThese batch operations group all the entities or keys by\n[entity group](/appengine/docs/legacy/standard/java/datastore/entities#Transactions_and_entity_groups)\nand then perform the requested operation on each entity group in parallel,\nwhich is faster than making separate calls for each individual entity because\nthey incur the overhead for only one service call. If the batch uses multiple\nentity groups, the work for all groups is done in parallel on the server side.\n\nA batch `put()` or `delete()` call may succeed for some entities but not others.\nIf it is important that the call succeed completely or fail completely, use\na [transaction](/appengine/docs/legacy/standard/java/datastore/transactions) with all affected\nentities in the same entity group.\n| **Note:** Attempting a batch operation inside a transaction with entities or keys belonging to multiple entity groups will result in an `IllegalArgumentException`."]]