HiberObjects

Download

Order

Forum

Vote for this plugin

Grails

HiberObjects can generate JPA classes for Grails from UML class diagrams. This results in a combination of UML domain modeling and agile web development; The model is designed in class diagrams, the view is Groovy Server Pages (similar to JSP), and the controllers are Groovy classes.

So why should you use HiberObjects and Java instead of Groovy for the model? The main reason is if you want to use UML class diagrams to model your domain. Or you could import a database into a class model and quickly create an application with Grails. You can also use the domain classes for other Java code, such as GWT. And you get the object oriented unit testing capabilities of HiberObjects.

Create a Grails Project

Please follow the Installation Guide for Grails.

Open a shell window and go to your workspace directory. Create a project with the name grailstest: grails create-app grailstest

Change directory to the new project: cd grailstest

Start the newly created application: grails run-app

Open a browser at http://localhost:8080/grailstest/

You should see an empty Grails page:

Create a HiberObjects Project

First, import the project into Eclipse: Select File - Import - Existing Projects into Workspace, Select the root directory <workspace>/grailstest and push Finish.

Now, you can add HiberObjects capabilities to it: Right-click on the project and select Properties.

On the HiberObjects page, set the following options:

In the Persistence page, set the following preferences:

Then, push OK.

Setting GRAILS_HOME in Eclipse

You need to define the classpath variable GRAILS_HOME.

Select the project and Project Properties, Java Build Path, Libraries. Select an entry with GRAILS_HOME, then push Edit:

Push Variable, and then New:

Type Name: GRAILS_HOME and browse to the path for your Grails installation:

Push OK, and let Eclipse rebuild your project. Then, all compiler errors should disappear.

Configure Grails to use Java Persistence API

Open grails-app/conf/DataSource.groovy and add configClass as follows:

dataSource { pooled = false driverClassName = "org.hsqldb.jdbcDriver" username = "sa" password = "" configClass = org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration }

Create Model

Open the Project Explorer view. Right-click on the HiberObjects icon and select New - Package and type the name "com.objectgen.grailstest.model". This will create a new package in the HiberObjects node.

Then, right-click on the package, select New Class Diagram and type a name. A new class diagram will be opened.

Create some classes and set their stereotype to <<Persistent>>. Then, save the diagram to generate the Java code.

HiberObjects should now have generated the file grails-app/conf/hibernate.cfg.xml:

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <mapping package="com.objectgen.grailstest.model" /> <mapping class="com.objectgen.grailstest.model.Customer" /> <mapping class="com.objectgen.grailstest.model.Airport" /> <mapping class="com.objectgen.grailstest.model.Flight" /> <mapping class="com.objectgen.grailstest.model.Ticket" /> </session-factory> </hibernate-configuration>

Create Views and Controllers

Create a controller for each domain class from the command line:

grails generate-controller com.objectgen.grailstest.model.Airport grails generate-controller com.objectgen.grailstest.model.Flight grails generate-controller com.objectgen.grailstest.model.Ticket grails generate-controller com.objectgen.grailstest.model.Customer

Create a view for each domain class:

grails generate-views com.objectgen.grailstest.model.Airport grails generate-views com.objectgen.grailstest.model.Flight grails generate-views com.objectgen.grailstest.model.Ticket grails generate-views com.objectgen.grailstest.model.Customer

Refresh the Eclipse project to see the generated files:

Test

Now, test your application again: grails run-app

And open a browser at http://localhost:8080/grailstest/.

That looks nice! Let's try it out.

Clicking on AirportController opens this page:

New Airport:

I create one more Airport, and then a Flight (click Home, FlightController and New Flight):

The text in the drop down menus are not nice, but that should be easy to fix by implementing the toString() method in the Airport class. (Remember to remove @generated.)

New Customer:

What's this? A NullPointerException? After looking around a bit, I find the error in the customer view (grails-app/views/customer/create.gsp):
<td valign='top' class='value ${hasErrors(bean:customer,field:'name','errors')}'> java.lang.NullPointerException </td>
Something must have gone wrong when I created the view.

But the other views are fine. Let's try to use something from airport/create.gsp: <input type='text' name='name' id='name' value="${fieldValue(bean:customer,field:'name')}" />

I try to create a new customer, and now the page looks ok:

I get an ugly exception when I try to Create, but after restarting grails run-app, the page looks fine.

But it still doesn't work! The Customer name is empty after creation. After searching around and comparing with other classes that work, I finally find the reason: Customer getName() and Customer setName() are not public. That's because I forgot the + sign before the name attribute in the class diagram (+ means public, - means private, # means protected and empty means package protected.) I fix this and save the class diagram:

Then, I create a new controller and view for Customer:

grails generate-controller com.objectgen.grailstest.model.Customer grails generate-views com.objectgen.grailstest.model.Customer

Restart Grails: grails run-app

I test to create a customer again, and now it works fine.

Persistent Data

Whenever I restart grails or compile a Java class, all data is lost. This is because the database is configured to run with HSQLDB in memory. Open grails-app/conf/Datasource.groovy and modify the development datasource:

environments { development { dataSource { dbCreate = "update" // one of 'create', 'create-drop','update' url = "jdbc:hsqldb:file:devDb" } }

References

The Grails project documentation can be found at grails.org.

Jason Rudolph has written a tutorial about Grails and EJB entity beans that explains more on how to customize the Grails application and about the dynamic methods that Grails will give you.

Here is an Eclipse plugin for Groovy. It gives compilation, syntax highlighting and code completion for Groovy. (The current version can be slow some times.) It can be installed from the update site http://dist.codehaus.org/groovy/distributions/update/

The Groovy site has more documentation of the Groovy language and libraries.