Java 在hibernate jpa中设置最大提取深度

Java 在hibernate jpa中设置最大提取深度,java,hibernate,jpa,Java,Hibernate,Jpa,我有三个班,分别是A、B、C,如下所示: A类: @Entity public class A implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; @ManyToOne(cascade = CascadeType.PERSIST) private B b; //Getters and Setters } B类

我有三个班,分别是A、B、C,如下所示:

A类:

@Entity
public class A implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @ManyToOne(cascade = CascadeType.PERSIST)
    private B b;

    //Getters and Setters
}
B类:

@Entity
public class B {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @ManyToOne(cascade = CascadeType.PERSIST)
    private C c;

    //Getters and Setters
}
C类:

@Entity
public class C implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    //Getters and Settters
}
当我使用这个方法时:

em.find(A.class, a.getId());
hibernate生成以下sql查询:

Hibernate :
select
    a0_.id as id1_0_0_,
    a0_.b_id as b_id2_0_0_,
    b1_.id as id1_1_1_,
    b1_.c_id as c_id2_1_1_,
    c2_.id as id1_2_2_
from
    A a0_
left outer join
    B b1_
        on a0_.b_id=b1_.id
left outer join
    C c2_
        on b1_.c_id=c2_.id
where
    a0_.id=?
加载所有三个对象,但我不希望hibernate走那么远。 我试图通过设置

<property name="hibernate.max_fetch_depth" value="0"/>
很明显,hibernate再次获取了所有三个对象

正如政府所说:

最大提取深度: 为单端关联(一对一、多对一)的外部联接获取树设置最大“深度”。0禁用默认的外部联接获取

它通常只影响外部连接,而不是最大获取深度


有没有办法在尝试获取对象时,hibernate只带来1级关联?(我的意思是A的对象有b,但b没有c)

使关联b.c变懒。我甚至建议将它们都设置为惰性,如果您想急切地加载一些连接,可以使用使用fetch连接的特殊查询。@JBNizet谢谢您的评论,但是如果我使用em.find(B.class,B.getId())b的c对象将是惰性的且未加载。我想知道是否有任何配置可用于设置hibernate goes的最大值?@HassanMusavi,我相信会使所有关联都是惰性的,并使用
EntityGraph
注释。
Hibernate:
    select
        a0_.id as id1_0_0_,
        a0_.b_id as b_id2_0_0_
    from
        A a0_
    where
        a0_.id=?

Hibernate:
    select
        b0_.id as id1_1_0_,
        b0_.c_id as b_id2_1_0_
    from
        B b0_
    where
        b0_.id=?

Hibernate:
    select
        c0_.id as id1_2_0_,
    from
        C c0_
    where
        c0_.id=?