Object Cases

DB Importer

HiberObjects

HiberObjects

HiberObjects is a UML tool for programmers, especially suited for Hibernate, JPA, GWT and Grails.

We want to use UML in an agile way, following these principles:

HiberObjects supports UML class diagrams, sequence diagrams and object diagrams. It can generate code for Java Persistence API (JPA) or Hibernate classes and DAO's, GWT services and unit tests.

The generated JPA classes can be used as the model in a Grails application.

PropertyChange support and DTO's for Google Web Toolkit (GWT) can also be generated.

Domain Classes

For example, a simple domain model for ordering airplane tickets can be designed in the class diagram below:

Flight classes

HiberObjects will then generate Java classes with JPA annotations or Hibernate mapping files. A helper class will also be generated, so just this diagram will generate all the code we need to create a database schema and read and write Java objects in the database.

Object Oriented Design of Behavior

The class diagram can be used to identify behavior. For instance, when a user orders a ticket, we need to create a Ticket object. The class diagram shows that the Ticket shall be connected it to 1 Customer and 1 Flight.

So, when a customer wants to order a Ticket, we need to find or create a Customer object, and let the customer select the desired Flight.

To select a Flight, the user will go through the following steps:

  1. Select an Airport to depart from.
  2. Select the destination Airport.
  3. Select one of the available Flights between those Airports.
The user can then review the price and place the order, and the system will create a Ticket.

We will implement this logic in a class OrderTicket. This object will gather the information from the user input and place the order when ready. Here is our first proposal for business methods:

OrderTicket class

1. Select an Airport to depart from

The listDepartures() method will return all available Airports, the user will select one of them and set the departure in the OrderTicket object.

2. Select the destination Airport

The user will view the destinations using listDestinations(). This method will return all Airports that can be reached from the selected departure. The user will select one of them and set the destination in the OrderTicket object.

3. Select one of the available Flights

Finally, the user will select a Flight with listFlights(), which returns all Flights between the selected departure and destination. Then, the user will set OrderTicket.flight to store the selection and call placeOrder() to make the payment.

Unit Testing

Now, we want to test our methods. The first method, listDepartures(), should return all Airport objects in the database.

To test OrderTicket, we will create another class OrderTicketTest that will prepare some objects, call methods in OrderTicket and verify the results. We set the stereotype of OrderTicketTest to <<Persistent Test>>. This will generate some code that makes it easy to test with a database.

OrderTicketTest class

We design some objects for OrderTicketTest in an object diagram. HiberObjects will create a method called setUp() in the OrderTicketTest class. This method will create the designed objects and store them in a database.

We implement testListDepartures() in Java to call listDepartures() and verify that it returns 2 Airport objects:

public void testListDepartures() throws Exception { List<Airport> departures = order.listDepartures(); HashSet<String> departureNames = new HashSet<String>(); for(Airport a : departures) departureNames.add(a.getCode()); String[] expected = { "OSL", "ARL" }; assertEquals("departures", new HashSet<String>(Arrays.asList(expected)), departureNames); }

By default, HiberObjects creates a hibernate.properties file that is configured with HSQLDB running in memory as follows:

hibernate.connection.url=jdbc:hsqldb:mem:mydatabase hibernate.hbm2ddl.auto=create-drop

This is perfect for unit testing, because we don't need to start a database server, and the database schema is created automatically at startup.

Making the Test Pass

We don't expect the test to succeed, we have not even implemented listDepartures() yet. Actually, the method will not even compile now.

So, let's implement it. Right-click on listDepartures() in the OrderTicket class and select Edit Query. This will open a dialog box with a suggested query in HQL (Hibernate Query Language):

from Airport a

This seems reasonable. Let's save the query and run the test in JUnit.

JUnit success

Success! The method works.

Conclusion

So what have we accomplished? We have implemented the first business method and verified that it works. We have actually created a database with 4 tables, inserted 4 rows, read 2 rows back and mapped everything to Java objects. That's not bad with this little effort!

We have also created a unit test that we can run periodically to ensure that this functionality doesn't break at a later time. We will automate this with JUnit. Automation is crucial, because it will take far too much resources to test everything manually all the time.

We have achieved this with very little Java code. All the domain objects are generated completely from a class diagram, and the test objects are designed in an object diagram. Not only is this very productive, but the diagrams are also useful for documentation and for discussions with others.

Admittedly, this was a very simple method to test, but it wouldn't be this easy to do it all in plain Java. The following pages go through more details, more unit testing and how to create DAO classes that handle the interaction with Hibernate.

Installation

Install HiberObjects from the update site: http://objectgeneration.com/update

DB Importer and HiberObjects generates code that needs a JPA implementation such as Hibernate. These libraries can be found in the hibernate plugins in the distribution. They are also available from Hibernate.org.

HiberObjects generates equals and hashCode methods that use Apache commons-lang.

If you generate GWT DTO's with HiberObjects, you need the following libraries: Dozer, Apache commons-logging, commons-beanutils, commons-collections and commons-lang. These libraries can also be downloaded as one bundle: runtime-libs.zip (8 MB)

HiberObjects Community Edition is open source licensed under the Eclipse Public License version 1.0. The source code is available in Mercurial here.