gpt4 book ai didi

java - Spring Data 可分页投影与 JPA2.1 的左连接

转载 作者:行者123 更新时间:2023-12-02 09:43:30 24 4
gpt4 key购买 nike

我有一个带有 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);

休息 Controller 应用程序:

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

所有工作都可以很好地进行分页并使用 id 进行排序。但是当我尝试按 ApplicationTranslationEntities 上的名称排序时,我看到 hibernate 尝试在 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(根据连接路径)。

关于java - Spring Data 可分页投影与 JPA2.1 的左连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56857938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com