【发布时间】: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