You must code getters and setters on your entity interface, and make an informed decision as to the level of visibility of each field.
You'll step through an example database table and code getters/setters in your API.
Let's go through the attributes on MyNewEntity and flesh out the entity API.
In general, getters and setters for your primary key column are straightforward - you don't write any.
You rarely need to code anything for the primary key of an entity, because each entity already has a getID method (inherited from StandardEntity).
/** ********** VERY VERY BAD - DO NOT DO THIS! ********** */ /** * @return the primary key of MyNewEntity. */ public Long getMyNewEntityID(); /** ********** VERY VERY BAD - DO NOT DO THIS! ********** */
Similarly, each entity implementation typically takes care of assigning its own primary key, and so callers of the entity API do not require a facility to set the primary key themselves.
/** ********** VERY VERY BAD - DO NOT DO THIS! ********** */ /** * @param value * the primary key of MyNewEntity. */ public void setMyNewEntityID(final Long value); /** ********** VERY VERY BAD - DO NOT DO THIS! ********** */
After analysis of requirements, you determine that callers of your API require to both get and set the name column of a database row.
Code a field getter as follows:
/** * Gets the name. * * @return the name */ public String getName();
Code a field setter as follows:
/** * Sets the name. * * @param value * the name */ public void setName(final String value);
You require your entity to return a DateRange helper object - rather than coding a getDateRange method, instead it's better to change your API to extend DateRanged:
/** * Description of my wonderful new entity. */ public interface MyNewEntity extends StandardEntity, DateRanged {
The DateRanged interface provides your entity with a getDateRange method and also allows access to helper functions which provide commonly-used processing on entities which contain a date range.
After analysis, you determine that your entity stores a codetable code describing the "type" of the entity instance.
Create a codetable specifying the permitted values:
<?xml version="1.0" encoding="UTF-8"?> <codetables package="curam.mypackage.codetable"> <codetable java_identifier="MYNEWENTITYTYPE" name="MYNEWENTITYTYPE" > <code default="false" java_identifier="SOMETYPE" status="ENABLED" value="TYPE1" > <locale language="en" sort_order="0" > <description>Some type</description> <annotation/> </locale> </code> <code default="false" java_identifier="SOMEOTHERTYPE" status="ENABLED" value="TYPE2" > <locale language="en" sort_order="0" > <description>Some other type</description> <annotation/> </locale> </code> </codetable> </codetables>
The Persistence Infrastructure includes a code generator to generate a class per codetable. These classes provide a type-safe mechanism for passing around an entry from the codetable, and each class is named after its codetable suffixed with the word Entry:
package curam.mypackage.codetable.impl; /** * Represents an entry from the * {@linkplain curam.mypackage.codetable.MYNEWENTITYTYPE} code * table. */ public class MYNEWENTITYTYPEEntry extends curam.util.type.CodeTableEntry { // ... /** * Private constructor. */ private MYNEWENTITYTYPEEntry(final String code) { super(TABLENAME, code); } /** * Gets the * {@linkplain curam.mypackage.codetable.impl.MYNEWENTITYTYPEEntry} * for the specified code value. * * @param code * the String representation of the code value required. * * @return a * {@linkplain curam.mypackage.codetable.impl.MYNEWENTITYTYPEEntry} * representation of the specified code value. * * @throws curam.util.exception.AppRuntimeException * if the specified code value is not present in the * {@linkplain curam.mypackage.codetable.MYNEWENTITYTYPE} * code table. */ public static curam.mypackage.codetable.impl.MYNEWENTITYTYPEEntry get( final String code) { // ... } /** * The name of the * {@linkplain curam.mypackage.codetable.MYNEWENTITYTYPE} table - * {@value}. */ public static String TABLENAME = curam.mypackage.codetable.MYNEWENTITYTYPE.TABLENAME; /** * Not specified (i.e. blank). */ public static final curam.mypackage.codetable.impl.MYNEWENTITYTYPEEntry NOT_SPECIFIED = get(null); /** * TYPE1 en = Some type */ public static final curam.mypackage.codetable.impl.MYNEWENTITYTYPEEntry SOMETYPE = get(curam.mypackage.codetable.MYNEWENTITYTYPE.SOMETYPE); /** * TYPE2 en = Some other type */ public static final curam.mypackage.codetable.impl.MYNEWENTITYTYPEEntry SOMEOTHERTYPE = get(curam.mypackage.codetable.MYNEWENTITYTYPE.SOMEOTHERTYPE); }
Use of this generated class is preferable to using a String to pass around the value, as (in particular) a String can be constructed with any text whereas the generated class only permits values corresponding to the underlying code table.
/** * Gets the type of this entity instance. * * @return the type of this entity instance */ public MYNEWENTITYTYPEEntry getType(); /** * Sets the type of this entity instance. * * @param value * the type of this entity instance */ public void setType(final MYNEWENTITYTYPEEntry value);
In particular, some database column names are abbreviated to comply with database name length constraints, and for these the getter and setter names should not slavishly repeat the abbreviation, e.g. use getSomeVeryVeryLongDatabaseColumnName rather than getSmVyVyLgDbColNm.
For foreign keys to related entity instances, in general you should not create getters and setters for the entity ID, but instead code getters and setters which deal with the API of the related entity:
/** * Gets the parent instance of MyParentEntity. * * @return the parent instance of MyParentEntity, or null if not * yet set */ public MyParentEntity getMyParentEntity(); /** * Sets the parent instance of MyParentEntity. * * @param value * the parent instance of MyParentEntity */ public void setMyParentEntity(final MyParentEntity value);
Note that this code assumes that the MyParentEntity API has already been coded. If not, you must create a skeletal API:
public interface MyParentEntity extends StandardEntity { }
/** ********** VERY VERY BAD - DO NOT DO THIS! ********** */ /** * @return the foreign key to the parent MyParentEntity instance */ public Long getMyParentEntityID(); /** * @param value * the foreign key to the parent MyParentEntity instance */ public void setMyParentEntityID(final Long value); /** ********** VERY VERY BAD - DO NOT DO THIS! ********** */
Each instance of your entity has a set of associated child entity instances (from a different database table).
If callers of your API require to navigate to these child instances, code a getter which returns a Set, typed with the API of the child entity:
/** * Gets the MyChildEntity children of this entity instance. * * @return the MyChildEntity children of this entity instance */ public Set<MyChildEntity> getMyChildren();
Note that this code assumes that the MyChildEntity API has already been coded. If not, you must create a skeletal API:
public interface MyChildEntity extends StandardEntity { }