无法使用请求的结果类型为返回多个以上查询的查询创建TypedQuery [英] Cannot create TypedQuery for query with more than one return using requested result type
问题描述
我收到错误消息无法使用请求的结果类型为具有多个返回的查询创建TypedQuery" 我尝试了所有列的值返回.到那时应用程序挂起.我需要在arraylist中获取客户端列表.请帮助,我是JPA的新手.
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.
@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;
}
客户端类为
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(){
}
}
推荐答案
通常,在Hibernate上,您只需选择特定的实体,而不必定义所需的列.像这样:
Usually on Hibernate you simply make selects of an specific entity, not necessarily defining what columns you want. Something like this:
List<Client> clients = entity.createQuery("select c from Client c", Client.class).getResultList();
您收到TypedQuery错误是因为EntityManager正在等待一组Client,但是您选择了它的两个特定列,这将使Hibernate无法将结果转换为Client实体.
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.
因此,在您的情况下,请使用上面提供的查询,一切都应正常工作.
So in your case, use the query given above and everything should work fine.
这篇关于无法使用请求的结果类型为返回多个以上查询的查询创建TypedQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!