Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public final class DocumentSession

  1. Object
  2. DocumentSession

ImplementsAutoCloseable, Closeable

Persists @Entity classes in a DocumentStore: one document per entity, one collection per entity table, each field under its column name.

DocumentSession s = DocumentSession.current();
s.persist(order);
List<Order> open = s.query(Order.class).eq("status", "OPEN").orderBy("placed", false).list();

What the SQL session does that this one does not is listed in the package description: relationships, numeric generated ids, identifiers of more than two columns, an identity map and multi-call transactions.

Constructors

public DocumentSession(DocumentStore store)A session over store; closing the session closes the store.

Methods

public static DocumentSession current() throws IOExceptionThe session of the calling thread’s server, opened from cn1.document.url the first time it is asked for and closed when the server stops.
public static DocumentSession open(Config config) throws IOExceptionA session from cn1.document.url: a mongodb:// connection string, or dynamodb://REGION, optionally with ?endpoint=URL (DynamoDB Local) and &prefix= for the table names.
public static DocumentSession open(String url) throws IOExceptionA session from a document store URL; see open(Config).
public DocumentStore getStore()The store this session writes to.
public void createCollections(Class... types) throws IOExceptionCreates the collection (or table) for each entity type, if missing.
public <T> T persist(T entity) throws IOExceptionInserts entity.
public <T> T merge(T entity) throws IOExceptionReplaces the stored document of entity.
public <T> T find(Class<T> type, Object id) throws IOExceptionThe entity with this id, or null.
public <T> boolean remove(T entity) throws IOExceptionDeletes entity, honouring its @Version like merge(Object).
public <T> DocumentQuery<T> query(Class<T> type)A query over the entities of type.
public void close()Closes this and releases what it holds.

Inherited methods

Constructor details

DocumentSession

public DocumentSession(DocumentStore store)
A session over store; closing the session closes the store. Opened inside a server, it keeps each @Searchable entity’s search index in step with every write that succeeds.

Method details

current

public static DocumentSession current() throws IOException
The session of the calling thread’s server, opened from cn1.document.url the first time it is asked for and closed when the server stops.

open

public static DocumentSession open(Config config) throws IOException
A session from cn1.document.url: a mongodb:// connection string, or dynamodb://REGION, optionally with ?endpoint=URL (DynamoDB Local) and &prefix= for the table names. Without the key, cn1.mongodb.uri and the Spring MongoDB properties are used.

open

public static DocumentSession open(String url) throws IOException
A session from a document store URL; see open(Config).

getStore

public DocumentStore getStore()
The store this session writes to.

createCollections

public void createCollections(Class... types) throws IOException
Creates the collection (or table) for each entity type, if missing.

persist

public <T> T persist(T entity) throws IOException
Inserts entity. A null generated text id is assigned first; the entity returned carries it (a new instance for an immutable entity).

merge

public <T> T merge(T entity) throws IOException
Replaces the stored document of entity. With a @Version field the write succeeds only if the stored version is the entity’s, and the version is incremented; otherwise OptimisticLockException is thrown.

find

public <T> T find(Class<T> type, Object id) throws IOException
The entity with this id, or null.

remove

public <T> boolean remove(T entity) throws IOException
Deletes entity, honouring its @Version like merge(Object). Returns false if there was nothing to delete and the entity has no version.

query

public <T> DocumentQuery<T> query(Class<T> type)
A query over the entities of type.

close

public void close()
Closes this and releases what it holds. Closing it again has no effect.