我有一個小問題。在我的應用程序有兩個實體,看起來像在片段:JPA查詢與計數和組(由多對多關係連接表)
//imports, annotations, named queries
public class WishList implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "wishes_seq",
sequenceName = "wish_list_id_seq",
allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "wishes_seq")
@Basic(optional = false)
@NotNull
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 80)
@Column(name = "book_title")
private String bookTitle;
@Size(max = 80)
@Column(name = "book_cattegory")
private String bookCattegory;
@ManyToMany(cascade= CascadeType.ALL, fetch= FetchType.EAGER)
@JoinTable(name="wl_authors",
joinColumns={@JoinColumn(name="wl_id")},
inverseJoinColumns={@JoinColumn(name="author_id")})
private List<Author> authorList;
// other methods
,第二個:
//imports, annotations, named queries
public class Author implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "authors_seq",
sequenceName = "authors_id_seq",
allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,
generator = "authors_seq")
@Basic(optional = false)
@NotNull
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 58)
@Column(name = "author_name")
private String authorName;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 58)
@Column(name = "author_surname")
private String authorSurname;
@Size(max = 58)
@Column(name = "nationality")
private String nationality;
@Size(max = 64)
@Column(name = "birth_place")
private String birthPlace;
@JoinTable(name = "wl_authors",
joinColumns = {@JoinColumn(name = "author_id",
referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "wl_id",
referencedColumnName = "id")})
@ManyToMany(fetch = FetchType.EAGER)
private List<WishList> wishListList;
// other methods
正如你所看到的實體,這是我準備代表在數據庫中的兩個表,這在多對多關係中(我使用了連接表)。我想算在願望清單實體中的所有結果,它們具有相同的作者,標題和cattegory和返回所有結果如下:
- 計數結果
- 書名
- 書cattegory
- 書的作者
結果應按計數結果排序。 我準備的JPA查詢:
SELECT Count(wl.bookTitle) AS POP,
wl.bookTitle,
wl.bookCattegory
FROM WishList wl
GROUP BY wl.bookTitle,
wl.bookCattegory,
wl.authorList
ORDER BY POP DESC
不滿足我。我無法從WishList中退回此書的作者。當我在'FROM'之前放置wl.authorList時出現EJB異常:
...
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERROR: column "t1.id" must appear in the GROUP BY clause or be used in an aggregate function
...
Call: SELECT COUNT(t0.book_title), t0.book_title, t0.book_cattegory, t1.id, t1.author_name, t1.author_surname, t1.birth_place, t1.nationality FROM wl_authors t4, wl_authors t3, authors t2, authors t1, wish_list t0 WHERE (((t3.wl_id = t0.id) AND (t1.id = t3.author_id)) AND ((t4.wl_id = t0.id) AND (t2.id = t4.author_id))) GROUP BY t0.book_title, t0.book_cattegory, t2.id, t2.author_name, t2.author_surname, t2.birth_place, t2.nationality ORDER BY COUNT(t0.book_title) DESC
Query: ReportQuery(referenceClass=WishList sql="SELECT COUNT(t0.book_title), t0.book_title, t0.book_cattegory, t1.id, t1.author_name, t1.author_surname, t1.birth_place, t1.nationality FROM wl_authors t4, wl_authors t3, authors t2, authors t1, wish_list t0 WHERE (((t3.wl_id = t0.id) AND (t1.id = t3.author_id)) AND ((t4.wl_id = t0.id) AND (t2.id = t4.author_id))) GROUP BY t0.book_title, t0.book_cattegory, t2.id, t2.author_name, t2.author_surname, t2.birth_place, t2.nationality ORDER BY COUNT(t0.book_title) DESC")
Caused by: org.postgresql.util.PSQLException: ERROR: column "t1.id" must appear in the GROUP BY clause or be used in an aggregate function
...
有人可以幫助我創建apriopriate查詢嗎?是否可以在單個查詢中執行?
嗨。感謝您的答覆。我將嘗試在兩個單獨的查詢中完成。 (在我的模型中,我有名爲Book的實體 - 但它用於其他目的) – problemgenerator