New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

InfoQ Article: Grails + EJB Domain Models Step-by-Step

| by Floyd Marinescu on Aug 22, 2006. Estimated reading time: 1 minute |
Grails could bring Ruby on Rails style productivity to the Java platform, built on the Groovy language and fully integrated with Java. In this tutorial, Jason Rudolph shows how to use Grails to quickly build a functional website around an existing EJB 3 entity bean domain model with very little code.  Read Grails + EJB Domain Models Step-by-Step.
As one impressive example of its enterprise integration abilities, Grails let's you quickly and easily build a web application backed by your existing EJB3 entity beans. But, it doesn't stop there. Grails gives your entity beans a hearty shot of steroids, but does so completely dynamically, without altering your EJB source code in any way. Grails Object Relational Mapping (GORM) is built on Hibernate 3 (but will eventually offer support for the Java Persistence API) and uses Groovy's Meta Object Protocol (MOP) to add all sorts of handy dynamic methods to your otherwise-static entity beans. And those methods are not only accessible from Grails and Groovy; your Java code can invoke those methods as well! We suddenly have all the enterprise-level capabilities of JEE/EJB3 and the benefits of RAD web application development!
The integration angle is quite important, as reuse of existing investments in Java is a commonly cited problem the Java community has been saying about using Ruby on Rails.  Grails brings a RAD framework bound together with our own dynamic language (Groovy) which is part of the Java platform.  Integrated stacks RAD stacks with a strong EJB focus is an area of innovation for a number of frameworks wuch as RIFE, Trails, JBoss SEAM (JSF+EJB), but Grails is the only one built around the Groovy dynamic language.  Could Grails become the Java community's answer to Ruby on Rails?

Rate this Article

Adoption Stage

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Wow EJB is now legacy? by j c

A legacy approach to using EJBs. Wow that speaks volumes.

And do it with another language. Ouch.

Floyd I cant figure if you think Java has a future or if you think we should all party on the VM like it was (a) 1999 (Corba container).

This is what frustrates me about the Java space right now. Everyone is so rattled by ruby and scripts and trying to wedge Java in a buzz trend that is so far removed from where Java is that its not even worth discussing Java in a "rails" context.

Or discussing rails in an EJB context.

If we have to port rails to the JVM to be happy then maybe we should just use the ruby interpreter and support its toolset and let IBM handle the legacy integration as they are so keen to do.

Its not interesting or all that relevant.

no, you missed the point by Floyd Marinescu

I think you missed the point - the article doesn't treat EJB as legacy, it's about developing a domain model in EJB and using Grails to quickly build a web app around it.

Considering strategies like Domain Driven Design, focusing on your domain model in Java and generating the UI around that is an emerging best practice that tools like Grails can enable.

Re: Wow EJB is now legacy? by Graeme Rocher

Where was it mentioned that EJB3 is legacy? Grails is all about embracing Java, the platform, language and tools.

Java has a huge future and it speaks volumes for the strength of the platform that Grails and its underlying frameworks (Spring, Hibernate, SiteMesh, Quartz etc.) exist.

