List<Goods> list =getHibernateTemplate().find( "select g.goodsid,g.goodsname,g.introduce,g.image,g.addr," + "gt.typename,g.price,g.time " + "from Goods g,Goodstype gt where g.goodstype = gt.goodstypeid and g.istop=""1"""); System.out.println(list.get(0).getGoodsname()); 错误信息 Struts has detected an unhandled exception: Messages: [Ljava.lang.Object; cannot be cast to domain.Goods
File: dao/TopDAO.java Line number: 18 ——————————————————————————– Stacktraces java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to domain.Goods Goods类 public class Goods { private String goodsid; //作为String是原作者设计不良? private String goodsname; private String introduce; private String image; private String addr; private int goodstypeid; private String typename; private Goodstype goodstype; private String istop; private String isrecomm; private Date time;//最新的更新时间 private String price;//最新的价格 //getter、setter略 } hbm.xml <?xml version=”1.0″ encoding=”UTF-8″?> <!– 指定Hibernate映射文件的DTD信息 –> <!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 3.0//EN” “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd” > <hibernate-mapping package=”domain”> <class name=”Goods” table=”goods”> <!– 读写缓存 –> <cache usage=”read-write”/> <id name=”goodsid” type=”string”/> <property name=”goodsname” type=”string” length=”50″/> <property name=”introduce” type=”string” length=”50″/> <property name=”image” type=”string” length=”50″/> <property name=”addr” type=”string” length=”50″/> <property name=”istop” type=”string” length=”50″/> <property name=”isrecomm” type=”string” length=”50″/> <property name=”price” type=”string” length=”50″/> <property name=”time” type=”date” length=”50″/> <!– 映射和Goodstype的关联关系 –> <many-to-one name=”goodstype” column=”goodstypeid” class=”Goodstype” not-null=”true”/> </class> </hibernate-mapping> 求解答。 |
|
#1 |
改为System.out.println((Goods)list.get(0).getGoodsname());
|
#3 |
List<Goods> list =getHibernateTemplate().find( "from Goods g where g.istop=""1"""); System.out.println(list.get(0).getGoodsname()); 或者 List<Object> list =getHibernateTemplate().find( "select g.goodsid,g.goodsname,g.introduce,g.image,g.addr," + "gt.typename,g.price,g.time " + "from Goods g,Goodstype gt where g.goodstype = gt.goodstypeid and g.istop=""1"""); System.out.println(((Goods)list.get(0)).getGoodsname()); 试试,我觉得你那个查询怎么看有点儿别扭,特别是g.goodstype=gt.goostypeid,你不是已经映射了关系了吗? |
#5 |
回复3楼: 刚学ssh对这个映射掌握不够深。你这么一说貌似确实有点别扭,但删去g.goodstype=gt.goostypeid后依然不行。倒是用你的第一种方法ok。(第二种觉得不行吧。) 原因是什么的呢?困惑。那我想要select而不是直接默认选全部该怎么办呢。谢谢哈 |
#7 |
不知和这个是否类似
Problem is (List<SwitcherServiceSource>) LoadSource.list(); This will return a List of Object arrays (Object[]) with scalar values for each column in the SwitcherServiceSource table. Hibernate will use ResultSetMetadata to deduce the actual order and types of the returned scalar values. Solution List<Object> result = (List<Object>) LoadSource.list(); Iterator itr = result.iterator(); while(itr.hasNextElement()){ Object[] obj = (Object[]) itr.next(); //now you have one array of Object for each row String client = String.valueOf(obj[0]); // don””t know the type of column CLIENT assuming String Integer service = Integer.parseInt(String.valueOf(obj[1])); //SERVICE assumed as int //same way for all obj[2], obj[3], obj[4] } |