java - 在 Spring Data JPA Repository 的 JPQL 查询中返回 ManyToOne 列表
问题描述
我有一个相对简单的领域模型,涉及使用 PostgreSQL 的 Spring Boot 2 应用程序中的书籍。
简单的情况是多本书属于一个系列(有排序),例如指环王 1、指环王 2 等。
我的域实体是
@Entity
@Table(name = "seriesentity")
public class SeriesEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@Column(nullable = false)
private String title;
@OneToMany(mappedBy = "series")
@OrderColumn(name = "series_index")
private List<BookEntity> books;
//Getters and Setters left out
}
@Entity
@Table(name = "book")
public class BookEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@Column(nullable = false)
private String title;
@ManyToOne
@JoinColumn(name="series_id")
private SeriesEntity series;
//Getters/setters and other unimportant properties left out
}
我还有一个“DTO”,我想直接从数据库中检索它,然后我可以在前端使用它:
public class SeriesDto {
private Long id;
private String title;
private Locale locale;
private List<Book> books;
public SeriesDto(Long id, String title, Locale locale, List<Book> books) {
this.id = id;
this.title = title;
this.locale = locale;
this.books = books;
}
public static class Book {
private Long id;
private String title;
public Book(Long id, String title) {
this.id = id;
this.title = title;
}
}
}
在没有书籍的情况下创建 dto(在 IntelliJ 的 JPA 控制台中测试):
SELECT new series.query.SeriesDto(s.id, s.title, s.locale) FROM SeriesEntity s WHERE s.id = :id
但是如何映射 OneToMany 关系?
SELECT distinct s.id as series_id, s.title, s.locale, books FROM SeriesEntity s INNER JOIN s.books books WHERE s.id = :id
这将返回多行,例如:
1 Lord of the Rings de 46 Der Herr der Ringe Band 1: Die Gefährten 1 Lord of the Rings de
1 Lord of the Rings de 47 Der Herr der Ringe Band 2: Die zwei Türme 1 Lord of the Rings de
另一个问题是它会再次返回系列信息,因为它链接到 Book 实体。
有没有办法在 JPQL 中自动聚合它,这样我就不必编写 Java 方法了?或者我至少可以提供一个映射功能吗?
谢谢!
解决方案
你应该使用:
SELECT new series.query.SeriesDto(s.id, s.title, s.locale, s.books)
FROM SeriesEntity s
WHERE s.id = :id
可以看到查询日志:cross join(你的是inner join)
推荐阅读
- vb.net - 根据另一个类调用的下载显示进度条
- spring-webflux - 生成将函数应用于 Reactor Flux 的两个连续元素的元素
- swift - FireStore - 在数组中搜索值
- powershell - PS:在File1中定义多个变量,读取File2,将File1中的所有标签替换为File2中的变量
- javascript - 无法渲染两个函数
- python - 在 Python 中使用正则表达式选择所有排列而不重复
- azure - 如何在 PowerShell Runbook 中使用 Azure 自动化帐户“加密变量”?
- python - Django - CreateView - 如果模型表单无效,则发送自定义错误消息
- c# - C# 方法调用无法将实现接口的类转换为所述接口
- mysql - 如何创建一个计划运行 cron 任务并运行 mysqldump 的 docker 映像