Hibernate One To Many Eager Not Pulling in all Data

孤人 提交于 2019-12-23 04:45:29

问题


I am doing an eager load on a one to many relationship in hibernate. The parent items are pulled back correctly but they only recieve one item each in their child list. One of the parent's should have two. I run the query Eclipse spits out, and it pulls the correct results. Question is why would only one item go into each list when one should have two?

 @OneToMany(mappedBy="badge", fetch=FetchType.EAGER)
     public List<BadgeLevel> getBadgeLevels() {
        return this.badgelevels;
     }

SQL

select
        * 
    from
        ( select
            this_.ID as ID10_2_,
            this_.ACTIVE as ACTIVE10_2_,
            this_.DATECREATED as DATECREA3_10_2_,
            this_.DATEMODIFIED as DATEMODI4_10_2_,
            this_.DESCRIPTION as DESCRIPT5_10_2_,
            this_.ENDDATE as ENDDATE10_2_,
            this_.GLOBAL as GLOBAL10_2_,
            this_.NAME as NAME10_2_,
            this_.PUBLISHDETAILS as PUBLISHD9_10_2_,
            this_.STARTDATE as STARTDATE10_2_,
            (SELECT
                COUNT(*) 
            FROM
                BADGELEVELS bl 
            WHERE
                bl.BADGEID = this_.ID) as formula0_2_,
            badgelevel2_.BADGEID as BADGEID4_,
            badgelevel2_.ID as ID4_,
            badgelevel2_.ID as ID9_0_,
            badgelevel2_.ACTIVE as ACTIVE9_0_,
            badgelevel2_.ASSETID as ASSETID9_0_,
            badgelevel2_.BADGEID as BADGEID9_0_,
            badgelevel2_.DATECREATED as DATECREA3_9_0_,
            badgelevel2_.DATEMODIFIED as DATEMODI4_9_0_,
            badgelevel2_.DESCRIPTION as DESCRIPT5_9_0_,
            badgelevel2_.FILTERS as FILTERS9_0_,
            badgelevel2_."ORDER" as ORDER9_0_,
            asset3_.ID as ID2_1_,
            asset3_.ACTIVE as ACTIVE2_1_,
            asset3_.DATECREATED as DATECREA3_2_1_,
            asset3_.DATEMODIFIED as DATEMODI4_2_1_,
            asset3_.DESCRIPTION as DESCRIPT5_2_1_,
            asset3_.FILENAME as FILENAME2_1_,
            asset3_.FILEPATH as FILEPATH2_1_,
            asset3_.TITLE as TITLE2_1_,
            asset3_.TYPE as TYPE2_1_ 
        from
            TEST.BADGES this_ 
        inner join
            TEST.BADGELEVELS badgelevel2_ 
                on this_.ID=badgelevel2_.BADGEID 
        inner join
            TEST.ASSETS asset3_ 
                on badgelevel2_.ASSETID=asset3_.ID 
        where
            this_.ACTIVE=1 
        ) 

回答1:


To get what I needed, the page size was off. So increasing that showed all the outer joins coming through, but they weren't being constructed correctly. So I added @Fetch annotation to fix that issue.

     @OneToMany(mappedBy="badge", fetch=FetchType.EAGER)
     @Fetch(value=FetchMode.SELECT)
     public List<BadgeLevel> getBadgeLevels() {
        return this.badgelevels;
     }


来源:https://stackoverflow.com/questions/5055961/hibernate-one-to-many-eager-not-pulling-in-all-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!