As an extension to last year’s Grails + EJB3 tutorial on, I had the pleasure today of presenting an updated demo on this topic, showing just how easy it is to pimp out your EJB3 entity beans to include all the slick dynamic goodness we’ve come to know and love from traditional Grails domain classes.

Groovy Duke - Pimp Extraordinaire

But as much as I enjoy infusing boring, statically-typed EJB3 POJOs with GORM-powered productivity, I’m recently finding myself more excited about the ability to implement your Grails domain classes with any technology you like, and then simply expecting it all to just work. The result? Implementation-agnostic domain classes and the flexibility to use whichever technology is best suited for the task at hand. We’re free to choose…

Traditional Grails domain classes

class Castle {
    String name
    String country

    static belongsTo = [Knight]

    Knight knight

EJB3 entity beans (i.e., JPA-annotated POJOs)

(Some of the noise has been omitted here for sanity’s sake. Please feel free to download the sample code for the full experience.)

public class Knight implements {

  private long id;
  private String name;
  private long numDragonsSlain;
  private Set swords = new HashSet(0);

  public Knight() {

  // ...
  @Column(name="knight_id", nullable=false)
  public long getId() {

  public void setId(long id) { = id;

  // ...

  @OneToMany(fetch=FetchType.LAZY, mappedBy="knight")
  public Set getSwords() {
      return this.swords;

  public void setSwords(Set swords) {
      this.swords = swords;

## JPA-annotated Groovy classes (i.e., POGOs)

public class Sword implements {

    @Column(name="serial_number", nullable=false, length=12)
    String serialNumber

    Knight knight

    @Column(name="manufacturer", nullable=false)
    String manufacturer
## Agnosticism runneth amuck And regardless of the technology we choose for one domain class, we're *still* free to implement the other classes however we like. The relationships remain intact, and GORM makes sure that it all just works. Using the classes above, we can walk the relationships navigating from one implementation technology ... to another ... to yet another still.
groovy> def c = Castle.get(1)
groovy> println "In the castle of ${}, you can see ${} wield his mighty collection of ${c.knight.swords.size()} swords."
groovy> go
In the castle of Camelot, you can see King Arthur wield his mighty collection of 7 swords.
  Cool! Use the implementation most appropriate for your needs, and let the framework figure out the rest. -- [Download the slides]( [Download the sample code](