0

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

    }
}
krishna Ram
  • 439
  • 2
  • 8
  • 18
  • 2
    Possible duplicate of [Error: Cannot create TypedQuery for query with more than one return](http://stackoverflow.com/questions/10807496/error-cannot-create-typedquery-for-query-with-more-than-one-return) – Szarpul Nov 12 '15 at 12:05
  • 1
    A basic JPA tutorial would explain how to get a list of an entity objects, using JPQL. Since you are new to JPA, that should be your first point of call. – Neil Stockton Nov 12 '15 at 13:28

3 Answers3

5

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.

Bonifacio
  • 1,362
  • 12
  • 18
0

You can cast to your result in (List< clients >)

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

0

That is a projection query on the "client" thay only return clientID and clientName, instead of loading the full object to memory. This approach can allow to reduce network traffic to the database server and save memory. So, you can use the next one:

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

This result set contains a List of Object arrays and each array represents one set of properties, in this case clientID and clientName. Now you can retrieved this:

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