From: Vance K. <va...@us...> - 2006-02-08 10:20:10
|
User: vancek Date: 06/02/08 02:20:01 Added: andromda-ejb3/src/site/xdoc howto5.xml Log: initial revision Revision Changes Path 1.1 cartridges/andromda-ejb3/src/site/xdoc/howto5.xml Index: howto5.xml =================================================================== <?xml version="1.0" encoding="iso-8859-1"?> <document> <properties> <author email="va...@us...">Vance Karimi</author> <title>AndroMDA - EJB3 - HowTo Enumerations</title> </properties> <body> <section name="Enumerations"> <p> You might have noticed the <code>carType</code> attribute in the <code>Car</code> entity. It is is a <code>String</code> type while in fact it makes more sense to restrict the set of possible values for this attribute. This can be achieved using <i>type-safe enumerations</i>. This is the topic discussed on this page. </p> <p> Type-safe enumerations are modeled by means of a regular class, only this time you need to use the <![CDATA[<<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. </p> <p> In the EJB3 cartridge, enumerations are <i>NOT</i> persisted behind the scenes, unlike the Hibernate framework however we can still use these enumeration types for entity attributes. The <code>enum</code> declaration in the <code>CarType</code> class defines an <i>enum type</i> class. </p> <p> In the next picture we have replaced the type of the <code>type</code> attribute in the <code>Car</code> entity rom <code>datatype::String</code> to <code>org.andromda.test.howto5.a.CarType</code>. </p> <p> <img src="images/org/andromda/test/5/a/uml.gif"/> </p> <p> <ul> <li class="gen">Auto-generated source that does not need manual editing</li> <li class="impl">Auto-generated source that should be edited manually</li> <li class="changed">File that is affected by the modifications applied in this section</li> </ul> </p> <p> <ul> <li class="gen"><a class="changed" href="src/org/andromda/test/howto5/a/CarEmbeddable.java.txt"><code>CarEmbeddable.java</code></a></li> <li class="impl"><a class="changed" href="src/org/andromda/test/howto5/a/Car.java.txt"><code>Car.java</code></a></li> <li class="gen"><a class="changed" href="src/org/andromda/test/howto5/a/CarType.java.txt"><code>CarType.java</code></a></li> <li class="gen"><a href="src/org/andromda/test/howto5/a/PersonEmbeddable.java.txt"><code>PersonEmbeddable.java</code></a></li> <li class="impl"><a href="src/org/andromda/test/howto5/a/Person.java.txt"><code>Person.java</code></a></li> <li class="gen"><a href="src/org/andromda/test/ServiceLocator.java.txt"><code>ServiceLocator.java</code></a></li> <li class="gen"><a href="src/org/andromda/test/howto5/a/RentalServiceBean.java.txt"><code>RentalServiceBean.java</code></a></li> <li class="gen"><a href="src/org/andromda/test/howto5/a/RentalServiceRemote.java.txt"><code>RentalServiceRemote.java</code></a></li> <li class="gen"><a href="src/org/andromda/test/howto5/a/RentalServiceDelegate.java.txt"><code>RentalServiceDelegate.java</code></a></li> <li class="impl"><a href="src/org/andromda/test/howto5/a/RentalServiceBeanImpl.java.txt"><code>RentalServiceBeanImpl.java</code></a></li> <li class="gen"><a href="src/org/andromda/test/howto5/a/RentalServiceException.java.txt"><code>RentalServiceException.java</code></a></li> </ul> </p> <subsection name="Literals"> <p> Note that in this example the enumeration attributes have been specified using regular variables in Java notation, the <em>initial value</em> has been specified using capitals (actually you would do this only when you want to have the value different from the attribute name): <source><![CDATA[ sedan : String = SEDAN liftback : String = LIFTBACK stationWagon : String = STATION_WAGON ]]></source> The following is perfectly equivalent, and shorter in use, it specifies the attribute name using capitals and omits the initial value: <source><![CDATA[ SEDAN : String LIFTBACK : String STATION_WAGON : String ]]></source> </p> <p class="highlight"> 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. </p> </subsection> </section> <section name="Next"> <p> We'll learn how to model entity finders and have the Query Language automatically generated, the <a href="howto6.html">next</a> section will go into more details. </p> </section> </body> </document> |