Developer: Open Source
 Oracle Database XE
grails, groovy, java, All

Grails on Oracle for Java Developers

by Tug Grall

Learn how to quickly build a Grails application on Oracle Database XE, and then deploy it to Oracle Application Server 10g.

Published May 2006

As you may have noticed, Oracle recently published an article by Richard Monson-Haefel about Ruby on Rails on Oracle. This article introduces the Ruby on Rails framework and explains how to use it to access an Oracle database ( Oracle Database 10g Express Edition (XE), to be precise)

If you are not familiar with Ruby on Rails, it is important to note that it has nothing to do with Java; it is a Ruby-based framework. Ruby on Rails is really interesting, powerful, and so on... but as a Java developer, I would like to do the same using Java (or equivalent) leveraging the investment that I have in J2EE. I also want to be able to deploy and manage applications that are developed this way using tools such as Oracle Enterprise Manager Application Server Control.

The "coding by convention" paradigm that is the driver of Ruby on Rails has been leveraged to develop a new framework: Grails. Grails uses Groovy as the underlying language, so it runs on a JVM and can leverage any existing Java API.

If you are a Java developer, you will find it very interesting to use this framework to accelerate the development of Web applications. If you are not yet a Java developer but need to develop and deploy Web applications faster, Grails is also a very good tool.

Here I will use the same application/database schema used by Richard in his article to develop my first Grails application. Follow along!

What is Groovy? What is Grails?

Groovy is a dynamic language that leverages features from other languages such as Ruby, Jython, and Smalltalk. Groovy runs on a Java VM and makes available any existing Java objects (so all the APIs) to Groovy. Groovy is currently under standardization with the JSR-241; you can learn more about Groovy on the Groovy site and its project leader's (Guillaume Laforge) blog.

Grails is to Groovy what Ruby on Rails is to Ruby. (Originally named "Groovy On Rails" , this name has been dropped in favor of "Grails" to avoid confusion/competition.) Like Ruby on Rails, Grails is designed to create CRUD ( Create Read Update Delete) Web applications.  You can learn more about Grail on the Grails site and its project leader's (Graeme Rocher) blog.

Next, let's dive into the sample application.

Example: The Product Catalog

Step 1: Set up the Oracle database

If you have not set up the schema and table from Richard's article you just need to create the following objects:

      id NUMBER(10) NOT NULL,
      title VARCHAR2(60),
      issue NUMBER(4),
      publisher VARCHAR2(60),
      PRIMARY KEY (id)

Based on that article I have created this table in the ruby schema.

Step 2: Install Grails

Grails installation is straightforward and explained in the Installation guide. Basically:

  • Download the binaries (I used Grails 0.2)
  • Setup the different environment variable (GRAILS_HOME, JAVA_HOME, PATH), I used Java 5.

You're done!

Step 3: Create the Web Application

Now that you have installed the product, the next step is to create the application itself.

Create the application. The create-app command creates the full project, with a template with placeholders for the different components of your application such as configuration, MVC, library, and much more. Enter the following command in your command line interface:

> grails create-app
    [input] Enter application name:

As you will see, Grails uses Ant intensively. The create-app command will ask you for an application name; enter for example comics_catalog.

The created application contains a list of directories, allowing you to start to build the application using Groovy, Grails, and other Web components.

Add the Business Logic and Model: Domain Classes. One of the biggest differences between Grails and Ruby on Rails is the fact that with the former, the main component of your application development is not the table but rather the domain class.  The domain class is the core of the business application; it contains the state and behavior of your application.

So the next step is to create a domain class for the comics. To do that you just need to go in the home directory of your project—e.g., cd comics_catalog—and run the create-domain-class.

> cd comics_catalog
> grails create-domain-class
    [input] Enter domain class name:

When the command asks you to enter the class name, enter "comics." Grails will not use the same naming convention that Ruby on Rails does, so you need to use the same name for the class and the table you want to map your object on. The persistence layer is made using GROM (Grails Object Relational Mapping), which leverages JBoss Hibernate.

