0

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>
bobbyrne01
  • 6,295
  • 19
  • 80
  • 150
  • From which package is your @NotNull annotation imported? – veljkost Jun 27 '23 at 13:58
  • `@NotNull` is from `javax.validation.constraints` – bobbyrne01 Jun 28 '23 at 21:06
  • Have you tried to update `liquibase-hibernate5` or the workaround as stated in https://stackoverflow.com/questions/31541544/liquibase-ignores-notnull? – sudo Jun 29 '23 at 15:06
  • Tried latest `liquibase-hibernate5` and still see issue. Likely occurrence of this open bug: https://github.com/liquibase/liquibase-hibernate/issues/301 – bobbyrne01 Jun 29 '23 at 15:14

0 Answers0