【问题标题】:Cannot persist data using kotlin and spring data无法使用 kotlin 和 spring 数据持久化数据
【发布时间】:2020-11-07 10:24:14
【问题描述】:

我想利用 JPA 中的继承。我有三个班级:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(
        name = "MY_DISC",
        discriminatorType = DiscriminatorType.STRING
)
open class BaseClass(
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        val id: Long? = null,
        ...shared things
) 

@Entity
@DiscriminatorValue(value = "FIRST_VALUE")
class FirstClass(...shared) : BaseClass(...shared things ) {

}


@Entity
@DiscriminatorValue(value = "SECOND_VALUE")
class SecondClass(...shared) : BaseClass(...shared things ) {

}

我还创建了存储库:

@Repository
interface FirstClassRepo : CrudRepository<FirstClass, Long> {
}
... other similar

但是,每当我尝试在存储库中使用 save 来保存 FirstClass 时,我都会得到:

org.postgresql.util.PSQLException: The column index is out of range: 4, number of columns: 3.

我已经检查了很多链接,但是没有一个有效,有没有关于 kotlin + spring data jpa + 继承的教程?还是我在该解决方案中遗漏了什么?

【问题讨论】:

  • 你是手动创建表还是jpa创建的?
  • 它是由 jpa 创建的。我唯一指定的是:spring.jpa.hibernate.ddl-auto=create
  • 您能否将其添加到application.properties 并使用生成的sql 更新问题以用于FirstClass 表创建和savespring.jpa.show-sql=truespring.jpa.properties.hibernate.format_sql=true
  • 实际上,如果您删除 @DiscriminatorColumn@DiscriminatorValue 注释,它应该可以工作

标签: postgresql hibernate kotlin spring-data-jpa


【解决方案1】:

显然,唯一有效的方法是添加带有注释的字段

@Column("MY_DESC", insertable = false, updatable = false)

它解决了我所有的顾虑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多