Integrating jOOQ with Grails Featuring the UWS-jOOQ Plugin
Introduction
Grails is a web framework aimed to boost development productivity. One of the main features is domain centric database schema generation. Applications built with Grails are able to update existing schema just before they start. To do this, Grails is using built-in domain mappers or migrations in more advanced cases. The goal of the UWS-jOOQ Grails-plugin is to integrate jOOQ into the existing Grails lifecycle in order to leverage features of jOOQ without compromising the ones provided by Grails.
This article is part of a series brought to you by the Germany based jOOQ integration partner UWS Software Service (UWS). UWS is specialised in custom software development, application modernisation and outsourcing with a distinct focus on the Java Enterprise ecosystem.
Why should I use jOOQ with Grails?
In enterprise applications we often face issues with Hibernate performance, lack of support of some statements or just too much hassle caused by the Hibernate model. Hibernate’s Query Language HQL often is not sophisticated enough to cope with some requirements. This forces us to use plain SQL, which is not bad and helps solving specific business problems. However in big projects where a larger group of people is involved and a product continuously evolves, type-safety is very precious but is thrown away when using plain SQL. That’s the moment where the jOOQ framework excels and the UWS-jOOQ Grails-Plugin comes into the game.
How can I Integrate jOOQ into Grails?
We tried to provide a simple integration of jOOQ into Grails using Grails built-in dependency resolution. Just add the following line to the plugins section of your BuildConfig.groovy:
compile ':uws-jooq:0.1.1'
Add plugin configuration to your Config.groovy:
jooq.dataSource = ['dataSource'] jooq.xmlConfigDir = "src/resources/jooq/" jooq.generatedClassOutputDirectory = 'src/java' jooq.generatedClassPackageName.dataSource = 'ie.uws.example'
As in this example the plugin allows to configure the datasources to be used and also some of the key-paths. Eventually you want to also take a look at a customized version of the Config.groovy in our sample integration project.
Next jOOQ needs an xml configuration file, which can be generated by the plugin using the following command. The plugin will use your existing datasources and their configuration to generate the jOOQ configuration:
grails jooq-generate-config
Now all the configuration is ready and it is time to get to one of the main features of jOOQ which is type-safe SQL. With the following command jOOQ will generate Java-classes which you want to use when writing SQL as they will give you compiler-based autocompletion:
grails jooq-init
Now that everything is in place, let’s say you would like to insert a new record into your database via jOOQ in one of your Controllers. It’s as simple as that:
class ExampleController { JooqService jooqService def insert() { DSLContext dsl = jooqService.dataSource BookRecord record = dsl.newRecord(Tables.BOOK) record.author = "John" record.name = "Uws" record.version = 1 record.store() } }
How does the integration of jOOQ with Grails work under the hood?
In the example above you noticed the JooqService which is dependency-injected by Grails. The JooqService is your entrypoint when it comes to using jOOQ within Grails as it is able to pick your datasource and provide the jOOQ DSL context for you. When you have multiple datasources it also allows you to select a different datasource just by providing the name of it:
DSLContext dsl = jooqService.dataSource_custom
Note that autocompletion won’t tell you about the existence of a dataSource_custom
field but JooqService will handle that for you.
In version 0.1 we added JooqService but DSLContext have to generated based on your databases schema. So it is important to execute jooq-init
command every time you change your Grails domain model since this command compiles your code and executes all migrations so that latest Java-classes are generated on the latest database-schema. Thanks to this approach it is possible to generate structures even from an in-memory H2 database which will not be available right after the application will be shut down.
Best-practices for using jOOQ with Grails
Integrate legacy databases
You might face the situation where you have to connect to some legacy database using the Grails framework. It’s doable, for sure, but you have to create the right Hibernate mapping first or – with some luck – let Hibernate generate the right one for you. With this plugin you can just let jOOQ generate its Java-classes and you’re ready to communicate with the database using a fully type-safe DSL.
Let a database schema change break your code
It’s one of the most precious gifts when you know that something will break early. With using jOOQ in Grails it will happen during compilation time. When you are executing jooq-init
, your application is compiled and the Java-classes are regenerated using the latest database schema. If the generated classes change, you will be notified that your code is not able to compile anymore. You can fix your SQL statements and ensure that your application won’t break during runtime.
Keep generated classes in your version control system
We recommend you to check in jOOQs generated classes into your VCS along with the rest of your applications source code. When you are using jOOQ classes to communicate with the database it’s mandatory for compilation to have those classes already defined. Do you use a different approach? Please let us know!
Roadmap
We’re planning to simplify integration even more and attach jooq-init
into regular grails compilation process. Also we’d like to make our plugin harder to misuse (see jooqService section) and add the possibility to use jOOQ not only in services or controllers but also in plain Java classes.
Contribution to UWS-jOOQ Grails-Plugin
This software is distributed under the Apache License, Version 2.0. We want to keep this software free and provide services for people
who integrate jOOQ and Grails. If you’re interested in this project feel free to submit issues or pull requests to the project’s git repository.
Further reading
The following links provide additional information about the UWS-jOOQ Grails-Plugin :
- UWS-jOOQ Grails-Plugin
- UWS-jOOQ Grails-Plugin: Documentation
- UWS-jOOQ Grails-Plugin: Usage examples
- UWS-jOOQ Grails-Plugin: Code repository
- UWS-jOOQ Grails-Plugin: Exemplary integration project
- Software Development with the Grails Framework
General information about UWS or jOOQ can be found here:
Reference: | Integrating jOOQ with Grails Featuring the UWS-jOOQ Plugin from our JCG partner Lukas Eder at the JAVA, SQL, AND JOOQ blog. |