I have a table fruit with four columns id, name, color, shape.
in the table will be:
1, apple, red, round 2, banana, yellow, long 3, tomato, red, round 4, orange, orange, round
Now I have created the Fruit entity class mapped to the table above.
@Entity @Table(name="fruit") public class Fruit implements Serializable { @Id @Column(name="ID") String id; @Column(name="NAME") String name; @Column(name="COLOR") String color; @Column(name="SHAPE") String shape;
In my DAO class, the code is:
String myQuery = "Select f.shape, f.name from Fruit f where f.shape = :shape"; Query query = this.em.createQuery(myQuery); query.setParameter("shape", "round");
As is obvious, executing the above query will return 3 rows.
I have a simple TO FruitSearchTO class
class FruitSearchTO { String shape; String name;
This TO corresponds to the strings returned by my request.
But something like works in my DAO:
List<FruitSearchTO> fruitList = new ArrayList<FruitSearchTO>(); fruitList = query.getResultList();
throws a java.lang.ClassCastException exception: [Ljava.lang.Object; incompatible with FruitSearchTO]
Where am I mistaken and what is the solution to this?