Hibernate嵌套了一对多关联

时间:2015-05-28 14:53:50

标签: hibernate join one-to-many fetch

我有类Polygon,LineString,Point以及以下关联:

  • 多边形 - LineString是一对多关系(例如polygon.getLineStrings())
  • LineString - Point是一对多关系(例如lineString.getPoints())

我想检索与Polygon相关的所有数据:它的值类型,LineStrings,以及每个LineString的Points。理想情况下,应该使用JOIN获取策略在单个查询中完成。

考虑以下数据库夹具:

Polygon1
|-LineString1
  |-Point1
  |-Point2
  |-Point3
|-LineString2
  |-Point4
  |-Point5

以下查询正确检索LineStrings。之后,对于每个LineString,将检索点,这是无效的:

from Polygon as p join fetch p.lineStrings as l

以下查询检索5个LineStrings(与Points一样多),这是不正确的:

select Polygon as p join fetch p.lineStrings as l join fetch l.points

然而,生成的查询似乎很好:

select polygonent0_.ID as ID1_23_0_, linestring2_.ID as ID1_14_1_, pointentit4_.ID as ID1_22_2_, polygonent0_.POLYGONID as POLYGONI2_23_0_, polygonent0_.POLYGONTYPE as POLYGONT3_23_0_, polygonent0_.POLYGONDESIGNATOR as POLYGOND4_23_0_, polygonent0_.POLYGONAREA as POLYGONA5_23_0_, polygonent0_.POLYGONCOLOUR as POLYGONC6_23_0_, linestring2_.LINESTRINGID as LINESTRI2_14_1_, linestring2_.LINESTRINGTYPE as LINESTRI3_14_1_, linestring2_.LINESTRINGDESIGNATOR as LINESTRI4_14_1_, linestring2_.LINESTRINGWIDTH as LINESTRI5_14_1_, linestring2_.LINESTRINGLENGTH as LINESTRI6_14_1_, linestring2_.LINESTRINGCOLOUR as LINESTRI7_14_1_, linestring1_.POLYGONIDREF as POLYGONI1_23_0__, linestring1_.LINESTRINGIDREF as LINESTRI2_24_0__, pointentit4_.POINTID as POINTID2_22_2_, pointentit4_.POINTTYPE as POINTTYP3_22_2_, pointentit4_.POINTDESIGNATOR as POINTDES4_22_2_, pointentit4_.POINTNORTH as POINTNOR5_22_2_, pointentit4_.POINTEAST as POINTEAS6_22_2_, pointentit4_.POINTUP as POINTUP7_22_2_, pointentit4_.POINTCOLOUR as POINTCOL8_22_2_, pointentit4_.POINTHORIZONTALACCURACY as POINTHOR9_22_2_, pointentit4_.POINTVERTICALACCURACY as POINTVE10_22_2_, pointentit4_.FILENAME as FILENAM11_22_2_, pointentit4_.FILELENGTH as FILELEN12_22_2_, points3_.LINESTRINGIDREF as LINESTRI1_14_1__, points3_.POINTIDREF as POINTIDR2_15_1__, points3_.idx as idx3_1__ 
from POLYGON polygonent0_ 
inner join POLYGON_LINESTRING linestring1_ on polygonent0_.ID=linestring1_.POLYGONIDREF 
inner join LINESTRING linestring2_ on linestring1_.LINESTRINGIDREF=linestring2_.ID 
inner join LINESTRING_POINT points3_ on linestring2_.ID=points3_.LINESTRINGIDREF 
inner join POINT pointentit4_ on points3_.POINTIDREF=pointentit4_.ID 
where polygonent0_.ID=? 

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

初看一眼,我发现你正在重复使用相同的别名' p'对于多边形和点。你能给他们不同的别名吗?