http://jdevelopment.nl/hibernates-pure-native-scalar-queries-supported/
In JPA one can define JPQL queries as well as native queries. Each of those can return either an Entity or one or more scalar values. Queries can be created on demand at run-time from a String, or at start-up time from an annotation (or corresponding XML variant seeWhere to put named queries in JPA?).
Of all those combinations, curiously Hibernate has never supported named native queries returning a scalar result, including insert, update and delete queries which all don’t return a result set, but merely the number of rows affected.
It’s a curious case, since Hibernate does support scalar returns in non-native named queries (thus a scalar return and named queries is not the problem), and it does support scalar returns in dynamically created native queries (thus scalar returns in native queries are not the problem either).
An example of this specific combination:
<named-native-query name="SomeName"> <query> INSERT INTO foo SELECT * FROM bar </query> </named-native-query>
If you do try to startup with such a query, Hibernate will throw an exception with the notorious message:
Pure native scalar queries are not yet supported
Extra peculiar is that this has been reported as a bug nearly 6 years(!) ago (see HHH-4412). In that timespan the advances in IT have been huge, but apparently not big enough to be able to fix this particular bug. “Not yet” certainly is a relative term in Hibernate’s world.
A quick look at Hibernate’s source-code reveals that the problem is withinorg.hibernate.cfg.annotations.QueryBinder, more specifically the bindNativeQuery method. It has the following outline:
public static void bindNativeQuery(org.hibernate.annotations.NamedNativeQuery queryAnn, Mappings mappings){ if (BinderHelper.isEmptyAnnotationValue(queryAnn.name())) { throw new AnnotationException("A named query must have a name when used in class or package level"); } NamedSQLQueryDefinition query; String resultSetMapping = queryAnn.resultSetMapping(); if (!BinderHelper.isEmptyAnnotationValue(resultSetMapping)) { query = new NamedSQLQueryDefinition ( // ... ); } else if (!void.class.equals(queryAnn.resultClass())) { // FIXME should be done in a second pass due to entity name? final NativeSQLQueryRootReturn entityQueryReturn = new NativeSQLQueryRootReturn ( // ... ); } else { throw new NotYetImplementedException( "Pure native scalar queries are not yet supported" ); } }
Apparently, the NamedNativeQuery annotation (or corresponding XML version), should either have a non-empty resultset mapping, or a result class that’s something other than void.
So, isn’t a workaround for this problem perhaps to just provide a resultset mapping, even though we’re not doing a select query? To test this, I tried the following:
<named-native-query name="SomeName" result-set-mapping="dummy"> <query> INSERT INTO foo SELECT * FROM bar </query> </named-native-query> <sql-result-set-mapping name="dummy"> <column-result name="bla" /> </sql-result-set-mapping>
And lo and behold, this actually works. Hibernate starts up and adds the query to its named query repository, and when subsequently executing the query there is no exception and the insert happens correctly.
Looking at the Hibernate code again it looks like this shouldn’t be that impossible to fix. It’s almost as if the original programmer just went out for lunch while working on that code fragment, temporarily put the exception there, and then after lunch completely forgot about it.
Until this has been fixed in Hibernate itself, the result-set-mapping workaround might be useful.
Arjan Tijms
注解方式:
@Entity(name="EmployeeEntity") @Table (name="employee") @SqlResultSetMapping(name="updateResult", columns = { @ColumnResult(name = "count")}) @NamedNativeQueries({ @NamedNativeQuery( name = "updateEmployeeName", query = "UPDATE employee SET firstName = ?, lastName = ? WHERE id = ?" ,resultSetMapping = "updateResult" ) }) public class EmployeeEntity implements Serializable { //entity code }
相关推荐
- 内置对 SMI-S (Storage Management Initiative Specification) 的支持,这使得 Scalar i500 可以被主流的 SRM (Storage Resource Management) 工具发现和管理,例如 EMC ControlCenter。 - 这种集成支持使得用户...
标量查询(Scalar queries) ------------------------- 最基本的 SQL 查询就是获得一个标量(数值)的列表。使用 sess.createSQLQuery("SELECT * FROM CATS").list(); 或者 sess.createSQLQuery("SELECT ID, NAME,...
16.1.1. 标量查询(Scalar queries) 16.1.2. 实体查询(Entity queries) 16.1.3. 处理关联和集合类(Handling associations and collections) 16.1.4. 返回多个实体(Returning multiple entities) 16.1.5. 返回非...
16.1.1. 标量查询(Scalar queries) 16.1.2. 实体查询(Entity queries) 16.1.3. 处理关联和集合类(Handling associations and collections) 16.1.4. 返回多个实体(Returning multiple entities) 16.1.5. 返回非...
16.1.1. 标量查询(Scalar queries) 16.1.2. 实体查询(Entity queries) 16.1.3. 处理关联和集合类(Handling associations and collections) 16.1.4. 返回多个实体(Returning multiple entities) 16.1.5. 返回非...
在OpenCV-Python库中,标量(Scalar)是一个非常基础且重要的概念,它用于表示图像处理中的像素值或各种计算中的数值。本篇将深入探讨这个概念,并结合实际应用场景来解析其工作原理。 首先,标量(Scalar)在数学...
- **Overview**: This introduces the main concepts and components of Hibernate's architecture, including sessions, transactions, and query execution. - **Instance States**: Details about the various ...
16.1.1. 标量查询(Scalar queries) 16.1.2. 实体查询(Entity queries) 16.1.3. 处理关联和集合类(Handling associations and collections) 16.1.4. 返回多个实体(Returning multiple entities) 16.1.4.1. 别名和...
Quantum 的Scalar:registered: 24 对自动加载机已不能满足数据增长需要的IT 部 门具有出色的价值。Scalar 24 是一款设计小巧的双驱动器磁带库,它结合 了企业级产品的特性、性能以及低端产品的支付能力和易用性。...
16.1.1. 标量查询(Scalar queries) 16.1.2. 实体查询(Entity queries) 16.1.3. 处理关联和集合类(Handling associations and collections) 16.1.4. 返回多个实体(Returning multiple entities) 16.1.5. 返回非...
10.4.4. Queries in native SQL 10.5. Modifying persistent objects 10.6. Modifying detached objects 10.7. Automatic state detection 10.8. Deleting persistent objects 10.9. Replicating object between two...
在MATLAB编程环境中,"scalar2colormap"是一个自定义函数,用于将灰度图像转换为具有颜色条的RGBA图像。这个过程涉及到图像处理和可视化技术,是MATLAB中的一个关键应用领域。以下是对该主题的详细解释: 1. **颜色...
Quantum:registered: 的Scalar:registered: 10K 是一台整合企业级备份的SAN 磁带库,能 够使IT 部门将存储整合在易于管理的单一系统中。它是业界第一台按需提 供容量磁带库,提供内置的容量扩展空间,并能使用户在...
如果SQL查询返回的结果不匹配任何实体类,我们需要显式指定结果集的映射,使用`<return-scalar>`标签。 5. **事务处理**:执行SQL查询通常涉及数据库事务,别忘了在适当的地方进行事务管理。在Hibernate中,可以...
第二层缓存(The Second Level Cache)s 14.3.1. 对映射(Mapping)缓冲 14.3.2. 策略:只读缓存 14.3.3. 策略:读/写缓存 14.3.4. 策略:不严格的读/写缓存 14.3.5. 策略:事务缓存(transactional) ...
Scalar::List::Utils是Perl语言中的一个核心模块,它提供了大量用于处理标量和列表的实用函数,极大地丰富了Perl的内置功能。这个模块在Perl社区中被广泛使用,是编写高效、简洁Perl代码的重要工具。`Scalar-List-...
### SNMP的Table与Scalar合并程序解析 #### 一、引言 简单网络管理协议(Simple Network Management Protocol,SNMP)是一种广泛应用于网络设备管理和监控的标准协议。在SNMP的应用场景中,经常需要处理两种类型的...
16. Native SQL查询 16.1. 创建一个基于SQL的Query 16.2. 别名和属性引用 16.3. 命名SQL查询 16.3.1. 使用return-property来明确地指定字段/别名 16.3.2. 使用存储过程来查询 16.3.2.1. 使用存储过程的规则和限制 ...
根据提供的文件信息,可以理解为需要生成有关于“Scalar_24磁带机”的用户手册知识点。尽管文件中并没有具体的内容详细介绍,但根据标题和描述,我们依然可以总结出一些关于磁带机的基础知识点,以及该设备可能涉及...
16.1.1. 标量查询(Scalar queries) 16.1.2. 实体查询(Entity queries) 16.1.3. 处理关联和集合类(Handling associations and collections) 16.1.4. 返回多个实体(Returning multiple entities) 16.1.5. 返回非...