简体   繁体   中英

Lazy fetch for JPA @OneToOne or @ManyToOne Two Join Columns

I have a Topic entity that should correspond to one Questionnaire based on questionnaire version. I need to use lazy fetch.

However, lazy fetch doesn't work. When getting all Topics, I see Hibernate also do individual SQL queries to fetch the questionnaire for each topic.

public class Topic {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    @Access(AccessType.PROPERTY)
    private long id;

    @Column(name = "questionnaire_version")
    private String questionVer;

    @ManyToOne(optional = true, fetch = FetchType.LAZY)
    @JoinColumns({
            @JoinColumn(name = "id", referencedColumnName = "topic_id",
                insertable = false, updatable = false),
            @JoinColumn(name = "questionnaire_version", referencedColumnName = "version",
                insertable = false, updatable = false)
        })
    @Access(AccessType.PROPERTY)
    private Questionnaire questionnaire;

How do I get lazy fetch working in this case? I need OneToOne here, and I've tried both OneToOne and ManyToOne.

Parent: @OneToOne(optional = true, fetch = FetchType.LAZY, cascade = {CascadeType.ALL})

Child: @ManyToOne(optional = true, fetch = FetchType.LAZY, cascade = {CascadeType.ALL})

Use Cascading either side. You may want to judicially use ALL as per your requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM