[英]JPA query Select new
查詢:
@Query("SELECT new com.abc.cba.domain.Attachment(ia.createdBy,
ia.fileName, ia.contentType, ia.someClass, ia.otherClass) from Attachment ia
where ia.someClass=?1")
List<Attachment> findAllBySomeClass(SomeClass someClass);
和構造函數:
public Attachment(User createdBy, String fileName, String contentType, SomeClass someClass, OtherClass otherClass) {
this.createdBy = createdBy;
this.fileName = fileName;
this.contentType = contentType;
this.someClass = someClass;
this.otherClass = otherClass;
}
我沒有任何附件。 但是當我執行此命令時:
@Query("SELECT ia from Attachment ia where ia.someClass=?1")
List<Attachment> findAllBySomeClass(SomeClass someClass);
然后我得到了我想要的一切。 但是我不需要所有這些字段,而只需要在第一個查詢中列出這些字段。 為什么不起作用?
從調試開始:它甚至沒有涉足此構造函數。
public Attachment(User createdBy, String fileName, String contentType, SomeClass someClass, OtherClass otherClass)
//User, SomeClass, OtherClass wont work here like that
當您想要將關系屬性選擇到新的元組對象中時,您必須自己進行適當的聯接,並為聯接的對象提供這樣的查詢
SELECT new com.abc.cba.domain.Attachment(user, ia.fileName, ia.contentType (....)) FROM Attachent ia JOIN ia.createdBy user JOIN ......"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.