Data manipulation
The Java SDK provides dedicated methods for common CRUD operations on records and tables. These methods offer a structured alternative to writing raw SurrealQL, with built-in type safety through Java generics and POJO deserialization.
API References
| Method | Description |
|---|---|
db.create(target, content) | Creates one or more records |
db.select(target) | Selects records from a table or by ID |
db.insert(target, content) | Inserts one or more records |
db.update(target, upType, content) | Updates records |
db.upsert(target, upType, content) | Updates or creates records |
db.delete(target) | Deletes records |
db.relate(from, table, to) | Creates a graph relation |
db.insertRelation(target, content) | Inserts a relation record |
Defining model classes
Data manipulation methods can work with POJOs (Plain Old Java Objects) for type-safe access. Model classes need a public no-argument constructor and fields that map to SurrealDB object keys. Use a RecordId field named id to hold the record identifier.
Creating records
The .create() method creates new records. When called with a table name, SurrealDB generates a random ID and returns a list. When called with a RecordId, the record is created with that specific ID and a single result is returned.
Selecting records
The .select() method reads records from the database. When called with a table name, it returns an Iterator. When called with a RecordId, it returns an Optional.
Use selectSync() instead of select() when you need thread-safe iteration over the results.
Inserting records
The .insert() method inserts one or more records into a table using varargs. This is more efficient than calling .create() in a loop for bulk operations.
Use .insertRelation() to insert graph edge records. See Creating graph relations for details.
Updating records
The .update() method modifies existing records. You specify the update strategy using the UpType enum:
| UpType | Behavior |
|---|---|
UpType.CONTENT | Replaces the entire record with the new content |
UpType.MERGE | Merges new fields into the existing record |
UpType.PATCH | Applies a JSON Patch to the record |
Upserting records
The .upsert() method works like .update() but creates the record if it does not already exist. It accepts the same UpType strategies.
Deleting records
The .delete() method removes records from the database. You can delete a single record by RecordId, multiple records by passing several RecordId values, a range of records with RecordIdRange, or all records in a table by passing the table name.
Creating graph relations
The .relate() method creates edges between records in SurrealDB's graph model. You specify the source record, the edge table, and the target record. Optionally, you can attach content to the edge.
You can also use .insertRelation() to insert relation records with in and out fields, similar to how .insert() works for regular records.
Learn more
Surreal API reference for complete method signatures
Value types for type mappings and the Value class
Executing queries for custom SurrealQL queries
RecordId reference for record identifier details
SurrealQL SELECT, CREATE, UPDATE, DELETE for the underlying query statements
SurrealQL RELATE for graph relation syntax
Record IDs for SurrealDB record identifier formats