In ClusterJ (as in JPA), annotations are used to describe how the interface is mapped to tables in a database. An annotated interface looks like this:
@PersistenceCapable(table="employee") @Index(name="idx_uhash") public interface Employee { @PrimaryKey int getId(); void setId(int id); String getFirst(); void setFirst(String first); String getLast(); void setLast(String last); @Column(name="municipality") @Index(name="idx_municipality") String getCity(); void setCity(String city); Date getStarted(); void setStarted(Date date); Date getEnded(); void setEnded(Date date); Integer getDepartment(); void setDepartment(Integer department); }
This interface maps seven columns: id
,
first
, last
,
municipality
started
,
ended
, and department
. The
annotation
@PersistenceCapable(table="employee")
is used
to let ClusterJ know which database table to map the
Employee
to (in this case, the
employee
table). The
@Column
annotation is used because the
city
property name implied by the
getCity()
and setCity()
methods is different from the mapped column name
municipality
. The annotations
@PrimaryKey
and @Index
inform ClusterJ about indexes in the database table.
The implementation of this interface is created dynamically by
ClusterJ at runtime. When the
newInstance()
method is called, ClusterJ creates an implementation class for
the Employee
interface; this class stores the
values in an internal object array.
ClusterJ does not require an annotation for every attribute. ClusterJ automatically detects the primary keys of tables; while there is an annotation in ClusterJ to allow the user to describe the primary keys of a table (see previous example), when specified, it is currently ignored. (The intended use of this annotation is for the generation of schemas from the domain object model interfaces, but this is not yet supported.)
The annotations themselves must be imported from the ClusterJ
API. They can be found in package
com.mysql.clusterj.annotation
,
and can be imported like this:
import com.mysql.clusterj.annotation.Column; import com.mysql.clusterj.annotation.Index; import com.mysql.clusterj.annotation.PersistenceCapable; import com.mysql.clusterj.annotation.PrimaryKey;