ActiveJDBC Presentation At Austin Groovy and Grails Group

I gave a presentation on ActiveJDBC last week at the Austin Groovy and Grails Group (@AustinGGUG on Twitter).

I gave two presentations before the main one.

I talked about the validation annotations I made for POGOs.

I talked about making a Gradle app that you can run from the Groovy shell, since that was how I was going to interface with ActiveJDBC.

The “main event” was the ActiveJDBC talk. Many of the people there knew about ORMs, either through Hibernate or GORM. I spent some time basically walking though some of the pages on the ActiveJDBC website to show the capabilities of ActiveJDBC. Some of the hightlights were the Getting Started page,  getters and settersrelationships, transactions, validators, optimistic locking, and some of the ways ActiveJDBC adheres to the ActiveRecord pattern, and the ways you can override the defaults.

All you need to do to get started is to extend the ActiveJDBC Model class, and you are good to go:

This magic works because of instrumentation, which honestly I had not heard of before I tried to get it to work with Groovy. My understanding is that instrumentation adds byte code to a Java class after it has been compiled. I think it is mostly used by profiling tools.

ActiveJDBC gives directions on how to do this with Ant and Maven. I had to do some digging to get it to work with Gradle. I was able to do it with help from some code used by the Griffon ActiveJDBC plugin and some code in Griffon itself.

I added a method called addURLIfNotPresent, which adds files to the classpath of a classloader:

This is called at the end of the compileGroovy step, thanks to the doLast() method:

I made a Grails app that had a few tables, and I had a Gradle app that used ActiveJDBC that pointed to the same database. I would make changes in one, and view them in the other, and go back and forth.

I also covered using Groovy metaprogramming to make ActiveJDBC more idiomatic in Groovy. ActiveJDBC does not provide getters and setters out of the box.

Here is how you would get the “name” field from a record in the People table:

Here is how you would set the name field:

The docs state you could write your own setter like this:

I showed how to use Groovy metaprogramming to do this automatically. I wrote something that will look at a model class, get a list of its fields, make getters and setters, and also make extra fields for particular data types (findBy$FIELD for varchar, findWhere$FIELDGreaterThan, findWhere$FIELDLessThan, findWhere$FIELDBetween for numbers). There are a lot of print statements for the presentation audience.

Some more complex queries were still kind of awkward in ActiveJDBC. To find the books written by authors from Scotland, you need to do this:

I just get the feeling there should be a cleaner way to do that.

You can find the code on github.

Image from “Evangelia quattuor [Évangiles dits de Hurault] (1r-216r). Capitulare evangeliorum (217r-234r)”, a 9th century manuscript housed at the Bibliothèque nationale de France. Source / BnF; image assumed allowed under Fair Use.