问题描述
我对hibernate中的一对多关系进行了急切的加载。父项目被正确拉回,但他们只在每个子项列表中接收一项。其中一位家长应该有两位。我运行Eclipse吐出来的查询,并且获取正确的结果。问题是为什么只有一个物品进入每个清单时,应该有两个?
@OneToMany(mappedBy =badge, fetch = FetchType.EAGER)
public List< BadgeLevel> getBadgeLevels(){
返回this.badgelevels;
$ / code>
SQL
<$ p从
中选择
*
(选择
this_.ID作为ID10_2_,
this_.ACTIVE作为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)作为formula0_2_,
badgelevel2_.BADGEID作为BADGEID4_,
badgelevel2_.ID作为ID4_,
badgelevel2_.ID作为ID9_0_ ,
badgelevel2_.AC TIVE为ACTIVE9_0_,
badgelevel2_.ASSETID as ASSETID9_0_,
badgelevel2_.BADGEID为BADGEID9_0_,
badgelevel2_.DATECREATED为DATECREA3_9_0_,
badgelevel2_.DATEMODIFIED为DATEMODI4_9_0_,
badgelevel2_。描述为DESCRIPT5_9_0_,
badgelevel2_.FILTERS为FILTERS9_0_,
badgelevel2 _。ORDER为ORDER9_0_,
asset3_.ID为ID2_1_,
asset3_.ACTIVE为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_
内连接
TEST.BADGELEVELS badgelevel2_
on this_.ID = badgelevel2_.BADGEID
内部连接
TEST.ASSETS asset3_
位于badgelevel2_.ASSETID = asset3_.ID
其中
this_.ACTIVE = 1
为了得到我需要的内容,大小关闭。所以越来越多,显示所有外部连接都已经通过,但是它们没有正确构建。所以我加了@Fetch注解来解决这个问题。
@OneToMany(mappedBy =badge,fetch = FetchType.EAGER)
@Fetch(value = FetchMode.SELECT)
public List< BadgeLevel> getBadgeLevels(){
返回this.badgelevels;
}
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
)
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;
}
这篇关于休眠一对多,急于不牵扯所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!