带有LeftJoin和JPA2.1的SpringData可分页投影

时间:2019-07-02 18:05:20

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

我有一个带有2.1.4.RELEASE的项目,其中包含弹簧启动数据。

该项目具有以下关系实体: 应用实体 ApplicationTranslateEntity 语言实体 它是数据库(ManyToMany)中的语言环境关系表,其中包含以不同语言显示的文本的额外表,该表中的额外列(ApplicationTranslateEntity)。

ApplicationEntity:

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

Rest Controller应用程序:

 @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:无法解析属性:名称:****** entity.ApplicationEntity [选择a.id作为id,a.urlImage作为urlImage,a.urlStoreiOS作为urlStoreiOS,a.urlStoreAndroid作为urlStoreAndroid,at.name作为名称,at.description作为来自******。entity.ApplicationEntity的描述,在at.language.key =:language上通过a.id asc,a.name左连接a.applicationTranslationEntities asc];嵌套异常是java.lang.IllegalArgumentException:org.hibernate.QueryException:无法解析属性:名称:******。entity.ApplicationEntity [选择a.id作为id,a.urlImage作为urlImage,a.urlStoreiOS作为urlStoreiOS,a.urlStoreAndroid作为urlStoreAndroid,at.name作为名称,at.description作为来自*******。entity.ApplicationEntity的描述,在at.language.key =:language上按顺序加入a.applicationTranslationEntities a.id asc,a.name asc]

1 个答案:

答案 0 :(得分:1)

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