`

Hibernate’s “Pure native scalar queries are not yet supported”

 
阅读更多

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
}
分享到:
评论

相关推荐

    在 ASP .NET Core 9.0 中使用 Scalar 创建漂亮的 API 文档

    在ASP.NET Core 9.0中使用Scalar,开发者可以轻松地集成和配置Scalar,生成高质量的API文档。这不仅包括API的描述、请求和响应的格式,还支持更高级的功能,比如示例请求、版本管理以及基于权限的访问控制。开发者...

    在 .NET 9.0 Web API 中实现 Scalar 接口文档及JWT集成

    在.NET 9.0 Web API中集成Scalar接口文档和JWT是一项非常实用的技术实践,它允许开发者在构建Web API的同时,也能够提供一个详尽的接口文档,并且通过JWT(JSON Web Tokens)来实现安全的用户认证机制。随着Swagger...

    Quantum Scalar i500 介绍

    - 内置对 SMI-S (Storage Management Initiative Specification) 的支持,这使得 Scalar i500 可以被主流的 SRM (Storage Resource Management) 工具发现和管理,例如 EMC ControlCenter。 - 这种集成支持使得用户...

    在 .net 9 中如何重新添加Swagger或改用Scalar

    在.NET 9环境中重新添加Swagger或采用Scalar的步骤 在.NET 9中,由于ASP.NET Core团队的决定,原先内置的Swagger支持(Swashbuckle)被移除了。开发者们若要继续使用类似功能或者寻求更加先进的API文档工具,可以...

    Native SQL查询 (使用SQLQuery).doc

    标量查询(Scalar queries) ------------------------- 最基本的 SQL 查询就是获得一个标量(数值)的列表。使用 sess.createSQLQuery("SELECT * FROM CATS").list(); 或者 sess.createSQLQuery("SELECT ID, NAME,...

    Hibernate+中文文档

    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. 返回非...

    snmp的Table和Scalar的合并程序

    ### SNMP的Table与Scalar合并程序解析 #### 一、引言 简单网络管理协议(Simple Network Management Protocol,SNMP)是一种广泛应用于网络设备管理和监控的标准协议。在SNMP的应用场景中,经常需要处理两种类型的...

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

    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. 返回非...

    HibernateAPI中文版.chm

    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是什么.rar

    在OpenCV-Python库中,标量(Scalar)是一个非常基础且重要的概念,它用于表示图像处理中的像素值或各种计算中的数值。本篇将深入探讨这个概念,并结合实际应用场景来解析其工作原理。 首先,标量(Scalar)在数学...

    hibernate_reference.pdf

    - **Overview**: This introduces the main concepts and components of Hibernate's architecture, including sessions, transactions, and query execution. - **Instance States**: Details about the various ...

    Hibernate 中文 html 帮助文档

    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. 别名和...

    .NET 9 彻底改变了 API 的文档:从 Swashbuckle 到 Scalar

    Scalar在功能上相较于Swashbuckle有显著的改进和优化,它不仅可以帮助开发人员生成更为详细和准确的API文档,还提供了更加友好的用户界面和交互方式。 在.NET 9中,从Swashbuckle过渡到Scalar的决策,不仅影响了...

    Scalar 24 磁带库产品手册

    Quantum 的Scalar:registered: 24 对自动加载机已不能满足数据增长需要的IT 部 门具有出色的价值。Scalar 24 是一款设计小巧的双驱动器磁带库,它结合 了企业级产品的特性、性能以及低端产品的支付能力和易用性。...

    Hibernate中文详细学习文档

    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. 返回非...

    Hibernate Reference Documentation3.1

    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

    在MATLAB编程环境中,"scalar2colormap"是一个自定义函数,用于将灰度图像转换为具有颜色条的RGBA图像。这个过程涉及到图像处理和可视化技术,是MATLAB中的一个关键应用领域。以下是对该主题的详细解释: 1. **颜色...

    Scalar 10K磁带库产品手册

    Quantum:registered: 的Scalar:registered: 10K 是一台整合企业级备份的SAN 磁带库,能 够使IT 部门将存储整合在易于管理的单一系统中。它是业界第一台按需提 供容量磁带库,提供内置的容量扩展空间,并能使用户在...

    Hibernate调用配置文件中的sql语句

    如果SQL查询返回的结果不匹配任何实体类,我们需要显式指定结果集的映射,使用`&lt;return-scalar&gt;`标签。 5. **事务处理**:执行SQL查询通常涉及数据库事务,别忘了在适当的地方进行事务管理。在Hibernate中,可以...

    hibernate 教程

    第二层缓存(The Second Level Cache)s 14.3.1. 对映射(Mapping)缓冲 14.3.2. 策略:只读缓存 14.3.3. 策略:读/写缓存 14.3.4. 策略:不严格的读/写缓存 14.3.5. 策略:事务缓存(transactional) ...

Global site tag (gtag.js) - Google Analytics