获得Hibernate Criteria的原生SQL

2023-10-20 07:08

本文主要是介绍获得Hibernate Criteria的原生SQL,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

对于Hibernate QBC查询,有时候我们会遇到需要获得最终SQL的需求,但是Hibernate本身并没有提供该方法。

虽然我们可以使用诸如p6spy之类的第三方组件来输出实际SQL日志,但是那是通过拦截dataSource-SQL实现的。我们仍然无法在程序内部获得最终实际的SQL代码。

那么如何在程序中获得SQL呢。其实有一个方法,那就是模拟一遍Hibernate中Criteria生成SQL的全部过程。

此处提供一个方法,用于通过criteria获取实际SQL:

public static String getCriteriaSql(Criteria criteria) {CriteriaImpl criteriaImpl = (CriteriaImpl) criteria;//转型SessionImplementor session = criteriaImpl.getSession();//获取SESSIONSessionFactoryImplementor factory = session.getFactory();//获取FACTORYCriteriaQueryTranslator translator = new CriteriaQueryTranslator(factory, criteriaImpl, criteriaImpl.getEntityOrClassName(), CriteriaQueryTranslator.ROOT_SQL_ALIAS);String[] implementors = factory.getImplementors(criteriaImpl.getEntityOrClassName());CriteriaJoinWalker walker = new CriteriaJoinWalker((OuterJoinLoadable) factory.getEntityPersister(implementors[0]), translator, factory, criteriaImpl, criteriaImpl.getEntityOrClassName(), session.getEnabledFilters());return walker.getSQLString();
}


结果如:

/* criteria query */
select this_.id                     as id166_0_,this_.arrive_station_code    as arrive2_166_0_,this_.arrive_station_name    as arrive3_166_0_,this_.clearing_code          as clearing4_166_0_,this_.clearing_name          as clearing5_166_0_,this_.company_type           as company6_166_0_,this_.config_memo            as config7_166_0_,this_.confirm_count          as confirm8_166_0_,this_.confirm_date           as confirm9_166_0_,this_.confirm_quantity       as confirm10_166_0_,this_.confirmor              as confirmor166_0_,this_.confirmor_name         as confirmor12_166_0_,this_.cut_count              as cut13_166_0_,this_.cut_qty                as cut14_166_0_,this_.cut_tag                as cut15_166_0_,this_.delivery_code          as delivery16_166_0_,this_.delivery_name          as delivery17_166_0_,this_.dept_code              as dept18_166_0_,this_.dept_name              as dept19_166_0_,this_.dy_reject              as dy20_166_0_,this_.dy_reject_code         as dy21_166_0_,this_.flow_code              as flow22_166_0_,this_.flow_name              as flow23_166_0_,this_.going_code             as going24_166_0_,this_.going_name             as going25_166_0_,this_.is_ocena_shipping      as is26_166_0_,this_.oil_code               as oil27_166_0_,this_.oil_name               as oil28_166_0_,this_.outcontrol_tag         as outcontrol29_166_0_,this_.plan_date              as plan30_166_0_,this_.plan_station_code      as plan31_166_0_,this_.producer               as producer166_0_,this_.producer_date          as producer33_166_0_,this_.producer_name          as producer34_166_0_,this_.quantity               as quantity166_0_,this_.railway_name           as railway36_166_0_,this_.reject                 as reject166_0_,this_.reject_code            as reject38_166_0_,this_.remain_count           as remain39_166_0_,this_.remain_quantity        as remain40_166_0_,this_.remark                 as remark166_0_,this_.rerceiv_unit_code      as rerceiv42_166_0_,this_.rerceiv_unit_name      as rerceiv43_166_0_,this_.return_count           as return44_166_0_,this_.return_quantity        as return45_166_0_,this_.special                as special166_0_,this_.start_station_code     as start47_166_0_,this_.start_station_name     as start48_166_0_,this_.start_station_type     as start49_166_0_,this_.status                 as status166_0_,this_.storage_point_code     as storage51_166_0_,this_.storage_point_name     as storage52_166_0_,this_.suggestion_end_date    as suggestion53_166_0_,this_.tank_count             as tank54_166_0_,this_.target_code            as target55_166_0_,this_.target_name            as target56_166_0_,this_.transport_type_code    as transport57_166_0_,this_.transport_type_name    as transport58_166_0_,this_.uninstall_oiltank_code as uninstall59_166_0_,this_.uninstall_oiltank_name as uninstall60_166_0_,this_.verifier               as verifier166_0_,this_.verifier_name          as verifier62_166_0_,this_.verify_date            as verify63_166_0_from p_station_multiple this_where this_.company_type = ?and this_.plan_date = ?and this_.target_code = ?



 

这篇关于获得Hibernate Criteria的原生SQL的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/245501

相关文章

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

MySQL 内存使用率常用分析语句

《MySQL内存使用率常用分析语句》用户整理了MySQL内存占用过高的分析方法,涵盖操作系统层确认及数据库层bufferpool、内存模块差值、线程状态、performance_schema性能数据... 目录一、 OS层二、 DB层1. 全局情况2. 内存占js用详情最近连续遇到mysql内存占用过高导致

Mysql中设计数据表的过程解析

《Mysql中设计数据表的过程解析》数据库约束通过NOTNULL、UNIQUE、DEFAULT、主键和外键等规则保障数据完整性,自动校验数据,减少人工错误,提升数据一致性和业务逻辑严谨性,本文介绍My... 目录1.引言2.NOT NULL——制定某列不可以存储NULL值2.UNIQUE——保证某一列的每一

解密SQL查询语句执行的过程

《解密SQL查询语句执行的过程》文章讲解了SQL语句的执行流程,涵盖解析、优化、执行三个核心阶段,并介绍执行计划查看方法EXPLAIN,同时提出性能优化技巧如合理使用索引、避免SELECT*、JOIN... 目录1. SQL语句的基本结构2. SQL语句的执行过程3. SQL语句的执行计划4. 常见的性能优

SQL Server 中的 WITH (NOLOCK) 示例详解

《SQLServer中的WITH(NOLOCK)示例详解》SQLServer中的WITH(NOLOCK)是一种表提示,等同于READUNCOMMITTED隔离级别,允许查询在不获取共享锁的情... 目录SQL Server 中的 WITH (NOLOCK) 详解一、WITH (NOLOCK) 的本质二、工作

MySQL 强制使用特定索引的操作

《MySQL强制使用特定索引的操作》MySQL可通过FORCEINDEX、USEINDEX等语法强制查询使用特定索引,但优化器可能不采纳,需结合EXPLAIN分析执行计划,避免性能下降,注意版本差异... 目录1. 使用FORCE INDEX语法2. 使用USE INDEX语法3. 使用IGNORE IND

SQL Server安装时候没有中文选项的解决方法

《SQLServer安装时候没有中文选项的解决方法》用户安装SQLServer时界面全英文,无中文选项,通过修改安装设置中的国家或地区为中文中国,重启安装程序后界面恢复中文,解决了问题,对SQLSe... 你是不是在安装SQL Server时候发现安装界面和别人不同,并且无论如何都没有中文选项?这个问题也

2025版mysql8.0.41 winx64 手动安装详细教程

《2025版mysql8.0.41winx64手动安装详细教程》本文指导Windows系统下MySQL安装配置,包含解压、设置环境变量、my.ini配置、初始化密码获取、服务安装与手动启动等步骤,... 目录一、下载安装包二、配置环境变量三、安装配置四、启动 mysql 服务,修改密码一、下载安装包安装地