How to manage Java code automatically generated by jOOQ & Flyway sample

TL;DR

2019-03-jooq-flyway-sample.png

Detail

At JJUG-CCC (Japan Java Users Group Cross Community Conference) in December 2018 Talking about O / R mapper and DB migration while comparing tools ) Was given the title.

Among the many O / R mappers and DB migration tools, jOOQ was recommended and Flyway was recommended. On the other hand, although it is a story that I hear from blogs and people on the street, it seems that there are many cases where they are used slightly incorrectly. ** What's particularly bad is that there are some cases where jOOQ automatically generated Java source code is versioned as a git commit target. You had better stop it. ** If multiple developers inadvertently add columns to the same table at the same time, a large amount of Java code automatically generated by jOOQ will always cause a conflict.

jOOQ reads the structure of the specified DB schema accurately and incomparably, and automatically generates Java code that makes O / R mapping easier. ** This means that it is the DB schema definition, that is, the so-called DDL statements such as CREATE TABLE and ALTER TABLE, that should be versioned accurately and unmatched. Flyway makes that possible. ** **

Since jOOQ automatically generates Java code, I want to specify the output destination under src / main / java, but that is the pitfall. If you are using gradle, you can compile by specifying the location of the source code after specifying the output destination of the automatically generated code of jOOQ under build /. There is no problem on IntelliJ IDEA. However, I don't know about the ancient weapon called Eclipse.

Try running the sample code

Just prepare a PC with git, JDK8 or above, docker and execute the following command.

git clone [email protected]:nabedge/jooq-flyway-spboot-sample.git
cd jooq-flyway-spboot-sample
sh setup.sh
sh ./gradlew run -p pj-web
Http in browser://localhost:Open 8080

Try importing the sample code into IntelliJ IDEA as a project

  1. Complete the steps above to run sh setup.sh
  2. File -> New -> Project from existing sources
  3. Open by specifying build.gradle directly under the jooq-flyway-spboot-sample directory
  4. Check use auto import and press the OK button
  5. Click SampleApplication.java in the pj-web project to launch it
  6. Open http: // localhost: 8080 in your browser

Points on how to use Docker

Points on how to use Flyway

Points on how to use jOOQ

jOOQ and build.gradle

Let's explain a little more from around.

jooq {
    version = "${jooqVersion}"
    edition = 'OSS' // if you use oracle, you should pay :-)

    // the name "sample" -> task name "generateSampleJooqSchemaSource" . see below.
    sample (sourceSets.main) {
        jdbc {
            driver = "${jdbcDriver}"
            url = "${dbUrl}"
            user = "${dbUser}"
            password = "${dbPassword}"
        }
        generator {
            target {
                packageName = "${jooqDestPackage}"
                directory = "${jooqDestDir}"
            }
            strategy {
                name = 'com.example.db.jooq.generator.SamplePrefixGeneratorStrategy'
            }
            database() {
                name = 'org.jooq.meta.postgres.PostgresDatabase'
                inputSchema = "public"
            }
            generate() {
                daos = true
                immutablePojos = true
                pojosEqualsAndHashCode = true
            }
        }
    }
}

compileJava {
    dependsOn generateSampleJooqSchemaSource
    sourceSets.main.java.srcDirs(jooqDestDir)
}

Add a prefix / suffix to the class name of the code automatically generated by jOOQ

As I mentioned in Around 50 pages of actual O / R mapper and DB migration talked while comparing tools, jOOQ is the table name by default. Will create a Java class with exactly the same name as. ** Custom is highly recommended. ** Class name conflicts can lead to considerable stress during the coding process.

So this part of build.gradle above

            strategy {
                name = 'com.example.db.jooq.generator.SamplePrefixGeneratorStrategy'
            }

Becomes important. This class is the only class in the subproject called pj-db-custom-strategy It is implemented as /src/main/java/com/example/db/jooq/generator/SamplePrefixGeneratorStrategy.java).

import org.jooq.codegen.DefaultGeneratorStrategy;
import org.jooq.meta.Definition;

public class SamplePrefixGeneratorStrategy extends DefaultGeneratorStrategy {

    @Override
    public String getJavaClassName(final Definition definition, final Mode mode) {

        String name = super.getJavaClassName(definition, mode);

        switch (mode) {
            case POJO:
                return name + "Vo";
            case DEFAULT:
                return 'J' + name;
        }

        return name;
    }

As you can see, all the Java class prefixes automatically generated by jOOQ have "J". This will give you the actual DB access code (https://github.com/nabedge/jooq-flyway-spboot-sample/blob/master/pj-web/src/main/java/com/example/web/ In BookRepository.java)

        final JBook jBook = JBook.BOOK;
        final List<BookVo> selected = dslContext
                .select(
                        jBook.ISBN,
                        jBook.TITLE,
                        jBook.PUBLISH_DATE
                )
                .from(jBook)
                .orderBy(jBook.PUBLISH_DATE)
                .fetchInto(BookVo.class);
                // .fetchInto(Book.class); // or you can use original class directly !

        return selected
                .stream()
                .map(bookVo -> {
                    Book book = new Book();
                    book.setIsbn(bookVo.getIsbn());
                    book.setTitle(bookVo.getTitle());
                    book.setPublishDate(bookVo.getPublishDate().toLocalDate());
                    return book;
                })
                .collect(Collectors.toList());

in this way,

You will be able to avoid these name conflicts.

in conclusion

If you move the sample while reading each and import it into the IDE, you can see the point. Happy Hacking !


This article is reprinted by the author himself from his own blog. -> https://nabedge.mixer2.org/2019/03/jooq-flyway-sample.html

Recommended Posts

How to manage Java code automatically generated by jOOQ & Flyway sample
Minimum configuration sample to automatically release Lambda by Java with Code pipeline
Java sample code 02
Java sample code 03
Java sample code 04
Java sample code 01
Sample code to convert List to List <String> in Java Stream
[Java] How to get a request by HTTP communication
[Java] How to cut out a character string character by character
How to build Java development environment with VS Code
Sample code for log output by Java + SLF4J + Logback
How to Git manage Java EE projects in Eclipse
How to check CircleCI code and automatically deploy to Heroku
How to execute WebCamCapture sample of NyARToolkit for Java
Sample code to parse date and time with Java SimpleDateFormat
How to select a specified date by code in FSCalendar
[Java] How to use Map
How to lower java version
[Java] How to use Map
How to uninstall Java 8 (Mac)
How to write good code
Java --How to make JTable
Java parallelization code sample collection
How to use java Optional
How to minimize Java images
How to write java comments
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
How to set Java constants
How to use Java variables
How to convert Java radix
[Java] How to implement multithreading
[Java] How to use Optional ①
How to initialize Java array
[RSpec on Rails] How to write test code for beginners by beginners
About TestSize advocated by Google and how to realize TestSize by Java and Maven
How to automatically operate a screen created in Java on Windows
Sample code to call the Yahoo! Local Search API in Java
How to study Java Silver SE 8
How to use Java HttpClient (Get)
Studying Java # 6 (How to write blocks)
[Java] How to update Java on Windows
How to make a Java container
How to disassemble Java class files
How to use Java HttpClient (Post)
How to learn JAVA in 7 days
Java 9 new features and sample code
[Processing × Java] How to use variables
[Java] How to create a folder
How to decompile java class files
Arbitrary string creation code by Java
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to write Java variable declaration
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
How to name variables in Java
How to pass Oracle Java Silver