Java 返回Spring数据JPA存储库中JPQL查询中的多通列表
我有一个相对简单的域模型,它涉及使用PostgreSQL的SpringBoot2应用程序中的书籍 简单的情况是,多本书属于一个系列(有顺序),例如《指环王1》、《指环王2》等 我的域实体是Java 返回Spring数据JPA存储库中JPQL查询中的多通列表,java,spring,jpql,Java,Spring,Jpql,我有一个相对简单的域模型,它涉及使用PostgreSQL的SpringBoot2应用程序中的书籍 简单的情况是,多本书属于一个系列(有顺序),例如《指环王1》、《指环王2》等 我的域实体是 @Entity @Table(name = "seriesentity") public class SeriesEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;
@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
}
另一个问题是,它将再次返回序列信息,因为它链接到Book实体
有没有什么方法可以在JPQL中自动聚合这些内容,这样我就不必编写Java方法了?或者我至少可以提供一个映射函数吗
谢谢 您应该使用:
SELECT new series.query.SeriesDto(s.id, s.title, s.locale, s.books)
FROM SeriesEntity s
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
SELECT new series.query.SeriesDto(s.id, s.title, s.locale, s.books)
FROM SeriesEntity s
WHERE s.id = :id