Note: In our case, we are leveraging an existing database object and creating the domain class on top of it. Usually, Grails uses a different approach where everything is driven by the application—you create the domain class first and then Grails will create the different database objects.

The Comics class does not have any information related to the mapping itself, so you have to create the different attributes in the domain class. This is where you start to use Groovy. The domain class is located in the following location:


Note that by default Grails creates the class with two attributes: id and version. Keep them in place and add title, issue, and publisher. 

class Comics { 
    @Property Long id
    @Property Long version

    // new properties for the Comics class
    @Property String title
    @Property Long issue
    @Property String publisher

    String toString() { "${} :  $id" }

Now you're all set and ready to run the magic command that will create the different screens and flow.

Create the different screens from the domain class. You can now run the generate-all command to create all the different screens.

> grails generate-all
    [input] Enter domain class name:

This command creates the different Views and Controllers; you can take a look to the directories:


Configure database access. Next you have to configure the application to use the Oracle database and schema. Grails uses a configuration file for data source:


Edit this file to connect to your Oracle database.

class ApplicationDataSource {
   @Property boolean pooled = true
   @Property String dbCreate = "update" // one of 'create', 'create-drop','update'
   @Property String url = "jdbc:oracle:thin:@localhost:1521:XE"
   @Property String driverClassName = "oracle.jdbc.OracleDriver"
   @Property String username = "ruby"
   @Property String password = "ruby"

Nothing special here, just properties such as URL, DriverClassName, username, and password. The one that is interesting is dbCreate, which allows you to configure the behavior on the schema to create (or not) objects. In our sample the table exists, so you want to reuse the object—but first you want to confirm you have all the mandatory objects (columns too), so select update.

The next thing to do is to add the Oracle JDBC driver to the application to make it available. To do that you just need to copy the JDBC driver into the lib directory of your application. In my case I am using Oracle Database XE, so I copy the file from the following location:




Step 4: Run the Application

Grails provides a way to run the application in standalone mode (run-app). This command starts a Web container (based on Jetty) with the application deployed.

> grails run-app

Note: Jetty will start on port 8080; in order to start on a different port (like 9090) use:

grails -Dserver.port=9090 run-app

You can now access the application using the following URL:


Your browser should show the list of comics from the Comics table.

Figure 1

You can create a new entry by clicking on the New Comics tab and view/edit/delete existing records by clicking on the Show link.

Figure 2

As you can see, creating an application is really easy. The next step is to deploy the application to your application server.

Step 5: Deploy the Application

Grails provides a command to package the application as a WAR ready to be deployed, so in the root directory of your project you can run the following command:

> grails war

When you run this command you end up with a WAR with the name of your application located in the root of your project (in our case, comics_catalog.war).

If you take a look to this WAR you'll see that it is quite big—more than 10MB—because all the libraries are included in the Lib directory of the Web application. You can see the exact structure of the WAR in the ./tmp (./comics_catalog/tmp/war) directory of the application.

You can deploy the application as it is to Oracle Application Server 10g, but to avoid the issue with the class loader, you should configure the Web application to load the local classes first. You can do that during deployment with the class loader configuration screen:

Figure 3

You can also save this configuration in a deployment plan to facilitate later deployment.

When the deployment is done you can access the application using the Oracle Application Server host and port, something like:


You can now administer and monitor the application like any other J2EE application deployed to Oracle Application 10g.

Note that there are a couple other deployment options:

  • I personally do not like the idea of shipping all the JAR files in the WAR file, so instead you can use the Oracle Application Server Shared Libraries to create a Grails library by uploading and configuring all the JARs and package the WAR without all these libraries.
  • You can also configure Hibernate/Spring to use a standard define Data source and use the JNDI name to lookup the connections.


Grails and Ruby on Rails are really interesting frameworks that allow developers to quickly create Web applications that access a relational database, and especially Oracle Database.

Grails is quite new (release 0.2), but the documentation is really nice and complete. I encourage all interested developers to use it and provide feedback to the development team.

Tug Grall is a product manager on the Oracle Application Server Containers for J2EE team.