hibernate获取配置文件不适用于嵌套级别的实体(三个级别)

shyt4zoc  于 2021-06-25  发布在  Mysql
关注(0)|答案(0)|浏览(285)

我有四个实体,比如开始,第一,第二,第三。
先开始一对多(单向)
第一个有一对多和第二个(单向的)
第二个有一对多和第三个(单向的)

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

如果我获取开始实体,我想获取所有实体。如何使用fetch profile概念。目前它只有两个级别。

起始实体

@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查询只有三个这样的表
开始-->第一-->第二
但是我想要四张table
开始-->第一-->第二-->第三
请提供解决方案,我有紧急情况。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题