欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

query.list()查询转化成实体时 会报转换异常

程序员文章站 2022-04-14 14:31:54
...
原因: hibernate中写了sql :“SELECT o.id AS orderId,o.door_id AS doorId,p.prd_id AS prdId,p.price_id AS priceId,p.num AS purchaseNum FROM store_order o,store_order_prd p WHERE o.status = 0 AND DATE_SUB(NOW(), INTERVAL 1 DAY) >= o.create_time AND p.order_id=o.id AND o.is_delete =0 AND p.is_delete =0”
Query query = this.getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(sql.toString());
在装换中hibernate中先是转化成object,这时 编译和查询都是没有问题,但是在services中应用到这个实体时 就会出现类转换异常的错误。
 
解决办法,查询时 要把查询出来的实体进行强制转化:
语法: return query.setResultTransformer(Transformers.aliasToBean(AddPrdStockVo.class)).list();
 
 
完整列子:
 
public List<AddPrdStockVo> getOrderStockNum() {
String sql = "SELECT o.id AS orderId,o.door_id AS doorId,p.prd_id AS prdId,p.price_id AS priceId,p.num AS purchaseNum FROM store_order o,store_order_prd p WHERE o.status = 0 AND DATE_SUB(NOW(), INTERVAL 1 DAY) >= o.create_time AND p.order_id=o.id AND o.is_delete =0 AND p.is_delete =0";
Query query = this.getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(sql.toString());
return query.setResultTransformer(Transformers.aliasToBean(AddPrdStockVo.class)).list();
}