简体   繁体   English

如何使用 SPRING DATA 在 1 个查询中获得 2 个 SELECT

[英]How get 2 SELECT in 1 query with SPRING DATA

I have 3 entities :我有 3 个实体:

@Entity
@Table(name = "copy")
public class Copy {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private Long id;

    @Column(name = "format")
    private String format;

    @Column(name = "status")
    private String status;

    @ManyToOne
    @JoinColumn(name = "book_id")
    private Book book;

    @ManyToOne
    @JoinColumn(name = "library_id")
    private Library library;


@Entity
@Table(name = "book")
public class Book implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private Long id;

    @Column(name = "title")
    private String title;

    @Column(name = "pub_date")
    private Date pubDate;

    @Column(name = "page")
    private int page;

    @Column(name = "synopsis")
    private String synopsis;

    //TODO Image à gérer
    @Column(name = "cover")
    private String cover;

    @ManyToOne
    @JoinColumn(name = "categorie_id")
    private Categorie categorie;

    @ManyToOne
    @JoinColumn(name = "author_id")
    private Author author;

    @OneToMany(mappedBy = "book")
    List<Copy> copyList = new ArrayList<>();


@Entity
@Table(name = "library")
public class Library implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private Long id;

    @Column(name = "nom")
    private String nom;

    @Column(name = "adress")
    private String adress;

    @Column(name = "phone_num")
    private String phoneNum;

    @Column(name = "email")
    private String email;

    @OneToMany(mappedBy = "library")
    private List<Copy> copyList = new ArrayList<>();

I would like to recover the number of copies of a book according to its format and its libraries.我想根据书的格式和图书馆恢复一本书的副本数。 However I cannot figure out how to retrieve a list of copies and the total number depending on the format and its library.但是,我无法弄清楚如何根据格式及其库检索副本列表和总数。 How can I do.我能怎么做。 I wrote this request but I can't get what I want.我写了这个请求,但我无法得到我想要的。

My request :我的请求 :

    @Query("SELECT DISTINCT  c, COUNT(c.format) FROM Copy c WHERE c.book.id = :id")
    List<Copy> getCopyById(@Param("id") Long id);

first you need to create class to handel query result (copy,total)首先,您需要创建类来处理查询结果(复制,总计)

public class CopyWithTotal{
    Copy c;
    int total;
    CopyWithTotal(Copy c, int total){
        this.c = c;
        this.total = total;
    }
}

then you should constratc this class in the query那么你应该在查询中构造这个类

@Query("SELECT new packgeTo.CopyWithTotal(DISTINCT  c, COUNT(c.format)) FROM Copy c WHERE c.book.id = :id group by c")
List<CopyWithTotal> getCopyById(@Param("id") Long id);

whenever you use aggregation function like count all selected column shoud apper in the group by每当您使用聚合函数时,例如计算组中的所有选定列都应按

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM