Annotation Type Entity
Marks a POJO or PropertyBusinessObject as a persistent entity, in the
app and on the Codename One backend alike.
At build time the Codename One Maven plugin generates a reflection-free
Dao next to the class with createTable, insert, update, delete,
findById, findAll, and find(where, params) methods. Application code
reaches the generated dao through com.codename1.orm.EntityManager:
@Entity(table="users")
public class User {
@Id(autoIncrement=true) public long id;
@Column(name="full_name") public String name;
public int age;
}
EntityManager em = EntityManager.open("MyDB");
Dao<User> users = em.dao(User.class);
users.createTable();
users.insert(new User());
The dao uses the same prepared-statement protocol as the existing
com.codename1.db.Database; no Class.forName lookups, so the binding
survives ParparVM rename / R8 obfuscation in shipped builds.
An entity is the one kind of class both halves of an application own: the
app stores rows of it in its local SQLite file and the server stores rows of
it in PostgreSQL, MySQL or SQLite. The backend runtime compiles this very
annotation (it is marked SharedWithBackend), so the entity class can be a
single file in a module both sides depend on. What differs is what the build
GENERATES from it: against the core the processor emits a dao over
com.codename1.db.Database, against the backend one over
com.codename1.backend.Database, whose SQL is built for whichever engine the
connection turns out to be. The class needs a public no-arg constructor.
Every field that is not static or transient is persistent, whatever its
access; @DbTransient leaves one out. A non-public field is read and written
through a static accessor the build weaves into the class that declares it,
so the generated dao needs no reflection on any platform.
-
Optional Element Summary
Optional Elements
-
Element Details
-
table
String tableSQL table name. When blank the app names the table after the simple class name, and the backend after the same name in snake case (RideEventbecomesride_event), as Spring Boot does.- Default:
""
-
indexes
Index[] indexesAdditional indexes created with the entity table.- Returns:
- index declarations, empty by default
- Default:
{}
-