Ljava.lang.Object; cannot be cast to……异常

这是因为类型无法转换抛出的异常。
在hibernate框架中,使用了select语句,没有指定返回数据类型,所以默认为Object类,它是所有类的父类。所以当我们遍历list的时候,Object无法转成实体类,便会抛出异常。

**

以下为返回值的四种情况:

**
1、没有select子句有from子句的HQL查询,查询表格的记录,返回的对象是查询类对象;即List<查询类名> list=query.list();

@Query("from UnifiedPo p where p.fileName = ?1")

2、有select子句的HQL查询,查询表格记录,返回的对象是Object[]类对象,即List lsit=query.lsit();

@Query("select p.customerId,p.fileName,p.status from UnifiedPo p where p.fileName = ?1")

3、我们可以通过在HQL语句中使用new list(…),new map(…),的方式来指定查询返回的对象类型。

@Query("select new list(p.customerId,p.fileName,p.status) from UnifiedPo p where p.fileName = ?1")
@Query("select new map(p.customerId as cid,p.fileName as fname,p.status as sta) from UnifiedPo p where p.fileName = ?1")

4、使用new UnifiedPo(…)的方式,即以自定义类型返回的方式,需要在该查询类的持久化类UnifiedPo.java中添加相应的构造函数。

@Query("select new UnifiedPo(p.customerId,p.fileName,p.status) from UnifiedPo p where p.fileName = ?1")

你可能感兴趣的:(异常解决)