mysql - 如何创建 hibernate 不同的查询

标签 mysql hibernate

我已经在网上看了看,并没有真正找到明确的答案。

我有两个表 A 和 B。B 是 A 的子表。我需要根据 A 的一些限制从 B 获取不同属性的列表。

例如:

SQL:

select distinct sirm.attribute
from store_item_received_material sirm
where sirm.store_item_id in (select si.id from store_item si where si.program_id = 9 and si.customer_id = 1 and si.date_processed is not null);

当然,SQL 工作得很好。

现在,我需要在我的项目中运行它。

我正在运行 hibernate 3.3.1。我尝试了以下方法:

@NamedNativeQueries ({
    @NamedNativeQuery (name = "select.distinct.sirm.for.customer.program", query = "select distinct(sirm.attribute) as attribute from store_item_received_material as sirm where sirm.store_item_id in (select si.id from store_item as si where si.customer_id = ? and si.program_id = ? and si.date_processed is not null)")
})

但失败并出现以下错误:

嵌套异常是 org.hibernate.cfg.NotYetImplementedException:尚不支持纯 native 标量查询

所以我尝试了以下方法:

@NamedNativeQueries ({
    @NamedNativeQuery (name = "select.distinct.sirm.for.customer.program", query = "select distinct(sirm.attribute) as attribute from store_item_received_material as sirm where sirm.store_item_id in (select si.id from store_item as si where si.customer_id = ? and si.program_id = ? and si.date_processed is not null)", resultClass=StoreItemReceivedMaterial.class)
})
@SqlResultSetMapping(name = "select.distinct.sirm.for.customer.program", entities=@EntityResult(entityClass = StoreItemReceivedMaterial.class))

但这也不起作用,因为该对象是一个实体对象并且没有 ID 列。

那么,关于如何做到这一点的任何帮助

最佳答案

对于标量查询,您需要使用 @ColumnResult 进行结果集映射:

@NamedNativeQueries ({
    @NamedNativeQuery (name = "select.distinct.sirm.for.customer.program",
        query = "select distinct(sirm.attribute) as attribute from store_item_received_material as sirm where sirm.store_item_id in (select si.id from store_item as si where si.customer_id = ? and si.program_id = ? and si.date_processed is not null)", resultSetMapping = "select.distinct.sirm.for.customer.program" }) 

@SqlResultSetMapping(name = "select.distinct.sirm.for.customer.program",
    columns = @ColumnResult(name = "attribute"))

关于mysql - 如何创建 hibernate 不同的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4999725/

相关文章:

mysql - 问题 无法登录MySQL服务器

java - 如何在oracle中查看由启用hibernate的java程序执行的最近sql命令的事务日志(带有时间和性能信息)

mysql - Full Outer Join 与 Union 重复?

mysql - 使用 Not 条件在 MySQL 中分组

MySQL触发器正则表达式不工作

java - system.out 之后的 hibernate 代理对象

java - 转换现有的 spring 项目以使用 roo help

java - PostgreSQL - 用户缺乏权限或找不到对象

java - 在 Hibernate 中连接表

c# - MySQL 到数组到 MSSQL 查询语句