Mapping and Modeling Java Enumerations
Cayenne allows to use any Java enumeration as an ObjAttribute type. In CayenneModeler's ObjEntity editor, under the Attributes tab, enter the full class name for your enumeration under the Java Type column, and this is it:
To convert a DB column value to an enumeration instance and back Cayenne uses enumeration's name for character colums (CHAR, VARCHAR, etc) or its ordinal for numeric columns. Also Cayenne allows users to explicitly control what value in DB corresponds to a given enumeration instance. To do that, a custom enumeration must implement org.apache.cayenne.ExtendedEnumeration interface, overriding "getDatabaseValue()" method to provide the DB value. Here is an example of a custom enumeration that maps to DB integers:
import org.apache.cayenne.ExtendedEnumeration; public enum Color implements ExtendedEnumeration { RED(3), GREEN(6), BLUE(9); private Integer value; private Color(Integer value) { this.value = value; } public Integer getDatabaseValue() { return value; } }
This instructs Cayenne to read/write 3, 6, and 9 as RED, GREEN, and BLUE, respectively. The order is unimportant - if someone re-orders them to be BLUE, GREEN, and RED in the enum class, all values will still map correctly.