Enumerations

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

Java5 type-safe enumerations are modeled by means of a regular class in UML, 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. This will generate your Java5 enum object containing your literals.

In the EJB3 cartridge, enumerations are NOT persisted behind the scenes, unlike the Hibernate framework, however we can still use these enumeration types for entity attributes.

In the next picture we have replaced the type of the type attribute in the Car entity from datatype::String to org.andromda.test.howto5.a.CarType and set the enumeration type to STRING (default being ORDINAL).

images/org/andromda/test/5/a/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

The enum declaration in the CarType class defines an enum type class.

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. Using the EJB3 cartridge, enum types ignore the attribute name. Therefore, in the EJB3 cartridge you CANNOT have a value that differs from the attribute name. This option is not available.

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.

Enumeration Types

In the above diagram, the tagged value andromda_persistence_enumeration_type specifies the enumeration type for the type attribute of the Car entity as STRING type. If your type-safe enumeration has attributes of type String and you wish to use these literal names, then you must specify the andromda_persistence_enumeration_type tagged value on the entity attribute.

To use the ordinal values relating to the literals of the type-safe enumeration class, you do not need to model the andromda_persistence_enumeration_type tagged value. By default, if an entity attribute has an enumeration type, the ordinal values of the enumeration literals are used.

Adding Data to Enumerations

You can now add data to enumeration literals. This is achieved by adding member variables to the enumeration class.

You simply model the enumeration as you would normally, but add a member variable as an attribute of the class. You have to model the andromda_persistence_enumeration_member_variable tagged value on this attribute to indicate that this is NOT a literal.

Once you have all your literals and member variables, you can model the andromda_persistence_enumeration_literal_parameters tagged value on enumeration literal attributes. This tagged value takes a comma separated list containing the data/values assigned to your member variables.

Remember to order this comma separated list according to the order you added the member variables. This is used in the enum class constructor to initialize the member variables.

Further Reading

To get a better understanding of Java5 enumerations, have a look at Java5 Enums.

Next

Next we'll learn how to model entity finders and have the Query Language automatically generated. Click next to find out more.