Enumerations

You might have noticed the carType attribute in the Car entity. It is is a String type while in fact it makes more sense to restrict the set of possible values for this attribute. This can be achieved using type-safe enumerations. This is the topic discussed on this page.

Type-safe enumerations are modeled by means of a regular class, only this time you need to use the <<Enumeration>> stereotype. All attributes on such an enumeration will be known as enumeration literals, they will assume the default values you assign to the attributes, or the name of the attribute if the default value is missing.

Enumerations are persisted by the Hibernate framework used behind the scenes, so it is no problem to use these enumeration types for entity attributes, in the next picture we have replaced the type of the carType attribute in the Car entity from datatype::String to org.andromda.test.CarType.

howto/org/andromda/test/5/uml.gif

  • Auto-generated source that does not need manual editing
  • Auto-generated source that should be edited manually
  • File that is affected by the modifications applied in this section

Literals

Note that in this example the enumeration attributes have been specified using regular variables in Java notation, the initial value has been specified using capitals (actually you would do this only when you want to have the value different from the attribute name):

sedan : String = SEDAN
liftback : String = LIFTBACK
stationWagon : String = STATION_WAGON
The following is perfectly equivalent, and shorter in use, it specifies the attribute name using capitals and omits the initial value:
SEDAN : String
LIFTBACK : String
STATION_WAGON : String

The latter is recommended for Java applications where it preferred to have literal names matching the persisted values: the enumeration literals will be constants and therefore a capitalized name is desired, and since the name is exactly what will be persisted it is very easy in use too.

Next

We'll learn how to model entity finders and have the Query Language automatically generated, the next section will go into more details.