使用SpringBoot 3和Hibernate 6,任何基于我的Java Entity类生成的Liquibase更改日志都是空的

jjhzyzn0  于 2023-11-15  发布在  Java
关注(0)|答案(1)|浏览(141)

我有一个基于SpringBoot 3的Gradle项目,使用Hibernate 6和Liquibase。
目标是使用liquibase-hibernate6依赖项生成一个初始Liquibase变更日志,其中包含我的Java Entity类(即Item.java)的详细信息。
问题是生成的更新日志基本上是空的。
为了从Java Entity类生成Liquibase变更日志,缺少了什么?

val liquibaseVersion: String = "4.23.0"
val liquibaseGradlePluginVersion: String = "2.2.0"

plugins {
    java
    war

    id("org.springframework.boot") version "3.1.1"
    id("io.spring.dependency-management") version "1.1.0"
    id("org.liquibase.gradle") version "2.2.0"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"

java {
    sourceCompatibility = JavaVersion.VERSION_17
}

repositories {
    mavenCentral()
}

dependencies {

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

    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-web")

    implementation("org.liquibase:liquibase-core:$liquibaseVersion")
    implementation("org.liquibase:liquibase-gradle-plugin:$liquibaseGradlePluginVersion")
    implementation("org.hibernate.orm:hibernate-core:6.2.5.Final")
    implementation("org.liquibase.ext:liquibase-hibernate6:$liquibaseVersion")

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

    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")

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

tasks.withType<Test> {
    useJUnitPlatform()
}

liquibase {
    activities.register("main") {
        this.arguments = mapOf(
            "changelogFile" to "src/main/resources/config/liquibase/changelog/0.1.0/000000_00000000_initial-changelog.xml",
            "url" to "hibernate:spring:com.example?dialect=org.hibernate.dialect.PostgreSQLDialect"
        )
    }
}

字符集
我有一个Java实体:

package com.example.demo;

import jakarta.persistence.*;

@Entity
public class Item {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false)
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}


通过Gradle运行generateChangeLog后Liquibase更新日志的内容:

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd"/>

i34xakig

i34xakig1#

老实说,我想知道为什么这是甚至工作.因为你使用Postgres你必须提供至少一个用户名.不安静的确定密码.你可以有一个用户在Postgres没有密码?无论如何,更重要的是,你必须指向你的包与你的实体类,如解释这里.
使用Gradle文件的此更新版本重试:

plugins {
    // The WAR plugin extends the Java plugin
    id 'war'

    id 'org.springframework.boot' version '3.1.5'
    id 'io.spring.dependency-management' version '1.1.3'
    id 'org.liquibase.gradle' version '2.2.0'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = JavaVersion.VERSION_17
}

repositories {
    mavenCentral()
}

dependencies {
    // Web already contains Tomcat as Webserver
    implementation 'org.springframework.boot:spring-boot-starter-web'
    // Data JPA already contains Hibernate
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    
    // jackson-databind has jackson-core as dependency
    implementation 'com.fasterxml.jackson.core:jackson-databind'
    implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml'

    implementation 'org.postgresql:postgresql:42.6.0'
    
    liquibaseRuntime 'org.liquibase:liquibase-core:4.24.0'
    liquibaseRuntime 'org.liquibase:ext.liquibase-hibernate6:4.24.0'
    liquibaseRuntime 'info.picocli:picocli:4.7.5'
    liquibaseRuntime sourceSets.main.output
}

tasks.named('test') {
    useJUnitPlatform()
}

liquibase {
    activities {
        def springProperties = new Properties()
        file('src/main/resources/application.properties').withInputStream { springProperties.load(it) }
        main {
            url springProperties.getProperty('spring.datasource.url')
            username springProperties.getProperty('spring.datasource.username')
            // Comment the password out, if there is no password
            password springProperties.getProperty('spring.datasource.password')
            // Assumption here: Your entity classes live in the "persistence" package
            referenceUrl "hibernate:spring:${group}.persistence?dialect=org.hibernate.dialect.PostgreSQLDialect"
            changelogFile 'src/main/resources/config/liquibase/changelog/0.1.0/000000_00000000_initial-changelog.xml'
            logLevel 'info'
        }
    }
}

diffChangelog.configure {
    // Keep Java byte code up to date
    dependsOn(compileJava)
}

字符集

相关问题