[英]@Formula to count object from a @OneToMany association in spring data jpa
In a java rest api, with spring boot, and spring-dta-jpa, db is postgresql I have a Book
entity that contains bookCopies
. In a java rest api, with spring boot, and spring-dta-jpa, db is postgresql I have a Book
entity that contains bookCopies
. These bookCopies
can be available or not.这些bookCopies
可能可用或不可用。 I'm trying to retrieve the book with the numbers of copies available.我正在尝试用可用的副本数检索这本书。 The books can be searched by title or author name.书籍可以按书名或作者姓名搜索。
I manage to get the info by adding a @Transient
field in my book entity, with the @Transient
annotation on a getNbCopiesAvailable()
method, but I'm asked to do it with @Formula
annotation, and I can't figure out how to do it.我设法通过在我的书实体中添加一个@Transient
字段来获取信息,并在getNbCopiesAvailable()
方法上使用@Transient
注释,但我被要求使用@Formula
注释来完成,我不知道如何去做吧。
I'm actually getting an error:我实际上遇到了一个错误:
org.postgresql.util.PSQLException: ERREUR: plus d'une ligne renvoyée par une sous-requête utilisée comme une expression
// which means that several lines are sent by the sub-query
Here are my entities:这是我的实体:
@Entity
@Getter
@Setter
@NoArgsConstructor
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String isbn;
@NotNull
private String title;
@JsonIgnore
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true)
private List<BookCopy> copyList = new ArrayList<>();
@NotNull
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(name = "book_author",
joinColumns = @JoinColumn(name = "book_id"),
inverseJoinColumns = @JoinColumn(name = "author_id"))
private Set<Author> authors = new HashSet<>();
@Formula("(SELECT COUNT(bc.id) FROM book b left join book_copy bc on bc.book_id = b.id WHERE bc.available = 'true' GROUP BY b.id)")
private Long nbCopiesAvailable;
@Entity
@Getter
@Setter
@Builder
@AllArgsConstructor
public class BookCopy {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String barcode;
private String editor;
private boolean available = true;
@ManyToOne(fetch = FetchType.LAZY)
private Book book;
Here is my repository with the query to retrieve books by title and author:这是我的存储库,其中包含按标题和作者检索书籍的查询:
@Query("select b, a from Book b join b.authors a where b.title like %:title% and concat(a.firstName, ' ', a.lastName) like %:author%")
List<Book> findByTitleAndAuthor(@Param("title")String title,@Param("author") String author);
The @Formula("(SELECT COUNT(bc.id) FROM book b left join book_copy bc on bc.book_id = b.id WHERE bc.available = 'true' GROUP BY b.id)")
was hard to write without having syntax errors, and it's strange to me to mix native sql and jpql. @Formula("(SELECT COUNT(bc.id) FROM book b left join book_copy bc on bc.book_id = b.id WHERE bc.available = 'true' GROUP BY b.id)")
很难写语法错误,混合原生 sql 和 jpql 对我来说很奇怪。 But if I try to write the @Formula
in jpql it doesn't work at all.但是,如果我尝试在@Formula
中编写 @Formula 它根本不起作用。
I checked this topic that was the closest to my problem ( @Formula count on ManyToMany ), but it still doesn't work.我检查了这个最接近我的问题的主题( @Formula count on ManyToMany ),但它仍然不起作用。
You need to reference the book in the where clause:您需要在 where 子句中引用该书:
@Formula("(SELECT COUNT(bc.id) FROM book b " +
"left join book_copy bc on bc.book_id = b.id " +
"WHERE bc.available = 'true' " +
"and b.id = id " + // This is the important condititon
"GROUP BY b.id)")
private Long nbCopiesAvailable;
Otherwise your query is returning all books.否则,您的查询将返回所有书籍。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.