In terms of porting Rails, well yes that is one option, but Rails (as with Ruby's runtime) has a very different object model and idioms for developing web applications. One that its contradictory to existing knowledge about specifications like Servlets, JSP and, indeed, EJB.

Grails is not a port of Rails and in fact is very different in many ways including how it is based on Java frameworks.

Re: no, you missed the point by j c

"built on the Groovy language" and "fully integrated with Java"
"built on the PL/SQL language" and "fully integrated with Java"
"built on Javascript language" and "fully integrated with Java"
"built on ActiveX" and "fully integrated with Java"
"Built on Flash" and "fully integrated with Java"

I didnt miss the point. The point was accessing EJBs with something other than Java. And if EJB 3 is a big plus in combination with another language then *something* in this picture just went legacy.

And something just went "integration strategy"

Thats how you know what legacy is and isnt.

Integration w/ Legacy Apps versus Developing w/ Complimentary Technologies by Jason Rudolph

If we were talking about using Grails to build a front-end to a VSAM file, I'd agree with you that we're talking about legacy integration; however, that's not the case.

There's a distinct difference between...

* Using a current technology to integrate with a pre-existing (legacy) application,


* Using a current technology (like Grails) in concert with another complimentary and current technology (like EJB) to build a new application.
The point was accessing EJBs with something other than Java. And if EJB 3 is a big plus in combination with another language then *something* in this picture just went legacy.

I respectfully disagree. We've long been dependent on another language to make our Java apps work. That language is XML, and XML does not make Java a legacy language. They're complimentary technologies.

If the goal is to build a web user interface for your EJBs (which are, by their very nature, business-tier components), chances are you're going to invoke some other (complimentary) technology for your web tier. Maybe it's just JSP. Maybe it's Struts or Spring MVC or Tapestry or Grails. The use of those complimentary technologies does not make EJB a legacy technology. It's instead a choice of using the most efficient tool for the job at hand. And for many applications, Grails offers the productivity gains to make it a strong contender to be that tool.

Re: Integration w/ Legacy Apps versus Developing w/ Complimentary Technolog by j c

OK Ill give you that, but you cant ignore the undercurrent of the Java space right now which is desperately trying to wedge the competition into its model out of fear of outright rejection.

What if the smalltalk guys had insisted that Java run in a smalltalk VM? What would people have thought of them? The smalltalk VM was way ahead of Java in the early days.

So now the Java scene wants to steal ruby and rails from the cradle and I say well if Java is so great then let Java solve its own problems without coopting the vibe of the scripting languages.

I think Java is trying to change before anyone acknowledges or speaks aloud of a greater failure of OO or more specifically Java projects to deliver.

Re: Integration w/ Legacy Apps versus Developing w/ Complimentary Technolog by Steven Devijver

I think Java is trying to change before
anyone acknowledges or speaks aloud of a greater failure of OO or more
specifically Java projects to deliver.

I read in a magazine a couple of years ago that "the Hummer is the greatest off-road vehicle we ever drove". And "with a Hummer you can get through any terrain except through narrow streets".

Java, Groovy and Ruby are great in many areas but none is great in all areas. Particularly, Java is not so great to write web handlers. Alternatively you could use Ruby and Rails or Grails if you want to reuse Java code. In fact, we've created Grails to augment the Java platform.

You can hate us for doing so and being successful. There's however no need to start a flame war.

relationship management by Jeff Payne

Any reason why the relationaship management code can't be generated too? It was a little anticlimactic to learn that the developer had to manually edit the generated groovy code just to handle the object relationaships. I'm assuming all that info is stored as annotations in the EJBs.

bug in employee list? by ma pel

I followed up this step-by-step and successfully run 'grails run-app'.
Everything looked great except this case:
I assigned 3 computers to an employee, for example, jane and I got
3 instances of the same 'jane' listed in the browser(IE6.0) when I click on
employeeBean list link. That seems a bug. How can I fix it?

Re: relationship management by Graeme Rocher

This is one the todo list:

Re: bug in employee list? by Jason Rudolph

Good catch. Thanks for your feedback.

There's apparently problem with the dynamic list method, which is used to fetch the list of employees. You can track the problem here:

Nice, but... by Randall Burt

This is a really great article and does a very nice job of showing off the features of Grails. However, I was sort of hoping for an article about integrating Grails and EJB. As it is, the author just illustrates the flexibility of the new EJB pojo implementation as well as the rapid development capabilities of Grails. What I'd like to see (and I believe is slated for a release later this year) is the ability to map those beans to/from ones already deployed on an existing EJB container instead of simply using Hibernate and copying the pojo implementations into the project. Maybe this is already possible using some tricks with the Spring configuration, but I don't know Spring well enough to say. In any case, thanks for a great article!

Re: Nice, but... by Pat Osterday


Your comment is exactly what I was thinking! I read the article and there's no mention of an app server or anything like that! Just copying the entity code and using that. While Grails looks cool, we've got a lot invested in the JEE resources. Guess we'll have to see what develops!

Re: Nice, but... by Piero Sartini

Yes, thats what I try to find out as well.

Article in pdf format? by Fred Janon

Would it be possible to the content of this article in pdf format, to be able to read it offline and print it nicely? Thanks.

having problems to run app with grails 1.0 RC2 & groovy 1.0 by Michael Brinkmann


I am stuck following your steps in a very early stage ("run-app" directly after "create-app"):
(I am running the grails / groovy-versions mentioned above with JDK 1.6.0_03 on Windows XP SP2)

> grails run-app

Welcome to Grails 1.0-RC2 -
Licensed under Apache Standard License 2.0
Grails home is set to: C:\Programme\Dev\grails-1.0-RC2

Base Directory: c:\Arbeit\Allgemein\Dev\Projekte\GrailsTest1
Environment set to development
Note: No plugin scripts found
Running script C:\Programme\Dev\grails-1.0-RC2\scripts\RunApp.groovy
groovy.lang.MissingPropertyException: No such property: war for class: java.lang.String
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(

Do you have any hints for me?

Kind regards

Re: having problems to run app with grails 1.0 RC2 & groovy 1.0 by Michael Brinkmann

Hello again,

FYI: just fixed the error by changing the line
in the generated file "grails-app/conf/Config.groovy".

Kind regards

Issue GRAILS-2042 by Venky VB

When I try out the tutorial I get an error which is logged in the JIRA (GRAILS-2042). However there seems to be no action on this issue ? Any workaround available for this ? (am using Grails 1.0 RC3)
- Venky

Scrollable results? by Siddharth Bala Ravi


Thanks for the article - it's awesome.
I was wondering what needs to be done to make the following search scrollable...being very new to grails, i'm having a real hard time figuring it out...

generate-controller failure with Grails 1.0.4 by Sherry Shen

I went through Step 1 to 3 with following changes
in ejb3_grails dir
I am not sure about hibernate.cfg.xml location.

At Step 4 - Generate the Scaffolding, I got an error.
% grails generate-controller
No domain class found for name com.jasonroudolph.ejb3example.entity.EmployeeBean.
Please try again and enter a valid domain class name

How to move forward?
Thanks for the help!

Re: Nice, but... by Gazi Mushfiqur Rahman

The thing I actually need is using the existing EJB 3 domain entities within a GORM entity. For example I already have a User entity bean developed using EJB 3 annotations. I am going develop the new entities in GORM. So, how can I reference/use the User entity from within the GORM entities?

Re: Nice, but... by Rajakumar Sennayan

I have J2EE EJB2 Session bean and EntityBean application. I would like to migrate EJB2 Session beans and Entity Beans to another working Grails application. Kindly let me know the migration steps

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

22 Discuss

Login to InfoQ to interact with what matters most to you.

Recover your password...


Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.


More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.


Stay up-to-date

Set up your notifications and dont miss out on content that matters to you