Class DocumentSession

java.lang.Object
com.codename1.backend.document.DocumentSession
All Implemented Interfaces:
Closeable, AutoCloseable

public final class DocumentSession extends Object implements 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.

  • 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.
      Throws:
      IOException
    • 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.
      Throws:
      IOException
    • open

      public static DocumentSession open(String url) throws IOException
      A session from a document store URL; see open(Config).
      Throws:
      IOException
    • 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.
      Throws:
      IOException
    • 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).
      Throws:
      IOException
    • 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.
      Throws:
      IOException
    • find

      public <T> T find(Class<T> type, Object id) throws IOException
      The entity with this id, or null.
      Throws:
      IOException
    • 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.
      Throws:
      IOException
    • query

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

      public void close()
      Description copied from interface: Closeable
      Closes this and releases what it holds. Closing it again has no effect.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable