问题
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