Session: Deleting Entities

Entities can be marked for deletion by using the delete method, but will not be removed from the server until saveChanges is called.

Syntax

<T> void delete(T entity);

void delete(String id);

void delete(String id, String expectedChangeVector);
Parameters
entity or id T or String instance of the entity to delete or entity ID
expectedChangeVector String a change vector to use for concurrency checks

Example I

Employee employee = session.load(Employee.class, "employees/1");

session.delete(employee);
session.saveChanges();

Concurrency on Delete

If useOptimisticConcurrency is set to 'true' (default 'false'), the delete() method will use loaded 'employees/1' change vector for concurrency check and might throw ConcurrencyException.

Example II

session.delete("employees/1");
session.saveChanges();

Concurrency on Delete

In this overload, the delete() method will not do any change vector based concurrency checks because the change vector for 'employees/1' is unknown.

Information

If entity is not tracked by session, then executing

session.delete("employees/1");

is equal to doing

session.advanced().defer(new DeleteCommandData("employees/1", null));

Change Vector in DeleteCommandData

In this sample the change vector is null - this means that there will be no concurrency checks. A non-null and valid change vector value will trigger a concurrency check.

You can read more about defer operations here.