0
votes

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
Can you post the mapping of both classes? At least ids, the relationships and any annotations at the class level.Augusto
It was rownum which was three. But my one to many is pulling five results instead of three. The five come from the outer join happening. I thought hibernate was smart enough go combine these into three entities with their children attached. Am I not correct?Mike Flynn

1 Answers

6
votes

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;
     }