Using DBDeploy in Gradle with hibernate as the ORM - java

I am looking to use a versioned database migrations tool like DBdeploy in a java project which uses Hibernate as the ORM and uses Gradle as the build system. I am unable to lookup any documentation on running DBDeploy on Gradle. Also is there a Hibernate based migrations manager, which can understand schema changes by looking at changes in Hibernate classes/definitions?

Since dbdeploy has an Ant task, it should be straightforward to use from Gradle. See Using Ant Tasks in the Gradle User Guide.

I am unable to lookup any documentation on running DBDeploy on Gradle.
Almost any build system can likely call either the ant task or the command-line interface for dbdeploy.
Also is there a Hibernate based migrations manager, which can understand schema changes by looking at changes in Hibernate classes/definitions?
You might want to look at Liquibase. I haven't actually used it (I've used dbdeploy), but it has hibernate integration that looks like what you want.

this post shows how to use DBDeploy in Gradle:
http://blog.codeborne.com/2012/09/using-dbdeploy-in-gradle.html

Related

How to use Spring boot, JOOQ and Flyway together?

So, let's consider a general Spring boot application, which uses JOOQ for database database access, and Flyway for database migration. The project uses gradle for dependency management.
I want the following things:
Run my application in docker. So, I want to use only in environment variables (https://12factor.net/config). Hence, I don't know, how to configure both spring boot application properties (database login and password) and gradle JOOQ plugin database login and password.
Automatic generation JOOQ classes. Flyway migration runs, when an application has started. But JOOQ generates code in gradle build task. So, we see wrong order of tasks execution.
I have a very similar setup, but resorted to manual action to generate Jooq classes.
I need them for development, so it makes no sense for me to delay the generation till target environment.
I decided to run a local dB for development purposes.
I run it in docker, but this is a detail in the entire setup.
When I have a new migration, I run it with flyway grade plugin against the local dB. Then I regenerate Jooq classes with grade Jooq plugin.
When the app is deployed in the target environment, I rely on flyway to run migration on startup. I have matching Jooq classes packaged, so everything works smoothly.
The jOOQ GitHub project has an example project that uses jOOQ with Spring Boot and the sql-maven-plugin.
You can easily replace the sql-maven-plugin by the Flyway plugin as demonstrated in the jOOQ/Flyway example project or this blog post.
On a related note, in case you're using one of the commercial distributions of jOOQ with Spring Boot, this is documented in this blog post here.
There is the following gradle task, which requires flyway, otj-pg-embedded, jooq and postgresql driver:
import com.opentable.db.postgres.embedded.*
import org.flywaydb.core.*
import org.jooq.codegen.*
tasks.named("compileKotlin") {
doFirst {
//create embedded postgresql
EmbeddedPostgres.builder().setPort(5400).start().use {
//migrate embedded posrtgresql
Flyway.configure()
.locations("filesystem:$projectDir/migrations/")
.schemas("public")
.dataSource(it.postgresDatabase)
.load()
.migrate()
//generate jooq classes
GenerationTool.generate("some xml for jooq")
}
}
}
The source is https://gist.github.com/whyoleg/63195b60eb85e8fe2114b30f28b892ef

What are the pros/cons of using the Gradle integration vs Spring Boot integration for Flyway?

Flyway has several integration options.
I'm trying to determine what the pros/cons are of using the Gradle integration vs the Spring Boot integration given that your project is already using both Spring Boot and Gradle.
The only thing I can think of is that if you want to be able to do migrations without starting the application or want to save time by not migrating every time you start the app then the Gradle choice could be better.
Think of it as build time vs run time.
In general you will build an artifact once and deploy it to many environments, so run time is a much better fit.
However sometimes build time makes sense. This is primarily for situations where you need a fully migrated database as part of the build, in order to for example generate code based on the structure of that database using frameworks like jOOQ or QueryDSL.

How do we migrate/update the database schema in Grails?

We've been working with Grails for a while and my Team Lead raised some questions about the Grails ORM (GORM):
How do we maintain the database schema once we have moved to production?
Can we update the database schema with Grails?
If the schema is updated, will the changes be automatically reflected / does the framework take care of this?
Is there any plugin for Grails that will allow us to update the schema without headaches?
I recently released the official Grails plugin for database migrations - see http://grails.org/plugin/database-migration and the docs at http://grails-plugins.github.com/grails-database-migration/docs/manual/index.html
I'm working with the author of Liquibase on this, so the older liquibase plugin is now deprecated and the new one should be used since it uses the latest version of Liquibase (2.0) and is officially supported by SpringSource. See http://blog.liquibase.org/2011/01/new-standard-liquibase-plugin-grails-database-migration.html for his announcement.
Ask usage questions on the Grails User mailing list (signup from http://grails.org/Mailing+lists) or the new plugin forum at http://grails-plugins.847840.n3.nabble.com/ or email the author directly :)
Remove dbCreate parameter in DataSource.groovy for your production environment - this will stop GORM from auto-updating DB schema.
Sure. Use LiquiBase plugin.
GORM can do it with dbCreate='update', but it's strongly not recommended. For instance, if you rename a field, GORM/LiquiBase can never determine that you have to migrate the data, and not just drop+create.
In one line: grails db-diff to generate LiquiBase's changelog.xml, and grails migrate -Dgrails.env=<whatever environment> to apply it to respective db server.
While the "auto create" functionality is ok to get a project up and running I find liquibase the best way to keep the db up-to-date. There is a grails plugin and I believe work is under way on a DSL too.
So, create a baseline schema (you could use liquibase's generate-changelog) then make all future changes through liquibase and it will manage the updates, rollbacks and even some db interop for you. You can set your DataSource.groovy config to verify and grails will not start up if the schema does not match the domain config:
environments {
development {
dataSource {
dbCreate = "validate"
You may also be interested in the liquibase-runner plugin to run your migrations on application start.

IntelliJ IDEA Hibernate

I'm learning hibernate and I am running into some issues. I'm reading "Harnessing Hibernate" by O'Reilly. They explain everything using ANT, but since I want to avoid writing a huge build.xml file, I'm trying to get it to work with IntelliJ.
I managed to make a mapping according to a DB table in a MySQL database, and wrote the bean for it. It worked, but I can't find any information on how to generate beans and SQL code, or how to reverse engineer with IntelliJ. I found loads of tutorials about Eclipse, using JBOSS Hibernate tools plugin, and the site claims this support for generating code is already in the standard installation of IntelliJ.
Am I forgetting some configuration such as adding libraries? I'm trying to find this out but I'm desperate now. Please don't suggest me to use Eclipse, I need IntelliJ for my current role.
AFAIK, IntelliJ IDEA includes the complete JPA/Hibernate support in its Ultimate Edition:
Generating Persistence Mappings from Database Schema
IntelliJ IDEA allows you to quickly
generate persistence mappings from any
database schema: Generating
Persistance Mappings
(source: jetbrains.com)
Now, the question is, what edition of Intellij IDEA are you using?
If you add the hbm2ddl to your Hibernate config and ask it to create the database schema you'll get it by running a single test or some other code that exercises Hibernate. Once you have it, turn off create.
Let Hibernate do the work.

How to generate JPA mapping file from the JPA annotated entity classes?

I am using openjpa runtime of JPA specification.
At development time I am using annotations to configure jpa entities.
At Integration, Pre-Production and and Production environment, I am using orm mapping files to configure entities. Please suggest a tool which can generate mapping files from jpa annotations, so that these mapping files can be manually edited for different environment.
If there is already a opensource maven-plugin; will be great.
I don't really know OpenJPA so there is maybe a better way to do this but one option would be to first generate the XML schema file from annotated entities using the Schema Tool and then the orm.xml file from the schema.xml using the Reverse Mapping Tool. Actually, this process is discussed in this thread.
I've checked the OpenJPA Maven Plugin but it doesn't seem to support the Reverse Mapping part (it only has a openjpa:schema goal that allows to Create a file which contains the schema mapping XML, the first required operation, but nothing for the second part). Extending the plugin to add the missing openjpa:reverse-mapping goal would thus require some development but it shouldn't be an hard task.
There is another option though. OpenJPA provides the following Ant tasks for both operations:
org.apache.openjpa.jdbc.ant.ReverseMappingToolTask
org.apache.openjpa.jdbc.ant.SchemaToolTask
So it should be possible to call them from Maven using the Maven AntRun Plugin. Check the documentation for more details on how to use them.

Categories

Resources