Hibernate @ManyToOne optional=true fetches parent

2019-08-23 04:14发布

问题:

Hibernate optional @ManyToOne relationship still fetches the Parent in a separate select optional=true and fetch=FetchType.Lazy

Child

@ManyToOne //may not exist
@JoinColumns({
        @JoinColumn(name="parent_key1", insertable=false, updatable=false),
        @JoinColumn(name="parent_key2", insertable=false, updatable=false)
    })
    Parent parent

Note, that I can have a value for parent_key1, parent_key2 columns existing in Child table but a corresponding Parent need not exist with this key, that's why the optional.

These are not final classes and any select on Child is fetching Parent again in a separate select with or without LEFT JOIN FETCH suggesting optional=true and lazy do not work.

Can Parent be lazy without compile time instrumentation using @OneToOne?