java - [Ljava.lang.Object; cannot be cast to error -
i list query.list(). after that, want display object inside list got error line for(commerce[] c: this.newslist) {
java.lang.classcastexception: [ljava.lang.object; cannot cast [lcom.model.commerce; com.action.commerceaction.searchcommerces(commerceaction.java:35)
here 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 , c.lieux=l.idlieux and" + " ca.libelle='" + categorie + "' , 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; }
i statement:
list<commerce[]> tuples = (list<commerce[]>) query.list(); produces unchecked type conversion warning. your code polluting heap doing unchecked type conversion. query.list() returns raw list, contain object[]. here relevant hibernate documentation:
return query results
list. if query contains multiple results per row, results returned in instance ofobject[].
note cannot cast array array of it's sub-type.
there couple of ways fix problem:
- use
list<object[]>instead oflist<commerce[]>. can transform result oflist()method more usable form, preferably within it's own method, before passing on rest of code. preferable method if need select morecommerceobject. - add
select cbeginning of query, allow safelist<commerce>cast.
Comments
Post a Comment