Unidirectional @OneToOne with @MapsId does not work with lazy loading

大城市里の小女人 提交于 2020-05-29 08:42:26

问题


I want to map a @OneToOne association using Hibernate 5.3.10 and JPA.

I know that the parent side of a @OneToOne association cannot be loaded lazily when not using bytecode enhancements.

In this case, I only want to map the client side and use @MapsId association which is suggested here: Best way to map onetoone

Here is my mapping on the Client side. The parent side CardEntity has no mapping to the DeviceType at all.

public class DeviceType {

    @Id
    @Column( name = "PRODUCT_CARD_TYPE_ID" )
    private Long cardTypeId;

    ...

    @OneToOne( fetch = FetchType.LAZY )
    @MapsId
    @JoinColumn( name = "PRODUCT_CARD_TYPE_ID" )
    private CardEntity card;

    ....
}

I give it an extra @JoinColumn because the KEY column in the CardEntity has a different name than "PRODUCT_CARD_TYPE_ID". See Change Id Column

For this mapping, LAZY loading does not work. It always executes another statement to fetch the CardEntity. What I am doing wrong here?


回答1:


It looks like this is the HHH-12842. The described approach perfectly works in the hibernate 5.4. But it does not work in the hibernate 5.3 branch.



来源:https://stackoverflow.com/questions/60597259/unidirectional-onetoone-with-mapsid-does-not-work-with-lazy-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!