java 无法使用请求的结果类型为具有多个返回的查询创建 TypedQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33669774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot create TypedQuery for query with more than one return using requested result type
提问by krishna Ram
I am getting error "Cannot create TypedQuery for query with more than one return using requested result type" I tried with all columns value returning. That time the application hangs. I need to get list of Client, in arraylist. Please help, I am new to JPA.
我收到错误“无法使用请求的结果类型为具有多个返回的查询创建 TypedQuery” 我尝试返回所有列值。那个时候应用程序挂起。我需要在arraylist 中获取客户端列表。请帮忙,我是JPA的新手。
@Override
public ArrayList<Client> findAllClients() {
EntityManager entity = this.emf.createEntityManager();
List<Client> clients = entity.createQuery("select clientID,clientName from Client", Client.class).getResultList();
return (ArrayList<Client>) clients;
}
Client class is
客户端类是
package com.springmaven.models;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="tblclient")
public class Client {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="ntClientID")
private Long clientId;
@Column(name="vcClientName")
private String clientName;
@Column(name="vcLocation")
private String location;
@Column(name="ofstTimeZone")
private Date timeZone;
@Column(name="vcCommunicationMode")
private String communicationMode;
@Column(name="vcContact")
private String contact;
@OneToMany(targetEntity=Project.class,mappedBy="client",
cascade=CascadeType.ALL,fetch=FetchType.EAGER)
private Set<Project> projects = new HashSet<Project>();
public Set<Project> getProjects() {
return projects;
}
public void setProjects(Set<Project> projects) {
this.projects = projects;
}
public Long getClientId() {
return clientId;
}
public void setClientId(Long clientId) {
this.clientId = clientId;
}
public String getClientName() {
return clientName;
}
public void setClientName(String clientName) {
this.clientName = clientName;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Date getTimeZone() {
return timeZone;
}
public void setTimeZone(Date timeZone) {
this.timeZone = timeZone;
}
public String getCommunicationMode() {
return communicationMode;
}
public void setCommunicationMode(String communicationMode) {
this.communicationMode = communicationMode;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public Client(){
}
}
回答by Bonifacio
Usually on Hibernate you simply make selects of an specific entity, not necessarily defining what columns you want. Something like this:
通常在 Hibernate 上,您只需选择一个特定的实体,而不必定义您想要的列。像这样的东西:
List<Client> clients = entity.createQuery("select c from Client c", Client.class).getResultList();
You are getting the TypedQuery error because the EntityManager is waiting for a collection of Clients, but instead you are selecting two specific columns of it, which will make Hibernate unable to cast the results as a Client entity.
您收到 TypedQuery 错误是因为 EntityManager 正在等待一组客户端,但您选择了其中的两个特定列,这将使 Hibernate 无法将结果转换为客户端实体。
So in your case, use the query given above and everything should work fine.
所以在你的情况下,使用上面给出的查询,一切都应该正常工作。
回答by Rohit Khadse
You can cast to your result in (List< clients >)
您可以将结果转换为 (List< clients >)
List<Client> clients = (List<Client>) entity.createQuery("select clientID,clientName from Client", Client.class).getResultList();
List<Client> clients = (List<Client>) entity.createQuery("select clientID,clientName from Client", Client.class).getResultList();
回答by vastrak
That is a projection query on the "client" thay only return clientID and clientName, instead of loading the full object to memory. This approach can allow to reduce network traffic to the database server and save memory. So, you can use the next one:
这是对“客户端”的投影查询,它只返回 clientID 和 clientName,而不是将完整对象加载到内存中。这种方法可以减少到数据库服务器的网络流量并节省内存。因此,您可以使用下一个:
List<Object[]> results =
entity.createQuery("select clientID, clientName from Client").getResultList();
This result set contains a List of Object arrays and each array represents one set of properties, in this case clientID and clientName. Now you can retrieved this:
此结果集包含一个对象数组列表,每个数组代表一组属性,在本例中为 clientID 和 clientName。现在你可以检索到这个:
Object[] o = results.get(0); // for first element!
Long id = (Long) o[0]; // choose the correct type!
String name = (String) o[1];