首页 > 解决方案 > Liquibase 差异:运行 Liquibase 时出现意外错误:无法加载类 [com.company.product.model.persist.Address]

问题描述

我正在尝试使用该liquibase-hibernate插件来促进我的 Kotlin 项目中的数据库迁移。我的build.gradle.kts文件配置如下:

plugins {
    [...]
    id("org.liquibase.gradle") version "2.0.1"
    [...]
}
dependencies {
    [...]
    liquibaseRuntime("org.liquibase:liquibase-core:3.6.2")
    liquibaseRuntime("org.liquibase.ext:liquibase-hibernate5:3.6")
    liquibaseRuntime("org.postgresql:postgresql")
    liquibaseRuntime("org.springframework.boot:spring-boot:2.1.6.RELEASE")
    liquibaseRuntime("org.springframework:spring-orm:5.1.8.RELEASE")
    liquibaseRuntime("ch.qos.logback:logback-classic:1.2.3")

    liquibaseRuntime(sourceSets.getByName("main").output)
    [...]
}
liquibase {
    activities.register("main") {
        this.arguments = mapOf(
                "changeLogFile" to "src/main/resources/db/changelog/db.changelog-master.yaml",
                "referenceUrl" to "hibernate:spring:com.company.product.model.persist?dialect=org.hibernate.dialect.PostgreSQL9Dialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy&hibernate.enhanced_id=true",
                "url" to "jdbc:postgresql://localhost:5432/postgres",
                "username" to "<username>",
                "password" to "<password>",
                "driver" to "org.postgresql.Driver"
        )
    }
    runList = "main"
}

我还有一个Address.kt文件如下:

package com.company.product.model.persist

import com.company.product.model.AbstractJpaPersistable
import javax.persistence.CascadeType
import javax.persistence.Entity
import javax.persistence.FetchType

@Entity
class Address(
        val street: String,
        val zipCode: String,
        val city: String
) : AbstractJpaPersistable<Long>()

我遇到的问题是在运行liquibase diff命令时出现以下错误:

运行 Liquibase 时出现意外错误:无法加载类 [com.company.product.model.persist.Address]

我已经验证了build/classes/kotlin/main/com/company/product/model/persist其中有一个已编译的Address类文件。

我已经尝试了所有我能想到的配置组合,但没有运气。我什至尝试在 GitHub 页面上按各种用户记录的那样指向liquibaseRuntimefiles("src/main")liquibase-hibernate没有运气。任何帮助表示赞赏。谢谢!


编辑

这条线是问题所在:: AbstractJpaPersistable<Long>().

如果我弄清楚为什么会这样,我会回帖。与此同时,这里是代码:

package com.company.product.model

import org.springframework.data.domain.Persistable
import org.springframework.data.util.ProxyUtils
import java.io.Serializable
import javax.persistence.GeneratedValue
import javax.persistence.Id
import javax.persistence.MappedSuperclass
import javax.persistence.Transient

/**
 * Abstract base class for entities. Allows parameterization of id type, chooses auto-generation and implements
 * [equals] and [hashCode] based on that id.
 *
 * This class was inspired by [org.springframework.data.jpa.domain.AbstractPersistable], which is part of the Spring Data project.
 */
@MappedSuperclass
abstract class AbstractJpaPersistable<T : Serializable> : Persistable<T> {

    companion object {
        private val serialVersionUID = -5554308939380869754L
    }

    @Id
    @GeneratedValue
    private var id: T? = null

    override fun getId(): T? {
        return id
    }

    /**
     * Must be [Transient] in order to ensure that no JPA provider complains because of a missing setter.
     *
     * @see org.springframework.data.domain.Persistable.isNew
     */
    @Transient
    override fun isNew() = null == getId()

    override fun toString() = "Entity of type ${this.javaClass.name} with id: $id"

    override fun equals(other: Any?): Boolean {
        other ?: return false

        if (this === other) return true

        if (javaClass != ProxyUtils.getUserClass(other)) return false

        other as AbstractJpaPersistable<*>

        return if (null == this.getId()) false else this.getId() == other.getId()
    }

    override fun hashCode(): Int {
        return 31
    }
}

标签: javahibernatekotlinliquibaseliquibase-hibernate

解决方案


推荐阅读