【问题标题】:Can not use Kotlin to config Spring JPA Entity with Abstract Class无法使用 Kotlin 配置带有抽象类的 Spring JPA 实体
【发布时间】:2019-04-26 03:10:13
【问题描述】:

我正在尝试在 Spring 项目中使用 Kotlin,我发现实体扩展了抽象类。 Kotlin 无法分辨抽象类中的注解。配置如下。

Base.kt

package io.qiyue.dream.entity

import org.hibernate.annotations.GenericGenerator
import org.springframework.data.annotation.CreatedBy
import org.springframework.data.annotation.LastModifiedBy
import org.springframework.data.annotation.LastModifiedDate
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import java.time.LocalDateTime
import javax.persistence.Column
import javax.persistence.EntityListeners
import javax.persistence.GeneratedValue
import javax.persistence.Id

@EntityListeners(AuditingEntityListener::class)
abstract class Base {

    @Id
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
    @Column(name = "id")
    open var id: String? = null

    @Column(name = "last_modified_at")
    @LastModifiedDate
    open val lastModifiedAt: LocalDateTime? = null

    @Column(name = "last_modified_by")
    @LastModifiedBy
    open val lastModifiedBy: String? = null

    @Column(name = "created_by")
    @CreatedBy
    open val createdBy: String? = null

}

角色.kt

package io.qiyue.dream.entity

import javax.persistence.*

@Entity
@Table(name = "q_role")
open class Role (val name: String) : Base(){
}

【问题讨论】:

    标签: spring jpa kotlin configuration


    【解决方案1】:

    这在 Java 中也不起作用。

    您需要将@MappedSuperclass 添加到您的基类中,以告诉JPA 它必须包含基类中的所有属性:

    @EntityListeners(AuditingEntityListener::class)
    @MappedSuperclass
    abstract class Base {
    

    【讨论】:

    • 知道了,我错过了那个注释。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2018-10-23
    • 2020-02-21
    • 2011-12-28
    • 2016-03-31
    • 2021-05-26
    • 1970-01-01
    • 2011-10-02
    • 2019-11-23
    相关资源
    最近更新 更多