Back to Examples

Updating data

Persistence, INSERT and UPDATE examples using DTO mode, SQL mode, and native SQL.

Persisting entities

The save() method is the primary mechanism for storing entities/mapped DTOs in Litebridge. It automatically executes an INSERT, UPDATE or MERGE as needed.

PersistenceExample.java
import org.example.dto.Person;
// Create a new Person object
Person person = new Person();
person.setId(1);
person.setName("Alice");
person.setSurname("Smith");

// Executes an upsert, using MERGE if supported, or UPDATE->INSERT if not
litebridge.save(person);

// Update a field in the DTO
person.Name("Bob");

// Executes UPDATE LB.PERSON SET FIRST_NAME = ? WHERE PERSON_ID = ?
litebridge.save(person);

Insert data

Explicitly insert an entity instance into the database. This ignores the persisted state of the entity and inserts all fields.

InsertExample.java
import org.example.dto.Person;
Person person = new Person();
person.setId(1);
person.setName("Alice");
person.setSurname("Smith");

// Executes INSERT INTO LB.PERSON (FIRST_NAME, SURNAME) VALUES (?, ?)
litebridge.insert(person);

Update data

Explicitly update an entity instance. This ignores the persisted state of the entity and updates all modified fields.

UpdateExample.java
import org.example.dto.Person;
Person person = new Person();
person.setId(1);
person.setName("Alice");
person.setSurname("Smith");

// Executes UPDATE LB.PERSON SET FIRST_NAME = ?, SURNAME = ? WHERE PERSON_ID = ?
litebridge.update(person);

Upsert/merge data

See Merging data.