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
.
Car.hbm.xml
Car.java
CarImpl.java
CarDao.java
CarDaoBase.java
CarDaoImpl.java
CarType.java
Person.hbm.xml
Person.java
PersonImpl.java
PersonDao.java
PersonDaoBase.java
PersonDaoImpl.java
RentalService.java
RentalServiceBase.java
RentalServiceImpl.java
ejb/RentalService.java
ejb/RentalServiceBean.java
ejb/RentalServiceHome.java
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
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.
We'll learn how to model entity finders and have the Query Language automatically generated, the next section will go into more details.