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,@OneToManyor an element collection is rejected when first used; keep the related id as an ordinary field and query by it.join,leftJoin,fetchandcontainsElementthrow. - No numeric generated identifiers. A generated
Stringid 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_idin MongoDB. - No identity map or dirty tracking. Each call goes to the store:
mergewrites the whole entity, and twofinds return two objects. - No transactions spanning several calls. A
@Versionattribute 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.
-
ClassDescriptionOne predicate of a document query: a field, an operator and its operand.A
Queryover a document collection.Persists@Entityclasses in aDocumentStore: one document per entity, one collection per entity table, each field under its column name.The operationsDocumentSessionneeds of a document database.DocumentStoreover DynamoDB: one table per collection, namedtablePrefix + collection, keyed by the partition key and, for a two-column key, the sort key.DocumentStoreover MongoDB: the identifier is stored as_id, criteria become a query filter, and sorting and paging run on the server.