JavaGen UML-to-EJB TutorialsThis page contains two tutorials using code examples that ship with XDoclet and JBoss, respectively. These examples demonstrate how various UML classes, attributes and associations are transformed into EJB CMP 2.0 entity beans with persistent properties and relationships. A recommended prerequisite is the JAMing EJBs tutorial, which is an introduction to using JAM to build and test EJBs. Note: A few changes were made to the original code to demonstrate features of JavaGen. XDoclet CMR EJB ExampleThis code snippet ships with the XDoclet distribution and is meant
to demonstrate how to add XDoclet attributes to create CMRs (container managed
relationships) for your EJBs.
The advantage of using a code generator like JavaGen is that most of these
XDoclet attributes can be inferred from the UML relationships. JavaGen looks for
stereotypes in the UML to determine what type of code to generate for a given design
element.
Stereotypes are a simple UML extension mechanism, three of which are required for this
example: <<cmp>>, <<cmp-field>>, and <<cmp-pk>>.
The <<cmp>> stereotype tells JavaGen to generate a CMP EJB (Container Managed
Persistence entity bean mapped to a database table), the <<cmp-field>>
stereotype generates a persistent property (mapped to a database column) and the
<<cmp-field>>
stereotype generates a primary key property (mapped to a primary key column).
JavaGen also looks for UML associations between <<cmp>> marked classes
and generates corresponding relationship navigation code. These relations can be
one-to-one, one-to-many, many-to-many, both unidirectional and
bi-directional, as well as self-referencing (like the Ultimately, the CMP EJBs do all kinds of SQL joins, selects, updates, inserts, cascading deletes and foreign key-primary key transactions, of which you can remain blissfully unaware! Diagram 1 shows the high level view of the UML Class Diagram as rendered in Poseidon.
Rather than try to explain UML in detail here, we suggest you load the UML file into Poseidon (available from www.gentleware.com) and compare the design details with the generated code. Notice how the names at either end of relations are utilized in the generated code. Locate instances of <<cmp>>, <<cmp-field>> and <<cmp-pk>> stereotypes and notice how they effect the code. The following steps show you how to build and deploy this example. The source distribution contains the generated code and we recommend you get this working before you regenerate; correspondingly the code generation step is explained last.
This exercise may seem trivial, but reflect upon what you have done. From a simple UML diagram, you've created an entity tier and a database tier (see architecture), including all the O-R (object to relational) mapping functionality and database definitions. (Still think this is trivial? I consulted at a Swiss bank that spent $200 million to do basically the same thing!) You can use the generated SQL (located under
<appender name="SQL" class="org.apache.log4j.FileAppender">
<param name="Append" value="false"/>
<param name="Threshold" value="DEBUG"/>
<param name="File" value="${jboss.server.home.dir}/log/sql.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<category name="org.jboss.ejb.plugins.cmp">
<appender-ref ref="SQL"/>
<priority value="DEBUG"/>
</category>
Restart JBoss and redeploy/undeploy your EJB and look in the resulting log file Gangsters ExampleThis example comes with the JBoss documentation. It is slightly more complex, illustrating an aggregation relation and a richer set of properties. It also comes with unit tests for verifying the code works as expected. Diagram 2 shows the Gangsters UML Class Diagram as rendered in Poseidon 1.6.
This tutorial is terser than the previous example. If you don't understand something, refer to the first tutorial for more explanation.
Only the GangsterITest class has any valid tests, the other integration test classes have a single generated test that fails. You can try your hand at EJB programming by writing some unit tests in these classes. Congratulations, you've graduated to J2EE gurudom. To see a more advanced EJB example,
look at the |
| Copyright 2003-2007 Outsource Cafe, Inc. All rights reserved. - Generated by JavaGen. |
