# Class EntityManager

java.lang.Object
com.codename1.orm.EntityManager

public final class EntityManager extends Object

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 + desktop -- `JavaSEPort#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.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `beginTransaction()`
  Begin a transaction.
  `void`
  `close()`
  Close the underlying database.
  `void`
  `commitTransaction()`
  Commit the current transaction.
  `<T> Dao<T>`
  `dao(java.lang.Class<T> entityClass)`
  Returns the dao for `entityClass`, freshly attached to this entity manager's `Database`.
  `Database`
  `database()`
  The underlying `Database`.
  `static EntityManager`
  `open(Database db)`
  Wraps an existing `Database` (e.g. one opened with a custom path) in an `EntityManager`.
  `static EntityManager`
  `open(String databaseName)`
  Opens (or creates) the SQLite file `databaseName` via `Display.openOrCreate`.
  `static <T> void`
  `registerDao(Dao<T> dao)`
  Installs `dao` under `dao.type().getName()`.
  `void`
  `rollbackTransaction()`
  Roll back the current transaction.

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### open

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

  - ### 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(java.lang.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 java.io.IOException
    Begin a transaction. Equivalent to `database().beginTransaction()`.
    Throws:
      `java.io.IOException`

  - ### commitTransaction

    public void commitTransaction() throws java.io.IOException
    Commit the current transaction.
    Throws:
      `java.io.IOException`

  - ### rollbackTransaction

    public void rollbackTransaction() throws java.io.IOException
    Roll back the current transaction.
    Throws:
      `java.io.IOException`

  - ### close

    public void close() throws java.io.IOException
    Close the underlying database. Idempotent.
    Throws:
      `java.io.IOException`