JHipster and JOOQ? Access your pre-existing SQL model from your fancy JHipster app.

Inquiring developers in the Java space are looking into JHipster because it combines a great Angular / Bootstrap UI with the awesome and deep Spring Boot.

JHipster is particularly attractive if you are working on a greenfield project.  As you may know, JHipster workflow revolves around Yeoman generators; you use generators to create "entities", generally.  Each time you do this, you get a new JPA entity and some associated Angular code / HTML to support CRUD ops.  As I said, this is great for new projects where you are creating the model on the fly.

But what if you have an existing model?  Is reverse engineering JPA models really the answer?  Not in my opinion...

Above, you can see a view of a relational model from within IntelliJ.  Nevermind the details. In the IDE, this is something you can zoom into and reason about. Intellij / Java / SQL tooling is excellent.

Have you heard of JOOQ?  It is the database access library that Java has needed for a long, long time.  I am not saying that Hibernate and JPA are bad--I am just saying that you don't have to use them for everything.  I think of JPA as a mapping facility that makes it easy to do certain CRUD ops.

But, as the prolific Lukas Eder has said, "JPA is not a replacement for SQL".

JOOQ takes a completely different approach to Java database access: It is a "database first" approach.  With JOOQ, you generate data access code directly from your database, using a Maven plugin that you roll into your JHipster build.

The JOOQ code generator creates Java classes that effectively _bring SQL semantics into your Java code.  _The generated code makes it possible to do complex queries in Java, in a way that is strikingly similar to pure SQL, yet the Java code is typesafe.  This blew my mind when I finally figured out how it works.

So, can we use JHipster to access our pre-existing giant corporate SQL data model?

Yes.  You need to use a JOOQ code generator to generate a bunch of static classes from your DB model.  For JHipster, you also need some Spring beans that make it easy to get at the JOOQ-generated code from within your services and/or controllers. I have written a JOOQ code generator that does this for you.

The jooq-jhipster-generator depends on JOOQ's existing JavaGenerator -- it just adds the Spring-specific stuff.  The jooq-jhipster-generator has been published to Maven Central, so you are not required (but you can) to download/build the generator itself.  The code is on Maven Central, so you can just use it directly in your pom.xml.

In order to use jooq-jhipster-generator with JHipster, you need to follow the instructions on Github and then integrate your model into the existing JHipster/Liquibase workflow.  In my case, what I did was to write a giant SQL model (such a joy compared to JPA).  My SQL model extends the existing JHipster JPA model--so the two can live together just fine.

Once you have this in place, you can now build REST services that have direct access to the full relational model.

One cost of using this approach is that you need to be deliberate about where (or when) you incorporate your external (SQL) model into the JHipster/Liquibase workflow.

Known issues/limitations:

  • Your Liquibase migrations may depend on classpath resources -- if your build fails, try mvn resources:resources in order to make Liquibase see the various config files.
  • There is currently no automatic UI code generation associated with this facility.  You need to make your own client-side code.
    Any feedback on this approach/toolkit would be welcomed.