Package com.codename1.backend.document


package com.codename1.backend.document

@Entity classes stored in MongoDB or DynamoDB instead of a SQL database.

The same generated entity models the SQL session uses map an entity to a document: each persistent attribute becomes a field named by its column, and the identifier becomes MongoDB's _id or the DynamoDB table's partition key. DocumentSession has the session operations -- persist, merge, find, remove -- and query returns the ORM's own Query, so a repository written against it moves between SQL and documents unchanged.

DocumentSession session = DocumentSession.current();   // cn1.document.url
session.persist(order);
List<Order> open = session.query(Order.class).eq("status", "open")
        .orderBy("created", false).limit(20).list();

What a document store cannot do the SQL session's way is refused rather than approximated:

  • No relationships. An entity with @ManyToOne, @OneToMany or an element collection is rejected when first used; keep the related id as an ordinary field and query by it. join, leftJoin, fetch and containsElement throw.
  • No numeric generated identifiers. A generated String id gets a new ObjectId (MongoDB) or a random 128-bit hex id (DynamoDB); a generated number needs a SQL sequence.
  • A composite identifier has exactly two columns, one marked SortKey. That is DynamoDB's partition and sort key, and a compound _id in MongoDB.
  • No identity map or dirty tracking. Each call goes to the store: merge writes the whole entity, and two finds return two objects.
  • No transactions spanning several calls. A @Version attribute is honoured on every write, which is what keeps concurrent updates safe.

On DynamoDB a query with an equality on the partition key reads only that partition, using at most one comparison on the sort key; every other query scans the table. Either way sorting and paging happen in memory. A scan is right for small tables and administration screens; a hot path on a large table belongs on a partition key, or on DynamoDb.query(String, String, String, Map, Map, int) with a secondary index.

  • Class
    Description
    One predicate of a document query: a field, an operator and its operand.
    A Query over a document collection.
    Persists @Entity classes in a DocumentStore: one document per entity, one collection per entity table, each field under its column name.
    The operations DocumentSession needs of a document database.
    DocumentStore over DynamoDB: one table per collection, named tablePrefix + collection, keyed by the partition key and, for a two-column key, the sort key.
    DocumentStore over MongoDB: the identifier is stored as _id, criteria become a query filter, and sorting and paging run on the server.