簡體   English   中英

無法使用請求的結果類型為返回多個以上查詢的查詢創建TypedQuery

[英]Cannot create TypedQuery for query with more than one return using requested result type

我收到錯誤消息“無法使用請求的結果類型為多個返回的查詢創建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(){

    }
}

通常,在Hibernate上,您只需選擇特定的實體,而不必定義所需的列。 像這樣:

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

您收到TypedQuery錯誤是因為EntityManager正在等待一組Client,但是您選擇了其中的兩個特定列,這將使Hibernate無法將結果轉換為Client實體。

因此,在您的情況下,請使用上面給出的查詢,一切都應正常工作。

您可以在(List <客戶>)中強制轉換為結果

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

那是在“客戶端”通道上的投影查詢,僅返回clientID和clientName,而不是將整個對象加載到內存中。 這種方法可以減少數據庫服務器的網絡流量並節省內存。 因此,您可以使用下一個:

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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM