【问题标题】:Cannot create TypedQuery for query with more than one return using requested result type无法使用请求的结果类型为具有多个返回的查询创建 TypedQuery
【发布时间】:2016-02-13 16:17:35
【问题描述】:

我收到错误“无法使用请求的结果类型为具有多个返回的查询创建 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;
    }

客户端类是

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

    }
}

【问题讨论】:

标签: java spring hibernate jpa


【解决方案1】:

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

List<Client> clients = entity.createQuery("select c from Client c", Client.class).getResultList();

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

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

【讨论】:

    【解决方案2】:

    您可以将结果投射到 (List)

    List&lt;Client&gt; clients = (List&lt;Client&gt;) entity.createQuery("select clientID,clientName from Client", Client.class).getResultList();

    【讨论】:

      【解决方案3】:

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

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

      此结果集包含一个对象数组列表,每个数组代表一组属性,在本例中为 clientID 和 clientName。现在你可以检索到这个:

      Object[] o = results.get(0); // for first element!
      Long id = (Long) o[0]; // choose the correct type!
      String name = (String) o[1];
      

      【讨论】:

        猜你喜欢
        • 2021-06-09
        • 2021-12-24
        • 2016-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-11
        • 2016-12-29
        • 2012-10-28
        相关资源
        最近更新 更多