【发布时间】: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表创建和save?spring.jpa.show-sql=true和spring.jpa.properties.hibernate.format_sql=true -
实际上,如果您删除
@DiscriminatorColumn和@DiscriminatorValue注释,它应该可以工作
标签: postgresql hibernate kotlin spring-data-jpa