[英]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.