Defining an entity
In Active Objects an entity is defined by an interface that extends the
net.java.ao.RawEntity<T> interface. Here
T defines the type of the primary key used for the entity.
In most cases, and this is advised to do so, your interfaces will extends
net.java.ao.Entity instead of extending directly
net.java.ao.RawEntity<T>. This interface defines for you the primary key as an
Entity is defined as follow:
Annotations here are self explanatory, but for good measure:
@NotNullmeans that this member cannot be persisted as
@PrimaryKeydefines this member as the primary key of the entity, with the (column) name
@AutoIncrementtells that the primary key is an auto generated integer.
Here is a simple example of an entity that extends this class. This code is an extract from the dogfood blog:
Active Objects will take care of naming the table and column names according to the name of the interface and methods.
Creating an entity
Once an entity is defined, creating and persisting an object represented by that entity is very simple, given an
This code can be found in the
BlogService implementation of the DogFood blog
Creating an entity with "not null"-constraints
The previous way of creating an entity works only as long as there are no "not null"-constraints on fields other than the primary key. Consider the case of someone adding a field to the sample entity:
entityManager.create(Blog.class); will not work, since the
author field needs to be provided. The
create call accepts parameters as well, so the code would look like this:
The column names are used here, so they must conform to the Column names convention.