gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:55:23 25 4
gpt4 key购买 nike

我收到错误“无法使用请求的结果类型为具有多个返回的查询创建 TypedQuery”我尝试返回所有列值。那时应用程序挂起。我需要获取数组列表中的客户端列表。请帮忙,我是 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 正在等待客户端集合,但您选择了其中的两个特定列,这将使 Hibernate 无法将结果转换为客户端实体。

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

关于java - 无法使用请求的结果类型为具有多个返回的查询创建 TypedQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33669774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com