Monday, September 26, 2011

Creating JPA entities using Spring Roo

In the previous post we saw how you can create a Java project using project command of Roo. In this post, you'll see that how you can create JPA entities using Roo.

Before we go about creating JPA entities, if you take a quick look at the pom.xml file of flight-app project, you'll see that it contains standard dependencies that you'll typically have in your Spring-based Java projects. The extra dependencies that you see are related to AspectJ and Spring Roo's annotation. You'll also notice that pom.xml configures Eclipse Maven plugin and Tomcat Maven Plugin.

Follow these steps to create a Flight JPA entity:
1. Setup a Hibernate as the persistence provider. Open command prompt and execute persistence setup command, as shown here:

... roo> persistence setup --provider HIBERNATE --database MYSQL
--databaseName myFlightAppDB

provider argument identifier a JPA provider (a couple of JPA providers are supported by Spring Roo)
database argument identifies the database that you are going to use for your application (a couple of databases are supported by Spring Roo)
databaseName argument identifies the name of the database

Executing persistence setup command creates a file which contains database configuration information (username, password, database name, database driver class). It also creates a persistence.xml file required by the JPA provider.

2. Create a persistence entity using entity command, as shown here:
..roo> entity --class ~.domain.Flight --table FLIGHT_TBL

The entity command shown above creates a class (representing a JPA entity) in com.sample.flightapp.domain package. The table argument identifies the table to which the Flight entity maps. You'll see couple of more files created with .aj extension....these AspectJ ITD files, which you should never modify.

The following code shows the class:

public class Flight {...}

As you can see, is annotated with couple of @Roo* annotations. These annotations trigger code generation in Spring Roo. The identifier definition, getter/setters for properties defined in class, the persistence related methods (create, merge, update, and so on), and the toString methods are all inside the AspectJ ITD files generated by Roo.

The following things to note:
- AspectJ ITD files are like any other Java file, with a slightly different syntax. You don't need to learn a new language to understand AspecJ ITDs.
- The method, constructor, and so on, definitions in AspectJ ITD files are compiled into the corresponding Java source file. In our case, declarations contained in Flight_Roo_Entity.aj file are compiled into Flight class. This means resulting bytecode is not dependent on Roo at all.
- AspectJ ITD files are managed by Spring Roo when you make modifications to Java source file. So, you should not change them.
- If you want to modify a declaration in AspectJ ITD file, you can simply use Push-in Refactoring (using your IDE) to push the declaration (which could be method, attribute or constructor) into the corresponding Java source file. After doing push-in refactoring, you can modify the code manually in Java source file.

In the next post we'll go a step further and scaffold a Spring Web MVC application.

No comments:

Post a Comment