Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 647
TosKen Avatar asked Sep 14 '25 03:09

TosKen


1 Answers

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.

like image 70
SternK Avatar answered Sep 15 '25 21:09

SternK