简体   繁体   English

JPA查询选择新

[英]JPA query Select new

Query: 查询:

@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);

And constructor: 和构造函数:

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;
}

I don't get any Attachments. 我没有任何附件。 But when I execute this: 但是当我执行此命令时:

@Query("SELECT ia from Attachment ia where ia.someClass=?1")
List<Attachment> findAllBySomeClass(SomeClass someClass);

then i get everything i want. 然后我得到了我想要的一切。 But i don't need all these fields, but only these listed in first query. 但是我不需要所有这些字段,而只需要在第一个查询中列出这些字段。 Why it doesn't work? 为什么不起作用?

From debugging: it doesn't even step into this constructor. 从调试开始:它甚至没有涉足此构造函数。

public Attachment(User createdBy, String fileName, String contentType, SomeClass someClass, OtherClass otherClass) 
//User, SomeClass, OtherClass wont work here like that

As you want to pick relational properties into new tuple object, you have to make proper joins yourself and provide joined objects with query like this 当您想要将关系属性选择到新的元组对象中时,您必须自己进行适当的联接,并为联接的对象提供这样的查询

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.

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