public final class EntityManager

  1. Object
  2. EntityManager

Public entry point for the build-time SQLite ORM.

@Entity classes get a generated dao at build time. Each dao’s static initializer self-registers with this registry. The registry stays empty until something triggers each generated class’s <clinit>:

  • iOS / Android – the build server probes the project zip for cn1app.DaoBootstrap and splices a new cn1app.DaoBootstrap(); into the per-build application stub before Display.init.
  • JavaSE simulator + desktopJavaSEPort#postInit calls Class.forName("cn1app.DaoBootstrap") so the registry is populated on the same boundary.
  • Unit tests / manual init – application code can call EntityManager.registerDao(...) directly.
EntityManager em = EntityManager.open("MyApp.db");
Dao<User> users = em.dao(User.class);
users.createTable();
users.insert(new User("alice"));
for (User u : users.findAll()) { ... }
em.close();

The registry is keyed on getClass().getName() so it survives ParparVM rename and R8 obfuscation: both the registration site and the lookup site see the same renamed name within a single execution.

Methods

public static EntityManager open(String databaseName) throws IOExceptionOpens (or creates) the SQLite file databaseName via Display.openOrCreate.
public static EntityManager open(Database db)Wraps an existing Database (e.g. one opened with a custom path) in an EntityManager.
public static <T> void registerDao(Dao<T> dao)Installs dao under dao.type().getName().
public <T> Dao<T> dao(Class<T> entityClass)Returns the dao for entityClass, freshly attached to this entity manager’s Database.
public Database database()The underlying Database.
public void beginTransaction() throws IOExceptionBegin a transaction.
public void commitTransaction() throws IOExceptionCommit the current transaction.
public void rollbackTransaction() throws IOExceptionRoll back the current transaction.
public void close() throws IOExceptionClose the underlying database.

Inherited methods

Method details

open

public static EntityManager open(String databaseName) throws IOException
Opens (or creates) the SQLite file databaseName via Display.openOrCreate. Throws IOException when the platform refuses to provide a SQLite database.

open

public static EntityManager open(Database db)
Wraps an existing Database (e.g. one opened with a custom path) in an EntityManager. The caller retains ownership; close() closes the database.

registerDao

public static <T> void registerDao(Dao<T> dao)
Installs dao under dao.type().getName(). The generated per-class dao’s static initializer calls this; hand-written daos for classes outside the build’s annotation scan call it explicitly.

dao

public <T> Dao<T> dao(Class<T> entityClass)
Returns the dao for entityClass, freshly attached to this entity manager’s Database. Throws IllegalStateException when no dao was generated for the class – typically because @Entity is missing or the process-annotations Mojo did not run.

database

public Database database()
The underlying Database. Use it for raw SQL when the dao surface isn’t enough.

beginTransaction

public void beginTransaction() throws IOException
Begin a transaction. Equivalent to database().beginTransaction().

commitTransaction

public void commitTransaction() throws IOException
Commit the current transaction.

rollbackTransaction

public void rollbackTransaction() throws IOException
Roll back the current transaction.

close

public void close() throws IOException
Close the underlying database. Idempotent.