首页 > 解决方案 > Spring Data Jpa:OneToOne mappedBy 属性始终返回 null

问题描述

我的应用程序中有几个@OneToOne关系,它们都有相同的问题。一个例子:

实体 1:

@Entity
class OfferPagePanel(
        @OneToOne
     var offerPage: OfferPage,
)

实体 2:

@Entity
abstract class OfferPage(
        var title: String
){

    @OneToOne(mappedBy="page")
    var ogTag: OgTag? = null

    @OneToOne(mappedBy="offerPage")
    var panel: OfferPagePanel? = null
}

问题:

    fun someServiceFunction(){
        offerPage.panel //This is always null, even though a matching panel is in the database
         offerPagePanelRepository.findByPage(offerPage) //this returns the correct panel
    }

(如果你想知道的话,KT 中的语法)

标签: hibernatespring-bootjpaspring-data-jpa

解决方案


嗯..我做到了

val offerPage = repository.getOne(id)

但这基本上返回一个空壳。一旦通过findOne(id)它加载,一旦属性被调用,Hibernate 就可以使用代理加载实体......


推荐阅读