使用hibernate查询product的结果为Object, 并且不能转为Product
2017-01-15 14:59
483 查看
使用hibernate查询product的结果为Object, 并且不能转为Product。
版本:hibernater-5.2.6错误代码:
public List<Product> findByPage(Integer cid, Integer begin, Integer countInPage) { List<Product> products = (List<Product>) this.getHibernateTemplate().execute(new HibernateCallback<List<Product>>() { @Override public List<Product> doInHibernate(Session session) throws HibernateException { Query query = session .createQuery("from Product p join p.categorySecond cs join cs.category c where c.cid=?"); query.setFirstResult(begin); query.setMaxResults(countInPage); query.setParameter(0, cid); return query.getResultList(); } }); return products; }若改为
Query<Product> query = session.createQuery("from Product p join p.categorySecond cs join cs.category c where c.cid=?" ,Product.class);
则报错:Cannot create TypedQuery for query with more than one return
从stackoverflow中查得如下答案:
Without goind into details about how
Mediaand
Bookshould be modeled, I will at least explain why you get this exception.
You're doing:
em.createQuery(someJPQL, Media.class);
This means: create a query using
someJPQL, and this query will return instances of the
Mediaentity.
But your
JPQLis:
SELECT m.title, b.isbn, b.authors ...
So the query does not return entities of type Media. It returns three fields, from two different entities. There is no way your JPA engine could magically create instances of Media from these 3 columns. A query would return instances of Media if it looked
like this:
select m from Media m ...
最后将代码改为如下,解决问题。
Query<Product> query = session.createQuery("select p from Product p join p.categorySecond cs join cs.category c where c.cid=?" ,Product.class); //或 Query<Product> query = session.createQuery("select p from Product p join p.categorySecond cs join cs.category c where c.cid=?");
相关文章推荐
- Hibernate查询,返回new对象(注意这个新定义的类要有构造函数),使用sql带条件分页查询并且把结果显示到一个对象的集里面的解决方案
- Hibernate查询,返回new对象(注意这个新定义的类要有构造函数),使用sql带条件分页查询并且把结果显示到一个对象的集里面的解决方案
- hibernate中使用sql返回结果转为非持久化对象
- hibernate使用hql多表关联查询list转为json报错
- 使用Hibernate criteria进行分页查询时,如何实现一次查询取得总记录数和分页后结果集
- 深入分析使用mysql_fetch_object()以对象的形式返回查询结果
- Hibernate 限制查询数目,解决Limit在hql不能使用问题
- not in 不能使用在子查询结果为null
- Hibernate将sql查询结果中字符转为char类型的原因
- 从Object对象中封装了其它对象,怎样取出呢?(Hibernate查询结果处理)
- hibernate中使用sql返回结果转为非持久化对象
- Hibernate 自定义查询sql 并使用自定义对象接收查询结果
- 深入分析使用mysql_fetch_object()以对象的形式返回查询结果
- 深入分析使用mysql_fetch_object()以对象的形式返回查询结果
- Hibernate之使用sql语句查询多个表,返回多种字段,将返回结果映射到自定义的普通pojo类中
- 在使用Hibernate时,因为一个查询需要更多的表连接而要使用SQL来解决性能问题。然而返回的结果集中包含了没有映射的Entity类中的表字段,在这个SQL中还有使用如何将层次关系的父子结点显示为横行
- 如何在Sql Server上使用一条SQL查询结果总数并且分页
- 【Struts2+Hibernate4】按照MVC思想使用Hibernate查询数据库,并且在前台使用OGNL表达式输出
- Hibernate 限制查询数目,解决Limit在hql不能使用问题
- Hibernate中使用sql查询结果后再封装成实体类的问题