jpa SpringBoot 3.1.2升级(从3.0.6)-现有的单向@OneToOneMap不再工作,(hibernate.PropertyAccessException)

2uluyalo  于 2023-10-19  发布在  Spring
关注(0)|答案(1)|浏览(120)

问题:

我目前正在将一个API从SpringBoot 3.0.6升级到SpringBoot 3.1.2。在内部,Hibernate也从6.1.7.Final更新到6.2.6.Final
通过此次要版本更新,现有的@OneToOneMap在调用RecordDao时会中断,因为它无法推断Record实体中定义的2个子实体之间的正确类型。
Caused by: org.hibernate.PropertyAccessException: Could not set value of type [com.hidden.CurrentRole] : 'com.hidden.Record.futureRole' (setter)

现有编码:

我有一个父实体,其中2个子实体使用JOINED继承策略继承父实体的ID。@Inheritance(strategy = InheritanceType.JOINED)
最后,一个单独的实体使用@OneToOneMap将两个子类关联起来。

父节点:

@Entity
@Table(name = "RESOURCE")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "RESOURCE_TYPE_CD", discriminatorType = DiscriminatorType.INTEGER, columnDefinition = "INT")
abstract class Resource(

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "RESOURCE_ID_SEQ")
    @SequenceGenerator(name = "RESOURCE_ID_SEQ", sequenceName = "RESOURCE_ID_SEQ", allocationSize = 1)
    override var id: Long? = null,

    @Column(name = "RESOURCE_TYPE_CD", insertable = false, updatable = false)
    open var resourceType: ResourceType

): MutableAuditableBaseJpa<Long>()

子实体:

@Entity
@Table(name = "RESOURCE_FUTURE_ROLE")
@DiscriminatorValue("1")
class FutureRole(
    ...
): Resource(null, ResourceType.FUTURE_ROLE)
@Entity
@Table(name = "RESOURCE_CURRENT_ROLE")
@DiscriminatorValue("2")
class CurrentRole(
    ...
): Resource(null, ResourceType.CURRENT_ROLE)

记录存在子关联的实体和dao

@Entity
@Table(name = "RECORD")
open class Record(

    @Column(name = "RESOURCE_ID") override var resourceId: Long,
    ...
): MutableAuditableBaseJpa<Long>() {
    @OneToOne
    @JoinColumn(name = "RESOURCE_ID", updatable = false, insertable = false)
    @NotFound(action = NotFoundAction.IGNORE)
    open var futureRole: FutureRole? = null

    @OneToOne
    @JoinColumn(name = "RESOURCE_ID", updatable = false, insertable = false)
    @NotFound(action = NotFoundAction.IGNORE)
    open var employee: CurrentRole? = null
}
interface RecordDao: JpaRepository<Record, Long> {
    fun findByResourceId(resourceId: Long): Record?
}

我尝试将@OneToOne子关联更新为@ManyToOne。

@ManyToOne
@JoinTable(name = "RESOURCE_FUTURE_ROLE", joinColumns = [JoinColumn(name = "ID", insertable=false, updatable=false)], inverseJoinColumns = [JoinColumn(name = "ID", insertable=false, updatable=false)])
@NotFound(action = NotFoundAction.IGNORE)
open var futureRole: FutureRole? = null

@ManyToOne
@JoinTable(name = "RESOURCE_CURRENT_ROLE", joinColumns = [JoinColumn(name = "ID", insertable=false, updatable=false)], inverseJoinColumns = [JoinColumn(name = "ID", insertable=false, updatable=false)])
@NotFound(action = NotFoundAction.IGNORE)
open var employee: CurrentRole? = null

通过此更新,我确实看到资源正在返回,但在部署时,所有数据都不正确。

相关问题