java.lang.ClassCastException: com.imooc.model.Commodity cannot be cast to [Ljava.lang.Object;

来源:8-1 课程总结

阿西莫夫

2018-01-09 22:01

测试代码如下

@Test

public void testWhere3() {

String hql = "from Commodity c";

Query query = session.createQuery(hql);

List<Object[]> list = query.list();

for (Object[] objects : list) {

System.out.println("id:" + objects[0]);

}

}


实体类代码:

package com.imooc.model;


import java.io.Serializable;


public class Commodity implements Serializable {

private Long id;//主键

private String name;//名称

private Double price;//价格

private String unit;//单位

private String category;//类别

private String description;//简介

private Seller seller;//商家

public Long getId() {

return id;

}

public void setId(Long id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Double getPrice() {

return price;

}

public void setPrice(Double price) {

this.price = price;

}

public String getUnit() {

return unit;

}

public void setUnit(String unit) {

this.unit = unit;

}

public String getCategory() {

return category;

}

public void setCategory(String category) {

this.category = category;

}

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

public Seller getSeller() {

return seller;

}

public void setSeller(Seller seller) {

this.seller = seller;

}

@Override

public String toString() {

return "Commodity [id=" + id + ", name=" + name + ", price=" + price + ", unit=" + unit + ", category="

+ category + ", description=" + description + ", seller=" + seller + "]";

}

public Commodity() {

super();

}

public Commodity(Long id, String name, Double price, String unit, String category, String description,

Seller seller) {

super();

this.id = id;

this.name = name;

this.price = price;

this.unit = unit;

this.category = category;

this.description = description;

this.seller = seller;

}

public Commodity(String name) {

super();

this.name = name;

}

}


写回答 关注

3回答

  • qq_纯粹_6
    2018-01-10 02:09:23
    已采纳

    这个老师已经讲过了。只有一个数据时使用List<Object> list = query.list();多个数据才可以使用你所写的数组形式,这是它本身的一个机制。记住就行了


  • qq_纯粹_6
    2018-01-09 22:28:23

    你创建的实体类类型跟Object不匹配,没法转换。

    第四行改为Query query = session.createSQLQuery(hql).addEntity(Stock.class);

  • 阿西莫夫
    2018-01-09 22:26:29

    经过几次试验,发现不能查询单个字段,改成查多个字段,成功了

    String hql = "select c.name,c.price from Commodity c";

    Query query = session.createQuery(hql);

    List<Object[]> list = query.list();

    for (Object[] objects : list) {

    System.out.println("name:" + objects[0]);

    System.out.println("price:" + objects[1]);

    }

    ---------------------------------------执行结果----------------------------

    Hibernate: 

        select

            commodity0_.NAME as col_0_0_,

            commodity0_.PRICE as col_1_0_ 

        from

            COMMODITY commodity0_

    name:玩具人偶

    price:1500.0

    name:鸭脖

    price:15.0

    name:腊肠

    price:200.0


HQL数据查询基础

使用HQL从数据库中找到你要的数据,掌握了使用Hibernate

41151 学习 · 90 问题

查看课程