2

I get a ClassCastException when trying to query my JPA entity class. I only want json to show two columns. That is name and address. How do I only show selected columns in JPA? From debugging it has to be the for loop. So List is the object and I need to make the right side an object instead of a list correct?

Entity

@Entity
@Table(name = "Personnel")
public class User implements Serializable {

private String id;
private String name;
private String address;

public User(String id, String name, String address) 
{
this.id = id;
this.name = name;
this.address = address;
}

@Id
@Column(name = "name", unique = true, nullable = false)
public String getName() {
    return this.name;
}....
 //setters getters

Query/Impl

public List<User> getRecords(User ent){

     String sql = "select "
                + " usr.name, usr.address "                 
                + " from User usr"           
                + " where usr.id = '1' ";

    List<User> records = this.getSession().createQuery(sql).list();
    for ( User event : records ) {
        System.out.println( "Records (" + event.getName + ") );
    }

    return records;
 }

Update

This is my attempt to declare the result object as List. Does the method have to be an object instead of ?

public List<User> getRecords(User ent){
     String sql = "select "
                + " usr.name, usr.address "                 
                + " from User usr"           
                + " where usr.id = '1' ";

       Map<String, String> results = new HashMap<String, String>();
       List<Object[]> resultList = this.getSession().createQuery(sql).list();

       // Place results in map
       for (Object[] items: resultList) {
          results.put((String)items[0], (String)items[1]);
          results.toString();
       }
       return (List<User>) results;
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
Sunny
  • 145
  • 1
  • 3
  • 14

4 Answers4

1

You can pass a DTO class to the SELECT clause like this:

List<UserDTO> resultList = this.getSession().createQuery("""
    select
        new my.package.UserDTO(usr.name, usr.address)
    from User usr
    where usr.id = :userId
    """)
.setParameter("userId", userId)
.getResultList();
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
0

You should read the complete object:

String sql = " from User usr"           
            + " where usr.id = '1' ";

Or declare the result object as List<Object[]>

Jens
  • 67,715
  • 15
  • 98
  • 113
  • Jens, reading the complete object works fine. Thanks. However I need to get specific columns so I'm attempting to declare the result object as List Does this mean I have to change the return type to be an object instead of returning User records? – Sunny Aug 16 '15 at 12:27
  • @Sunny You can also use the List as return type. But you have to map it by your own. – Jens Aug 16 '15 at 17:05
0

You can use generic(I assure you are using java 1.5 and above) and and one you get result, you do type check and downcast to desired type.

java_bee
  • 443
  • 4
  • 5
0

If You don't want to read the complete object you have to use Criteria and Projection on specific properties.

See this answer it will help

Community
  • 1
  • 1
karim mohsen
  • 2,164
  • 1
  • 15
  • 19