因为您具有课程列表和一组汽车,所以您只需在一个查询中获取整个图形即可:
select sfrom Student sleft join fetch s.coursesleft join fetch s.dealer dleft join fetch d.carswhere s.id = :id
因为您要获取两个集合,所以此查询将生成笛卡尔乘积,因此您需要确保所选的子集合没有太多条目。
如果您不想运行笛卡尔积,则只需运行以下查询:
select sfrom Student sleft join fetch s.coursesleft join fetch s.dealer dwhere s.id = :id
然后您访问Dealer.cars并通过单独的查询获取该集合:
Student s = ...;s.getDealer().getCars().size();



