`

hibernate hql return object or list 指定类型

    博客分类:
  • java
阅读更多
java.lang.ClassCastException: [Ljava.lang.Object; incompatible with com.shkco.adsr3.cbm.vo.ICustomerAccount

 

看到这错误都知道是数据类型转换不匹配,Object 与ICustomerAccount.

 

DAO 方法原始如下:

	public List<ICustomerAccount> getListOfCustomerAccount(ISessionContext sessionContext, IClientCentricCustomer clientCentricCustomer) {
		
		ISessionWrapper session = getCurrentSession();
		
		String sqlStr = "from " + CustomerAccount.class.getName() 
				+ " as cust join cust.companyGroup as cg where cg.compGrpId = "
				+ ":companyGroupId "
				+ " and cust.ccdRef= :ccdRef ";		
		
		Query query = session.createQuery(sqlStr);

		query.setParameter("companyGroupId", getCompGrpId(sessionContext));
		query.setParameter("ccdRef", clientCentricCustomer);
		
		
		return query.list();
	}

 

没有注意看代码,因为是直接写的from .....没有写select 对象,所以返回的就是Object.

修改如下即可:

加上Select cust,即ICustomerAccount 对象就ok啦。

	public List<ICustomerAccount> getListOfCustomerAccount(ISessionContext sessionContext, IClientCentricCustomer clientCentricCustomer) {
		
		ISessionWrapper session = getCurrentSession();
		
		String sqlStr = "select cust  from " + CustomerAccount.class.getName() 
				+ " as cust join cust.companyGroup as cg where cg.compGrpId = "
				+ ":companyGroupId "
				+ " and cust.ccdRef= :ccdRef ";		
		
		Query query = session.createQuery(sqlStr);

		query.setParameter("companyGroupId", getCompGrpId(sessionContext));
		query.setParameter("ccdRef", clientCentricCustomer);
		
		
		return query.list();
	}

 

 

分享到:
评论

相关推荐

    Hibernate注释大全收藏

    • 属性的类型为 java.sql.Clob or java.sql.Blob, 则映射到 @Lob 对应的类型。 映射主键属性 @Id 注解可将实体Bean中某个属性定义为主键,使用@GenerateValue注解可以定义该标识符的生成策略。 • AUTO - 可以是...

    Hibernate+中文文档

    16.2.1. 使用return-property来明确地指定字段/别名 16.2.2. 使用存储过程来查询 16.3. 定制SQL用来create,update和delete 16.4. 定制装载SQL 17. 过滤数据 17.1. Hibernate 过滤器(filters) 18. XML映射 ...

    hibernate3.2中文文档(chm格式)

    16.2.1. 使用return-property来明确地指定字段/别名 16.2.2. 使用存储过程来查询 16.3. 定制SQL用来create,update和delete 16.4. 定制装载SQL 17. 过滤数据 17.1. Hibernate 过滤器(filters) 18. XML映射 ...

    HibernateAPI中文版.chm

    16.2.1. 使用return-property来明确地指定字段/别名 16.2.2. 使用存储过程来查询 16.3. 定制SQL用来create,update和delete 16.4. 定制装载SQL 17. 过滤数据 17.1. Hibernate 过滤器(filters) 18. XML映射 ...

    Hibernate中文详细学习文档

    16.2.1. 使用return-property来明确地指定字段/别名 16.2.2. 使用存储过程来查询 16.3. 定制SQL用来create,update和delete 16.4. 定制装载SQL 17. 过滤数据 17.1. Hibernate 过滤器(filters) 18. XML映射 ...

    最全Hibernate 参考文档

    16.3.1. 使用return-property来明确地指定字段/别名 16.3.2. 使用存储过程来查询 16.3.2.1. 使用存储过程的规则和限制 16.4. 定制SQL用来create,update和delete 16.5. 定制装载SQL 17. 过滤数据 17.1. Hibernate ...

    Hibernate 中文 html 帮助文档

    16.2.1. 使用return-property来明确地指定字段/别名 16.2.2. 使用存储过程来查询 16.2.2.1. 使用存储过程的规则和限制 16.3. 定制SQL用来create,update和delete 16.4. 定制装载SQL 17. 过滤数据 17.1. Hibernate ...

    Hibernate教程

    17.3.1. 使用return-property来明确地指定字段/别名 17.3.2. 使用存储过程来查询 17.3.2.1. 使用存储过程的规则和限制 17.4. 定制SQL用来create,update和delete 17.5. 定制装载SQL 18. 过滤数据 18.1. ...

    hibernate 体系结构与配置 参考文档(html)

    使用return-property来明确地指定字段/别名 16.3.2. 使用存储过程来查询 16.3.2.1. 使用存储过程的规则和限制 16.4. 定制SQL用来create,update和delete 16.5. 定制装载SQL 17. 过滤数据 17.1. Hibernate 过滤...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    16.2.1. 使用return-property来明确地指定字段/别名 16.2.2. 使用存储过程来查询 16.3. 定制SQL用来create,update和delete 16.4. 定制装载SQL 17. 过滤数据 17.1. Hibernate 过滤器(filters) 18. XML映射 ...

    hibernate3.04中文文档.chm

    17.3.1. 使用return-property来明确地指定字段/别名 17.3.2. 使用存储过程来查询 17.3.2.1. 使用存储过程的规则和限制 17.4. 定制SQL用来create,update和delete 17.5. 定制装载SQL 18. 过滤数据 18.1. ...

    Hibernate3+中文参考文档

    16.3.1. 使用return-property来明确地指定字段/别名 16.3.2. 使用存储过程来查询 16.3.2.1. 使用存储过程的规则和限制 16.4. 定制SQL用来create,update和delete 16.5. 定制装载SQL 17. 过滤数据 17.1. Hibernate ...

    hibernate 框架详解

    使用return-property来明确地指定字段/别名 17.3.2. 使用存储过程来查询 17.3.2.1. 使用存储过程的规则和限制 17.4. 定制SQL用来create,update和delete 17.5. 定制装载SQL 18. 过滤数据 18.1. Hibernate ...

    Hibernate参考文档

    16.2.1. 使用return-property来明确地指定字段/别名 16.2.2. 使用存储过程来查询 16.2.2.1. 使用存储过程的规则和限制 16.3. 定制SQL用来create,update和delete 16.4. 定制装载SQL 17. 过滤数据 17.1. Hibernate ...

    NHibernate参考文档 2.0.0 chm

    14.2.1. 使用return-property来明确地指定字段/别名 14.2.2. 使用存储过程来查询 14.2.2.1. 使用存储过程的规则和限制 14.3. 定制SQL用来create,update和delete 14.4. 定制装载SQL 15. 过滤数据 15.1. NHibernate ...

    NHibernate中文帮组文档(2008.11月更新)

    14.2.1. 使用return-property来明确地指定字段/别名 14.2.2. 使用存储过程来查询 14.2.2.1. 使用存储过程的规则和限制 14.3. 定制SQL用来create,update和delete 14.4. 定制装载SQL 15. 过滤数据 15.1. NHibernate ...

Global site tag (gtag.js) - Google Analytics