Java 带LeftJoin的SpringData分页投影与JPA2.1
我有一个2.1.4.0版本的项目,其中包含spring引导数据 本项目具有以下关系实体: 应用性 ApplicationTranslateEntity 语言实体 它是数据库中的一个区域设置关系表(ManyToMany),在该表中为不同语言的定位文本提供了额外的表(ApplicationTranslateEntity) 应用性:Java 带LeftJoin的SpringData分页投影与JPA2.1,java,hibernate,spring-data-jpa,jpa-2.1,Java,Hibernate,Spring Data Jpa,Jpa 2.1,我有一个2.1.4.0版本的项目,其中包含spring引导数据 本项目具有以下关系实体: 应用性 ApplicationTranslateEntity 语言实体 它是数据库中的一个区域设置关系表(ManyToMany),在该表中为不同语言的定位文本提供了额外的表(ApplicationTranslateEntity) 应用性: @Getter @Setter public class ApplicationEntity { @Id private Lon
@Getter
@Setter
public class ApplicationEntity {
@Id
private Long id;
private String urlImage;
private String urlStoreiOS;
private String urlStoreAndroid;
@OneToMany(mappedBy = "application")
Set<ApplicationTranslationEntity> applicationTranslationEntities;
}
@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);
}
投影:
@Getter
@Setter
public class ApplicationEntity {
@Id
private Long id;
private String urlImage;
private String urlStoreiOS;
private String urlStoreAndroid;
@OneToMany(mappedBy = "application")
Set<ApplicationTranslationEntity> applicationTranslationEntities;
}
@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);
}
带有查询的存储库:
@Getter
@Setter
public class ApplicationEntity {
@Id
private Long id;
private String urlImage;
private String urlStoreiOS;
private String urlStoreAndroid;
@OneToMany(mappedBy = "application")
Set<ApplicationTranslationEntity> applicationTranslationEntities;
}
@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);
}
@Query(“选择a.id作为id,a.urlImage作为urlImage,at.name作为name,at.description作为ApplicationEntity的描述a左连接a.applicationTranslationEntities at.language.key=:language”)
PageFindAllByLanguage(可分页,可分页语言);
Rest控制器应用程序:
@Getter
@Setter
public class ApplicationEntity {
@Id
private Long id;
private String urlImage;
private String urlStoreiOS;
private String urlStoreAndroid;
@OneToMany(mappedBy = "application")
Set<ApplicationTranslationEntity> applicationTranslationEntities;
}
@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);
}
@GetMapping()
Page all(Pageable Pageable,@RequestHeader(value=headerAcceptencode,required=false)语言{
返回applicationService.findAll(可分页,语言);
}
分页和id排序都很好。但当我尝试按名称排序时,它位于ApplicationTranslationEntity上,我看到hibernate尝试在ApplicationEntity中而不是ApplicationTranslationEntity中进行排序。
为什么会这样
错误是:
org.hibernate.QueryException:无法解析属性:名称:******实体.ApplicationEntity[选择a.id作为id,a.urlImage作为urlImage,a.urlStoreiOS作为urlStoreiOS,a.urlstoreadroid作为urlstoreadroid,at.name作为名称,at.description作为描述,从******.entity.ApplicationEntity.ApplicationEntity a左连接a.applicationTranslationEntity at at on.language.key=:a.id asc的语言顺序,a.name asc];嵌套异常为java.lang.IllegalArgumentException:org.hibernate.QueryException:无法解析属性:名称:******.entity.ApplicationEntity[选择a.id作为id,a.urlImage作为urlImage,a.urlStoreiOS作为urlStoreiOS,a.urlstoreadroid作为urlstoreadroid,at.name作为名称,at.description作为描述,从*********.entity.ApplicationEntity.ApplicationEntity a左连接a.applicationTranslationEntity at at on.language.key=:a.id asc的语言顺序,a.name asc]
您正在使用
name
的分页属性,而它应该是applicationTranslationEntities.name
(属性路径)或a.name
(根据连接路径)中的(idk哪一个是正确的).你用来构建Pageable的分页属性是什么?在其他作品中,展示了你是如何创建Pageable的我编辑了这个问题,我使用了导入org.springframework.data.domain.Page并且我没有在Pageable中更改任何内容。很好,但我仍然不知道你用于分页的参数是什么。它包含属性名。和at是我感兴趣的关于at.name的工作很好谢谢,我可以做些什么来改变这个?但是为什么你要改变它?我想用页面中的sort来改变name参数,使用字段的名称而不使用数据库的别名,因为如果我在查询中改变了一些东西,可能用户不能很好地使用这个sort字段,那么你将不得不映射列names从视图到自己在查询中使用的列名。