Class EntityManager
java.lang.Object
com.codename1.orm.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.DaoBootstrapand splices anew cn1app.DaoBootstrap();into the per-build application stub beforeDisplay.init. - JavaSE simulator + desktop --
JavaSEPort#postInitcallsClass.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 TypeMethodDescriptionvoidBegin a transaction.voidclose()Close the underlying database.voidCommit the current transaction.<T> Dao<T> Returns the dao forentityClass, freshly attached to this entity manager'sDatabase.database()The underlyingDatabase.static EntityManagerWraps an existingDatabase(e.g. one opened with a custom path) in anEntityManager.static EntityManagerOpens (or creates) the SQLite filedatabaseNameviaDisplay.openOrCreate.static <T> voidregisterDao(Dao<T> dao) Installsdaounderdao.type().getName().voidRoll back the current transaction.
-
Method Details
-
open
Opens (or creates) the SQLite filedatabaseNameviaDisplay.openOrCreate. ThrowsIOExceptionwhen the platform refuses to provide a SQLite database.- Throws:
IOException
-
open
Wraps an existingDatabase(e.g. one opened with a custom path) in anEntityManager. The caller retains ownership;close()closes the database. -
registerDao
Installsdaounderdao.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
-
database
The underlyingDatabase. Use it for raw SQL when the dao surface isn't enough. -
beginTransaction
Begin a transaction. Equivalent todatabase().beginTransaction().- Throws:
IOException
-
commitTransaction
Commit the current transaction.- Throws:
IOException
-
rollbackTransaction
Roll back the current transaction.- Throws:
IOException
-
close
Close the underlying database. Idempotent.- Throws:
IOException
-