Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.
public final class DocumentSession
- Object
- 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
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 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.Throws
open
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. Without the key, cn1.mongodb.uri and the
Spring MongoDB properties are used.Throws
open
public static DocumentSession open(String url)
throws IOExceptionA session from a document store URL; see
open(Config).Throws
getStore
public DocumentStore getStore()The store this session writes to.
createCollections
public void createCollections(Class... types)
throws IOExceptionCreates the collection (or table) for each entity type, if missing.
Throws
persist
public <T> T persist(T entity)
throws IOExceptionInserts
entity. A null generated text id is assigned first; the entity
returned carries it (a new instance for an immutable entity).Throws
merge
public <T> T merge(T entity)
throws IOExceptionReplaces 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.Throws
find
public <T> T find(Class<T> type, Object id)
throws IOExceptionThe entity with this id, or null.
Throws
remove
public <T> boolean remove(T entity)
throws IOExceptionDeletes
entity, honouring its @Version like merge(Object).
Returns false if there was nothing to delete and the entity has no version.Throws
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.