I am getting error "Cannot create TypedQuery for query with more than one return using requested result type" I tried with all columns value returning. That time the application hangs. I need to get list of Client, in arraylist. Please help, I am new to 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;
}
Client class is
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(){
}
}
答案 0 :(得分:3)
Usually on Hibernate you simply make selects of an specific entity, not necessarily defining what columns you want. Something like this:
List<Client> clients = entity.createQuery("select c from Client c", Client.class).getResultList();
You are getting the TypedQuery error because the EntityManager is waiting for a collection of Clients, but instead you are selecting two specific columns of it, which will make Hibernate unable to cast the results as a Client entity.
So in your case, use the query given above and everything should work fine.
答案 1 :(得分:0)
您可以在(List&lt; clients&gt;)
中投射到您的结果中 List<Client> clients = (List<Client>) entity.createQuery("select clientID,clientName from Client", Client.class).getResultList();
答案 2 :(得分:0)
这是对“客户端”的投影查询,仅返回clientID和clientName,而不是将整个对象加载到内存中。这种方法可以减少数据库服务器的网络流量并节省内存。 因此,您可以使用下一个:
scala> var table1 = Seq((11, 25, 2, 0), (42, 20, 10, 0)).toDF("col_1", "col_2", "col_3", "col_4")
table1: org.apache.spark.sql.DataFrame = [col_1: int, col_2: int ... 2 more fields]
scala> table1.show()
+-----+-----+-----+-----+
|col_1|col_2|col_3|col_4|
+-----+-----+-----+-----+
| 11| 25| 2| 0|
| 42| 20| 10| 0|
+-----+-----+-----+-----+
scala> table1.createOrReplaceTempView("table1")
scala> val result = spark.sql(s""" select col_1,
| col_2,
| col_3,
| CASE WHEN col_1 > (col_2 + col_3)
| THEN 5
| ELSE 1
| END as col_4
| from table1 """)
result: org.apache.spark.sql.DataFrame = [col_1: int, col_2: int ... 2 more fields]
scala> result.show(false)
+-----+-----+-----+-----+
|col_1|col_2|col_3|col_4|
+-----+-----+-----+-----+
|11 |25 |2 |1 |
|42 |20 |10 |5 |
+-----+-----+-----+-----+
此结果集包含一个对象数组列表,每个数组代表一组属性,在这种情况下为clientID和clientName。现在您可以检索到此:
List<Object[]> results =
entity.createQuery("select clientID, clientName from Client").getResultList();