@ManyToOne(fetch = FetchType.LAZY,optional = false)仍在提取

时间:2018-02-12 04:11:13

标签: java hibernate jpa

我有一个@Embeddable课程:

@Embeddable
@Table(name="MY_TABLE")
public class MyTable {
    @ManyToOne(fetch=FetchType.LAZY, optional=false)
    @JoinColumn(name="my_other_id")
    private MyOtherEntity myOtherEntity;
    ...
}

@Entity
@Table(name="MY_OTHER_TABLE")
@DiscriminatorColumn(name="DISC", discriminatorType=DiscriminatorType.STRING)
public abstract class MyOtherEntity {
    ...
}

@Entity
@DiscriminatorValue("A")
public class MyOtherEntityA extends MyOtherEntity {
    ...
}

@Entity
@DiscriminatorValue("B")
public class MyOtherEntityB extends MyOtherEntity {
    ...
}

LAZY获取类型一样,我希望MyOtherEntity仅作为代理,而不是实际初始化的类。但是,一旦MyOtherEntity的所有者类调用MyTable,Hibernate仍然会发出一个查询来获取其所有列(getMyTable()中进一步标记为LAZY的列除外)。

按照Hibernate: one-to-one lazy loading, optional = falseHibernate ManyToOne FetchType.LAZY is not working?中的建议添加optional=false没有帮助。正如Hibernate ManyToOne with FetchType.LAZY not fetching lazy中所建议的那样,课程不是最终的。 LAZY如何按预期工作的任何其他原因?

1 个答案:

答案 0 :(得分:0)

指定@Proxy(lazy = true)

@Proxy(lazy=true) 
@Embeddable
@Table(name="MY_TABLE")