I have been using Spring Roo for last 6 months but till today I had not used the Hades add-on for Roo. Finally today, I decided to play with the Hades add-on for Spring Roo. Spring Roo is a RAD tool for developing spring based applications using the popular and proven technologies and standards like maven, junit, JPA, JSR 330 bean validation etc. Hades is a utility library to work with Data Access Objects implemented with Spring and JPA. If you are newbie to Spring Roo and Hades you can refer to my posts on Roo and Hades. Spring Roo follows Active Record Pattern where in all the data access related methods(CRUD, finders etc. ) are defined in the entity class rather than defining them in a s a separate DAO layer. This is not liked by many Java developers as some developers prefer a layered approach more than Active Record approach. There is a very popular jira issue (ROO-301)related to providing an option to choose between different data access patterns. Hades Roo addon provides an option to create repositories for your entities. Lets create a sample Roo application using Hades addon.
- Create a directory called hades-roo
- Open a command line shell and go to the hades-roo director and start Roo Shell.
- Create a project using roo project command
project --topLevelPackage com.shekhar.jl.hadesrooplay
- Setup persistence using persistence command
persistence setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
- Create a simple entity for which we want to create a hades repository
entity --class ~.domain.MyUser
field string --fieldName firstname --notNull
field string --fieldName lastname --notNull
- Next we need to install hades addon . To do that type the following osgi commands
osgi install --url http://hades.synyx.org/static/roo/repo/org/synyx/hades/org.synyx.hades.roo.addon/0.3.0.RELEASE/org.synyx.hades.roo.addon-0.3.0.RELEASE.jar
osgi start --url http://hades.synyx.org/static/roo/repo/org/synyx/hades/org.synyx.hades.roo.addon/0.3.0.RELEASE/org.synyx.hades.roo.addon-0.3.0.RELEASE.jar
- Now if you type help command you will see two more commands. These commands are hades install and hades repository. The hades install command will add the hades maven dependency to the project and creates a spring context file specific to hades called applicationContext-hades.xml. This context file contains the hades:dao-config element which specifies the package in which repositories should be searched. This is similar to component scanning where all the classes annotated with @Component annotation are instantiated by the IOC container.
- Type hades install command which will install the Hades in your application.
- Type hades repository command to create a new repository for an entity. You can use * if you want to create repositories for all entities.
hades repository --entity *