I get a list from my query.list()
. After that, I want to display the object inside this list but I got this error for this line for(Commerce[] c: this.newsList) {
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.model.Commerce; com.action.CommerceAction.searchCommerces(CommerceAction.java:35)
Here is my code:
My business service
public List<Commerce[]> getCommercesBySearch(String categorie, String lieux) {
Query query = hibUtil.getSession().createQuery("from Commerce c, Categorie ca,Lieux l "
+ "where c.categorie=ca.idCategorie and c.lieux=l.idLieux and"
+ " ca.libelle='" + categorie + "' and l.ville='" + lieux + "' ");
List<Commerce[]> tuples = (List<Commerce[]>) query.list();
return tuples;
}
My action class
private CommerceService service;
private Commerce commerce = new Commerce();
private List<Commerce[]> newsList;
public String searchCommerces() {
String libelle = ServletActionContext.getRequest().getParameter("libelle");
String ville = ServletActionContext.getRequest().getParameter("ville");
this.newsList = service.getCommercesBySearch(libelle,ville);
for(Commerce[] c: this.newsList){
System.out.println(c[0].getNom());
}
if (this.newsList.size() > 0) {
return SUCCESS;
}
addActionError("Il n'existe aucun commerce de cette catégorie dans cette ville");
return ERROR;
}