Java 无法使用请求的结果类型为具有多个返回的查询创建TypedQuery

Java 无法使用请求的结果类型为具有多个返回的查询创建TypedQuery,java,spring,hibernate,jpa,Java,Spring,Hibernate,Jpa,我收到错误“无法使用请求的结果类型为具有多个返回的查询创建TypedQuery” 我尝试让所有列返回值。此时应用程序挂起。我需要在arraylist中获取客户列表。请帮忙,我是JPA的新手 @Override public ArrayList<Client> findAllClients() { EntityManager entity = this.emf.createEntityManager(); List<Client> c

我收到错误“无法使用请求的结果类型为具有多个返回的查询创建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;
    }
@覆盖
公共阵列列表findAllClients(){
EntityManager实体=this.emf.createEntityManager();
List clients=entity.createQuery(“从客户端选择clientID、clientName”,Client.class).getResultList();
返回(ArrayList)客户端;
}
客户端类是

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(){

    }
}
package com.springmaven.models;
导入java.util.Date;
导入java.util.HashSet;
导入java.util.Set;
导入javax.persistence.CascadeType;
导入javax.persistence.Column;
导入javax.persistence.Entity;
导入javax.persistence.FetchType;
导入javax.persistence.GeneratedValue;
导入javax.persistence.GenerationType;
导入javax.persistence.Id;
导入javax.persistence.OneToMany;
导入javax.persistence.Table;
@实体
@表(name=“tblclient”)
公共类客户端{
@Id@GeneratedValue(strategy=GenerationType.IDENTITY)@Column(name=“ntClientID”)
私人长客户;
@列(name=“vcClientName”)
私有字符串clientName;
@列(name=“vcLocation”)
私有字符串位置;
@列(name=“ofstTimeZone”)
私人日期时区;
@列(name=“vcCommunicationMode”)
私有字符串通信模式;
@列(name=“vcContact”)
私人字符串联系人;
@OneToMany(targetEntity=Project.class,mappedBy=“client”,
cascade=CascadeType.ALL,fetch=FetchType.EAGER)
private Set projects=new HashSet();
公共集getProjects(){
返回项目;
}
公共项目集合(集合项目){
这个项目=项目;
}
公共长getClientId(){
返回客户ID;
}
public void setClientId(长clientId){
this.clientId=clientId;
}
公共字符串getClientName(){
返回clientName;
}
public void setClientName(字符串clientName){
this.clientName=clientName;
}
公共字符串getLocation(){
返回位置;
}
公共void集合位置(字符串位置){
这个位置=位置;
}
公共日期getTimeZone(){
返回时区;
}
公共无效设置时区(日期时区){
this.timeZone=时区;
}
公共字符串getCommunicationMode(){
返回通信模式;
}
public void setCommunicationMode(字符串communicationMode){
this.communicationMode=通信模式;
}
公共字符串getContact(){
回接;
}
公共无效设置联系人(字符串联系人){
this.contact=contact;
}
公共客户机(){
}
}

通常在Hibernate上,您只需选择特定的实体,而不一定要定义所需的列。大概是这样的:

List<Client> clients = entity.createQuery("select c from Client c", Client.class).getResultList();
List clients=entity.createQuery(“从客户机c中选择c”,Client.class).getResultList();
出现TypedQuery错误是因为EntityManager正在等待客户端集合,但是您选择了其中的两个特定列,这将使Hibernate无法将结果强制转换为客户端实体


因此,在您的情况下,使用上面给出的查询,一切都会正常工作。

通常在Hibernate上,您只需选择特定的实体,而不一定要定义所需的列。大概是这样的:

List<Client> clients = entity.createQuery("select c from Client c", Client.class).getResultList();
List clients=entity.createQuery(“从客户机c中选择c”,Client.class).getResultList();
出现TypedQuery错误是因为EntityManager正在等待客户端集合,但是您选择了其中的两个特定列,这将使Hibernate无法将结果强制转换为客户端实体


因此,在您的情况下,使用上面给出的查询,一切都会正常工作。

您可以在(列表)中转换结果


List clients=(List)entity.createQuery(“选择clientID,clientName from Client”,Client.class).getResultList()

您可以在(列表)中强制转换到结果


List clients=(List)entity.createQuery(“选择clientID,clientName from Client”,Client.class).getResultList()

这是“客户端”上的投影查询,它只返回clientID和clientName,而不是将整个对象加载到内存中。这种方法可以减少到数据库服务器的网络流量并节省内存。 因此,您可以使用下一个:

List<Object[]> results = 
entity.createQuery("select clientID, clientName from Client").getResultList();

这是“客户端”上的投影查询,它只返回clientID和clientName,而不是将完整对象加载到内存中。这种方法可以减少到数据库服务器的网络流量并节省内存。 因此,您可以使用下一个:

List<Object[]> results = 
entity.createQuery("select clientID, clientName from Client").getResultList();

基本JPA教程的可能副本将解释如何使用JPQL获取实体对象列表。因为您是JPA新手,所以这应该是您的第一个关注点。基本JPA教程的可能副本将解释如何使用JPQL获取实体对象列表。既然你是JPA的新手,那应该是你的第一个拜访点。