当前位置: 首页 > 知识库问答 >
问题:

Hibernate:spring jpa存储库中order by字段的额外交叉连接

丁念
2023-03-14

我在添加嵌套字段对象时遇到了一个Spring JPARepository的问题。

public Page<MeteoInfoDto> list(MeteoInfoFilter filter, Pageable pageable) {
    mapObjectDao.findByType(MapObjectType.Meteo, pageable);
}
@Query(value = "select m from MapObject as m left join fetch m.parent as parent left join fetch m.road as road left join fetch m.parentRegion as parentRegion left join fetch m.parentOrganization as parentOrganization where m.type = :type", countQuery = "select count(m.dbid) from MapObject m where m.type = :type")
Page<MapObject> findByType(@Param("type") MapObjectType type, Pageable pageable);
select m from MapObject as m left join fetch m.parent as parent left join fetch m.road as road left join fetch m.parentRegion as parentRegion left join fetch m.parentOrganization as parentOrganization where m.type = :type order by m.parent.shortDescription asc, m.shortDescription asc
select
        mapobject0_.dbid as dbid1_27_0_,
        ....
        mapobject4_.parent_org_id as parent_15_27_4_ 
    from
        map_object mapobject0_ 
    left outer join
        map_object mapobject1_ 
            on mapobject0_.parent_id=mapobject1_.dbid 
    left outer join
        map_object mapobject2_ 
            on mapobject0_.road_id=mapobject2_.dbid 
    left outer join
        map_object mapobject3_ 
            on mapobject0_.parent_region_id=mapobject3_.dbid 
    left outer join
        map_object mapobject4_ 
            on mapobject0_.parent_org_id=mapobject4_.dbid 

    cross join map_object mapobject5_

    where
        mapobject0_.parent_id=mapobject5_.dbid 
        and mapobject0_.type=? 
    order by
        mapobject5_.short_description asc,
        mapobject0_.short_description asc limit ?

//模型

public class MapObject extends DomainObject {

    private UUID id;
    private MapObjectType type;
    ...
    private MapObject parent;
    private MapObject parentRegion;
    private MapObject parentOrganization;
    private MapObject road;
    ...
    private Set<MapObject> children;
    private Set<MapObject> childrenRegion;
    private Set<MapObject> childrenOrganization;
}

为什么它为排序parent.shortdescription添加交叉联接map_object mapobject5_

共有1个答案

许庆
2023-03-14

解决了!

    @Query(value = "select m from MapObject as m left join m.parent as parent left join fetch m.parent left join fetch m.road left join fetch m.parentRegion left join fetch m.parentOrganization where m.type = :type", countQuery = "select count(m.dbid) from MapObject m where m.type = :type")
Page<MapObject> findByType(@Param("type") MapObjectType type, Pageable pageable);

通过添加带有别名和无fetch的左联接,删除了交叉联接。

 select
        mapobject0_.dbid as dbid1_27_0_,
        mapobject2_.dbid as dbid1_27_1_,
        mapobject3_.dbid as dbid1_27_2_,
        mapobject4_.dbid as dbid1_27_3_,
        mapobject5_.dbid as dbid1_27_4_,
        mapobject0_.id as id2_27_0_,

        mapobject0_.short_description as short_de3_27_0_,       
        ...
        mapobject0_.parent_org_id as parent_15_27_0_,
        mapobject2_.id as id2_27_1_,
        ...
        mapobject2_.parent_org_id as parent_15_27_1_,
        mapobject3_.id as id2_27_2_,
        ....
        mapobject3_.parent_region_id as parent_14_27_2_,
        mapobject4_.short_description as short_de3_27_3_,
     ...      
        mapobject4_.parent_org_id as parent_15_27_3_,
        mapobject5_.id as id2_27_4_,
        mapobject5_.short_description as short_de3_27_4_,
       ...
        mapobject5_.parent_org_id as parent_15_27_4_ 
    from
        map_object mapobject0_ 
    left outer join
        map_object mapobject1_ 
            on mapobject0_.parent_id=mapobject1_.dbid 
    left outer join
        map_object mapobject2_ 
            on mapobject0_.parent_id=mapobject2_.dbid 
    left outer join
        map_object mapobject3_ 
            on mapobject0_.road_id=mapobject3_.dbid 
    left outer join
        map_object mapobject4_ 
            on mapobject0_.parent_region_id=mapobject4_.dbid 
    left outer join
        map_object mapobject5_ 
            on mapobject0_.parent_org_id=mapobject5_.dbid 
    where
        mapobject0_.type=? 
    order by
        mapobject0_.short_description asc,
        mapobject1_.short_description asc limit ?`enter code here`
 类似资料:
  • 我使用的是Querydsl 2.9、Spring Data JPA 1.3.0和Hibernate JPA 2 API 1.0版。我试图在两个表和之间进行简单的连接,连接到列。由于某些原因,Hibernate执行的查询中总是有一个额外的。这些表格如下所示: 但是,实际运行的查询是这样的: 请注意结尾处额外的。我意识到,如果在上执行可以得到正确的结果,但我不希望在不需要的情况下增加的额外开销。我尝试

  • 我正在尝试在我的设计表单上添加一个额外的字段,如下所示: 我必须允许额外的字段,所以我创建了一个新的初始值设定项devise_permitted_parameters.rb,如下所示 输出如下: "email"=? LIMIT?[["email","wtgwg@gmail.com "], ["LIMIT",1]]SQL(1.1ms)插入到"用户"("email","encrypted_passwo

  • 问题内容: 我正在尝试保存订单以及订单中的产品。 订单已保存,但产品未保存。 我有一张桌子,一张桌子和一张桌子。 在订单模型中,我设置了 在桌子上,我有几个额外的字段:,加上,以记录销售价格和售出数量。 我通过以下方式保存数据: $ this-> Order-> saveAll($ data); 这是$ data是什么: 订单已保存到订单表,但什么也没有保存到orders_products表。我希

  • 问题内容: 我有这两个课(表) 还有这个: 运行此代码后,在数据库(student_course)中创建了一个额外的表,现在我想知道如何在该表中添加额外的字段,例如(Grade,Date和…(我的意思是student_course表)),我看到了一些解决方案,但我不喜欢它们,而且我对它们有一些问题: 第一个样品 问题答案: 如果在链接表(STUDENT_COURSE)上添加额外的字段,则必须根据s

  • 我一直在Mac OSX上本地使用Git,后来决定试用Github和Bitbucket。我有一些问题使它工作,并尝试了一些修复其他人建议谁有类似的问题。我能够推送到Github和Bitbucket,但我也设法在这个过程中向我的存储库添加了额外的目录和文件。 我正在使用Xampp进行本地开发,我在那里有一个文件夹,我用它创建了我的存储库,所以文件结构如下所示

  • 问题内容: 我正在订购我的数据,并且其工作正常,但某些字段为空或没有值。订购时,这些空字段首先出现。例如,当订购数字时,我们将在获得“ 0”值之前得到一个巨大的空列表。 我这样做是这样的: 和 JSFiddle:http : //jsfiddle.net/JZuCX/1/ 有没有简单而优雅的方法可以解决此问题?无论什么情况,我都希望空白字段排在最后。 问题答案: 我要编写一个过滤器,该过滤器从有序