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

kri*_*Ram 0 java spring hibernate 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;
    }
Run Code Online (Sandbox Code Playgroud)

客户类别为

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

    }
}
Run Code Online (Sandbox Code Playgroud)

Bon*_*cio 5

通常,在Hibernate上,您只需选择特定的实体,而不必定义所需的列。像这样:

List<Client> clients = entity.createQuery("select c from Client c", Client.class).getResultList();
Run Code Online (Sandbox Code Playgroud)

您收到TypedQuery错误是因为EntityManager正在等待一组Client,但是您选择了它的两个特定列,这将使Hibernate无法将结果转换为Client实体。

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