Java @NotNull constraint information not part of generated Liquibase changelog when using diffChangeLog command

66 Views Asked by At

I have a Java Entity with an id field.

I've now added a new column to the Entity but I would like to add a Java bean validation constraint of @NotNull to that field but the generated Liquibase changelog does not contain any information about the constraint.

Any ideas why the constraint information is not being generated into the Liquibase changelog?

Gradle script:

val projectVersion: String by project
val liquibaseVersion: String = "4.20.0"
val liquibaseGradlePluginVersion: String = "2.0.4"

plugins {
    java
    id("org.springframework.boot") apply false
    id("org.liquibase.gradle") apply true
}

dependencies {

    implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))

    liquibaseRuntime(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))

    implementation("org.postgresql:postgresql")
    implementation("info.picocli:picocli:4.6.3")

    implementation("org.liquibase:liquibase-core:$liquibaseVersion")
    implementation("org.liquibase:liquibase-gradle-plugin:$liquibaseGradlePluginVersion")

    implementation("org.hibernate:hibernate-core")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.liquibase.ext:liquibase-hibernate5:$liquibaseVersion")

    implementation("com.fasterxml.jackson.core:jackson-core")
    implementation("com.fasterxml.jackson.core:jackson-databind")
    implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml")

    liquibaseRuntime("info.picocli:picocli:4.6.3")
    liquibaseRuntime("org.liquibase:liquibase-core:$liquibaseVersion")
    liquibaseRuntime("org.postgresql:postgresql")
    liquibaseRuntime("org.liquibase.ext:liquibase-hibernate5:$liquibaseVersion")
    liquibaseRuntime("org.springframework.boot:spring-boot-starter-data-jpa")
    liquibaseRuntime(sourceSets.getByName("main").output)
}

tasks.register("propertiesForDiffChangeLog") {
    doLast {
        propertiesForDiffChangeLog()
    }
}

tasks.getByName("diffChangeLog").dependsOn(tasks.getByName("propertiesForDiffChangeLog"))
tasks.getByName("diffChangeLog").dependsOn(JavaPlugin.CLASSES_TASK_NAME)

fun propertiesForDiffChangeLog() {

    liquibase {

        val schemaName = "test"

        activities.register("main") {
            this.arguments = mapOf(
                    "driver" to "org.postgresql.Driver",
                    "changeLogFile" to "src/main/resources/config/liquibase/changelog/TODO_describe-change.xml",
                    "referenceUrl" to "hibernate:spring:com.test?dialect=org.hibernate.dialect.PostgreSQLDialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy",
                    "url" to "jdbc:postgresql://localhost:5432/postgres",
                    "username" to "postgres",
                    "password" to password,
                    "defaultSchemaName" to "$schemaName"
            )
        }
    }
}

Java Entity:

import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Entity
@Table(name = "item")
public class ItemEntity {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "id")
  private Long id;

  @NotNull // <-- Not present in Liquibase changelogs
  @Column(name = "another")
  private String anotherField;

  /** No-args constructor required by Hibernate to maintain compatibility across VMs. */
  public ItemEntity() {}
}

Liquibase changelog:

<changeSet author="me" id="1687774415103-1">
    <addColumn tableName="item">
        <column name="another" type="varchar(255)"/>
    </addColumn>
</changeSet>
0

There are 0 best solutions below