带有 JPA2.1 左连接的 Spring Data 可分页投影

SpringData Pageable Projection with LeftJoin with JPA2.1

我有一个 2.1 的项目。4.RELEASE 具有 spring 引导数据。

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

应用程序实体:

    @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);
}

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

错误是:

org.hibernate.QueryException: could not resolve property: name of: ******entity.ApplicationEntity [select a.id as id, a.urlImage as 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]; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: name of: ******.entity.ApplicationEntity [select a.id as id, a.urlImage as 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]

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