OneToOne不适用于延迟加载

OneToOne不适用于延迟加载

本文介绍了具有@MapsId的单向@OneToOne不适用于延迟加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Hibernate 5.3.10和JPA映射@OneToOne关联.

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

我知道,当不使用字节码增强功能时,@OneToOne关联的父端不会被延迟加载.

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

在这种情况下,我只想映射客户端并使用在此处建议的@MapsId关联:映射onetoone的最佳方法

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

这是我在客户端的映射.父方CardEntity完全没有与DeviceType的映射.

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;

    ....
}

我给它一个额外的@JoinColumn,因为CardEntity中的KEY列的名称与"PRODUCT_CARD_TYPE_ID"不同.参见更改ID列

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

对于此映射,LAZY加载不起作用.它总是执行另一条语句来获取CardEntity.我在这里做错了什么?

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

推荐答案

它看起来像是 HHH-12842 .所描述的方法在hibernate 5.4中完全有效.但是它在休眠5.3分支中不起作用.

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.

这篇关于具有@MapsId的单向@OneToOne不适用于延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 08:23