Skip to content

Examples

Tilmann edited this page Feb 25, 2016 · 8 revisions

Examples

ZooDB contains usage examples in the /examples folder.

Example 1 - Basic Usage

Example code

This example demonstrates basic functionality:

  • Create/delete databases
  • How to turn a class into a persistent class: add extends ZooPC and an default constructor to the class definition and insert zooActivateRead() into getter methods and zooActivateWrite() into setter methods
  • Open a database
  • Transparent persistence: Once one persistent object (PC) is made persistent, all reachable objects will be stored as well. Changes to objects are detected automatically (via zooActivateWrite()).
  • Basic queries
  • Extents: a faster query, but without filter

Example 2 - Inheritance, Indexing, Navigation & Path-Queries

Example code

This example demonstrates some more advanced features:

  • How to handle inheritance: Simply extend a persistent class
  • Indexes can be created with ZooJdoHelper.createIndex(...)
  • How to use navigation: simplify follow a Java reference (no JOIN on primary keys needed!)
  • Path Queries: query over references, this is similar to multiple JOINs in RDBMS. For example, querying teacher.name == "Peter" on the Course class returns all courses whose teacher is called Peter. Null references always evaluate to false.

Example 3 - Accessing Objects Outside of Transactions with Non-Transactional Read

Example code

This example that shows how to access object outside of transactions with non-transactional read. This can be useful in GUI applications where a transaction should be closed most of the time, while the GUI should still be able to read data from the database.

Note that in a setting with multiple concurrent persistence managers, this may result in inconsistencies because any values that were read outside of a transaction may be outdated by the time when the next transaction commits.

Non-transactional can be configured on different levels, for example on the PersistenceManagerFasctory or on the Transaction.