Hibernate Fetch Profile不适用于嵌套级别的实体(三个级别)

时间:2016-10-06 15:26:40

标签: java mysql hibernate hibernate-criteria

我有四个实体,如开始,第一,第二,第三。

开始拥有第一个(单向)的一对多 第一次具有第二(单向)的一对多 第二一对多第三(单向)

获取配置文件在启动类中定义。它将在运行时使用session.enableFetchProfile()方法

启用

如果我获取启动实体,我想获取所有实体。如何使用获取配置文件概念。目前它只获取两个级别。

启动实体

     @Entity
    @FetchProfiles({
        @FetchProfile(name="Start_First_Second_Third_FP",fetchOverrides={
                @FetchOverride(entity=Start.class,association="list_first",mode=FetchMode.JOIN),
                @FetchOverride(entity=First.class,association="list_second",mode=FetchMode.JOIN),
                @FetchOverride(entity=Second.class,association="list_third",mode=FetchMode.JOIN)
        })
    })
    public class Start {

        @Id
        @GeneratedValue
        private int start_id;

        private String name;

        @OneToMany(cascade=CascadeType.ALL)
        @JoinColumn(name="start_id")
        private Set<First> list_first = new HashSet<First>();
// Getter and Setter here
}

第一实体

@Entity
public class First{
    @Id
    @GeneratedValue
    private int first_id;

    private String name;

    @OneToMany(cascade=CascadeType.ALL)
    @JoinColumn(name="first_id")
    private Set<Second> list_second = new HashSet<Second>();
// Setter and Getter 
}

第二实体

@Entity
public class Second{
    @Id
    @GeneratedValue
    private int second_id;

    private String name;

    @OneToMany(cascade=CascadeType.ALL)
    @JoinColumn(name="second_id")
    private Set<Third> list_third = new HashSet<Third>();
// Setter and Getters
}

第三实体

@Entity
public class Third{

    @Id
    @GeneratedValue
    private int third_id;

    private String name;

    public int getThird_id() {
        return third_id;
    }
// Setters and Getters
}

检索开始实体

Session session = this.sessionFactory.getCurrentSession();          
Transaction td = session.beginTransaction();

session.enableFetchProfile("Start_First_Second_Third_FP"); // Enabled Fetch Profile

System.out.println("Session Fetch Profile is "+session.isFetchProfileEnabled("Start_First_Second_Third_FP")); // It prints True

List<Object> objects = session.createCriteria(Start.class).list();

Hibernate生成的查询是:

select
        this_.start_id as start_id1_31_2_,
        this_.name as name2_31_2_,
        list_first2_.start_id as start_id3_2_4_,
        list_first2_.first_id as first_id1_2_4_,
        list_first2_.first_id as first_id1_2_0_,
        list_first2_.name as name2_2_0_,
        list_secon3_.first_id as first_id3_29_5_,
        list_secon3_.second_id as second_i1_29_5_,
        list_secon3_.second_id as second_i1_29_1_,
        list_secon3_.name as name2_29_1_ 
    from
        Start this_ 
    left outer join
        First list_first2_ 
            on this_.start_id=list_first2_.start_id 
    left outer join
        Second list_secon3_ 
            on list_first2_.first_id=list_secon3_.first_id

这里我启用了所有FetchProfiles但是hibernate查询只有三个这样的表

开始 - &gt;首先 - &gt;第二

但我想要四张桌子加入

开始 - &gt;首先 - &gt;第二 - &gt;第三

请提供解决方案我有危急情况。

0 个答案:

没有答案