Skip to content

Jakarta EE and plain Hibernate

Any project with JPA entities works, with or without an application framework.

What you need

Migrax comes in two parts, and most projects use both:

Part What it's for How you get it
A tool for your terminal Writing migrations from your entities (generate) and looking after them: status, lint, verify, rollback and the other commands The Maven plugin, the Gradle plugin or the migrax command-line tool
The startup library migrax-jakarta-ee Applying the pending migrations each time the application is deployed, before JPA A dependency: see Migrate at startup

The tool for your terminal, pick one:

pom.xml
<build>
  <plugins>
    <plugin>
      <groupId>org.migrax</groupId>
      <artifactId>migrax</artifactId>
      <version>0.19.0</version>
    </plugin>
  </plugins>
</build>

Then run mvn migrax:generate, mvn migrax:status and so on in the project folder. See Maven plugin.

build.gradle.kts
plugins {
    id("org.migrax") version "0.19.0"
}

Then run ./gradlew migraxGenerate, ./gradlew migraxStatus and so on. See Gradle plugin.

Install migrax once per machine (Installation), then run migrax generate, migrax status and so on in the project folder. It works with any build, and nothing is added to the project.

Only one of the two?

  • Without the startup library the application doesn't migrate by itself: run migrate yourself or in the deployment pipeline (mvn migrax:migrate, ./gradlew migraxMigrate, migrax migrate or the container image).
  • Without a tool for your terminal there's no generate: write the migration files yourself, and the startup library still applies them.

Database settings

Migrax reads the first persistence unit in src/main/resources/META-INF/persistence.xml:

<persistence-unit name="shop">
  <properties>
    <property name="jakarta.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/shop"/>
    <property name="jakarta.persistence.jdbc.user" value="shop"/>
    <property name="jakarta.persistence.jdbc.password" value="secret"/>
    <property name="hibernate.hbm2ddl.auto" value="none"/>
  </properties>
</persistence-unit>

javax.persistence.jdbc.* and hibernate.connection.* work too. On an application server, the data source is usually defined in the server's own configuration, which the migrax command can't read: give it the connection with environment variables or options, or let the startup integration migrate inside the server.

export MIGRAX_DATABASE_URL=jdbc:postgresql://db:5432/shop
export MIGRAX_DATABASE_USER=shop
export MIGRAX_DATABASE_PASSWORD=secret
migrax migrate

Naming

Plain Hibernate keeps names as written (jpa naming). A physical or implicit naming strategy in persistence.xml (hibernate.physical_naming_strategy) is used when Migrax reads the mapping.

Running migrations

Run migrax migrate as a step of your deployment, before the new version starts, or let the application migrate when it starts on the server.

Migrate at startup

Add the integration to your WAR or EAR:

<dependency>
  <groupId>org.migrax</groupId>
  <artifactId>migrax-jakarta-ee</artifactId>
  <version>0.19.0</version>
</dependency>

When the application starts, Migrax applies the migrations from db/migration to the data source your persistence.xml names, as the server provides it. You don't repeat the connection settings. It is tested on WildFly and Payara and uses only standard Jakarta EE (CDI and JNDI), so it works on other servers too.

<persistence-unit name="shop">
  <jta-data-source>java:app/jdbc/shop</jta-data-source>
  <properties>
    <property name="jakarta.persistence.schema-generation.database.action" value="none"/>
  </properties>
</persistence-unit>

Keep the schema generation at none. The server starts JPA while it deploys the application, before any application code runs, so Hibernate's validate would check the schema before the migrations. Validate in CI with migrax verify instead.

Setting Default Meaning
migrax.enabled true Apply migrations at startup
migrax.migrate-at-startup true false: start without migrating, when a pipeline or a DBA runs the migrations; the health check and the pending gauge still report them (details)
migrax.drift.check off warn or fail: compare the database with the schema its migrations produce when the application starts; the build must put .migrax/snapshot.json into the application (details)
migrax.datasource the data source persistence.xml names JNDI name of the data source to migrate
migrax.locations db/migration Migration folders on the classpath, comma-separated
migrax.java-package db.migration Package of JavaMigration classes
migrax.env Environment: migrations marked -- migrax:env <name> run only there
migrax.resume false Re-run a failed migration marked -- migrax:resume-safe
migrax.lock-timeout 0 How long to wait while another instance migrates, for example 2m
migrax.table-lock-timeout 5s How long a migration waits for a lock another session holds (PostgreSQL) before it stops; 0 for no limit (details)
migrax.placeholders.<name> Values for ${name} placeholders
migrax.metrics.enabled true Micrometer metrics of the migrations, when the application has a MeterRegistry (Metrics and tracing)
migrax.tracing.enabled true OpenTelemetry spans of the migrations, when the application has an OpenTelemetry
migrax.schemas Schemas to migrate one after another

Jakarta EE has no standard place for application settings, so Migrax reads them from migrax.properties in the application (src/main/resources), from MicroProfile Config where the server has it, from environment variables (MIGRAX_DATASOURCE, MIGRAX_LOCATIONS, MIGRAX_PLACEHOLDERS_<NAME>, ...) and from system properties. Each one overrides the one before. (A migrax.properties in the project folder, next to pom.xml, is a different file: the migrax command's own settings.)

JPA providers

Migrax reads the mapping through the server's JPA provider: Hibernate (WildFly), EclipseLink (Payara, GlassFish) or OpenJPA (TomEE). With another JPA provider it scans the annotations and applies Hibernate's naming rules; check the generated names, and use migrax drift --entities to compare.