【问题标题】:SpringData Pageable Projection with LeftJoin with JPA2.1Spring Data Pageable Projection with Left Join with JPA2.1
【发布时间】:2019-07-02 18:05:20
【问题描述】:

我有一个带有 Spring Boot 数据的 2.1.4.RELEASE 项目。

该项目具有以下关系实体: 应用实体 应用程序翻译实体 语言实体 它是数据库中的一个区域设置关系表(ManyToMany),在该表中具有用于以不同语言定位的文本的额外表(ApplicationTranslateEntity)。

应用实体:

    @Getter
    @Setter
    public class ApplicationEntity {

    @Id
    private Long id;

    private String urlImage;
    private String urlStoreiOS;
    private String urlStoreAndroid;

    @OneToMany(mappedBy = "application")
    Set<ApplicationTranslationEntity> applicationTranslationEntities;

}

ApplicationTranslateEntity:

@Getter
@Setter
public class ApplicationTranslationEntity {
    @EmbeddedId
    ApplicationTranslationKey id;

    @ManyToOne
    @MapsId("application_id")
    @JoinColumn(name = "application_id")
    ApplicationEntity application;

    @ManyToOne
    @MapsId("language_id")
    @JoinColumn(name = "language_id")
    LanguageEntity language;

    @Column(length = 100)
    private String name;

    @Column(length = 1000)
    private String description;

}

投影:

public interface ApplicationProjection {

    Long  getId();
    String getName();
    String getDescription();
    String getUrlImage();
    String getUrlStoreiOS();
    String getUrlStoreAndroid();
}

带有查询的存储库:

 @Query("select  a.id as id, a.urlImage as urlImage, at.name as name, at.description as description  from ApplicationEntity a left join a.applicationTranslationEntities at on at.language.key = :language")
Page<ApplicationProjection> findAllByLanguage(Pageable pageable, Language language);

休息控制器应用程序:

 @GetMapping()
Page<ApplicationDto> all(Pageable pageable, @RequestHeader(value= headerAcceptEncoding, required = false) Language language){
    return applicationService.findAll(pageable,language);
}

分页和排序都可以正常工作。但是当我尝试按 ApplicationTranslationEntities 上的名称进行排序时,我看到休眠尝试在 ApplicationEntity 中而不是在 ApplicationTranslateEntity 中进行排序。 为什么会这样?

错误是:

org.hibernate.QueryException:无法解析属性:名称:******entity.ApplicationEntity [选择 a.id 作为 id,a.urlImage 作为 urlImage,a.urlStoreiOS 作为 urlStoreiOS,a.urlStoreAndroid 作为urlStoreAndroid, at.name as name, at.description as description from ******.entity.ApplicationEntity a left join a.applicationTranslationEntities at on at.language.key = :language order by a.id asc, a.name升序];嵌套异常是 java.lang.IllegalArgumentException:org.hibernate.QueryException:无法解析属性:名称:******.entity.ApplicationEntity [选择 a.id 作为 id,a.urlImage 作为 urlImage,a.urlStoreiOS as urlStoreiOS, a.urlStoreAndroid as urlStoreAndroid, at.name as name, at.description as description from *******.entity.ApplicationEntity a left join a.applicationTranslationEntities at on at.language.key = :language order by a.id asc, a.name asc]

【问题讨论】:

  • 您用来构建 Pageable 的分页属性是什么?在其他作品中展示您如何创建 Pageable
  • 我编辑了问题,我使用了import org.springframework.data.domain.Page,但我没有在 Pageable 中进行任何更改。
  • 很好,但我仍然不知道您使用什么参数进行分页。它包含属性名称。这就是我感兴趣的地方

标签: java hibernate spring-data-jpa jpa-2.1


【解决方案1】:

您正在使用name 的分页属性,而它应该是(idk 哪个是正确的)applicationTranslationEntities.name(属性路径)或a.name(根据连接路径)。

【讨论】:

  • 与 at.name 配合使用很好,谢谢,我可以做些什么来改变这个吗?
  • 但是你为什么要改变它?
  • 我想在页面中使用排序更改名称参数,使用没有数据库别名的字段名称,因为如果我在查询中更改某些内容,用户可能无法使用此排序字段
  • 那么您必须将视图中的列名映射到您自己在查询中使用的列名。
猜你喜欢
  • 2019-02-08
  • 2021-12-21
  • 2013-11-02
  • 2017-08-21
  • 2017-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多