获得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的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq

java.sql.SQLTransientConnectionException连接超时异常原因及解决方案

《java.sql.SQLTransientConnectionException连接超时异常原因及解决方案》:本文主要介绍java.sql.SQLTransientConnectionExcep... 目录一、引言二、异常信息分析三、可能的原因3.1 连接池配置不合理3.2 数据库负载过高3.3 连接泄漏

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

MySQL中On duplicate key update的实现示例

《MySQL中Onduplicatekeyupdate的实现示例》ONDUPLICATEKEYUPDATE是一种MySQL的语法,它在插入新数据时,如果遇到唯一键冲突,则会执行更新操作,而不是抛... 目录1/ ON DUPLICATE KEY UPDATE的简介2/ ON DUPLICATE KEY UP

MySQL分库分表的实践示例

《MySQL分库分表的实践示例》MySQL分库分表适用于数据量大或并发压力高的场景,核心技术包括水平/垂直分片和分库,需应对分布式事务、跨库查询等挑战,通过中间件和解决方案实现,最佳实践为合理策略、备... 目录一、分库分表的触发条件1.1 数据量阈值1.2 并发压力二、分库分表的核心技术模块2.1 水平分

Python与MySQL实现数据库实时同步的详细步骤

《Python与MySQL实现数据库实时同步的详细步骤》在日常开发中,数据同步是一项常见的需求,本篇文章将使用Python和MySQL来实现数据库实时同步,我们将围绕数据变更捕获、数据处理和数据写入这... 目录前言摘要概述:数据同步方案1. 基本思路2. mysql Binlog 简介实现步骤与代码示例1

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp

MySQL 表空却 ibd 文件过大的问题及解决方法

《MySQL表空却ibd文件过大的问题及解决方法》本文给大家介绍MySQL表空却ibd文件过大的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录一、问题背景:表空却 “吃满” 磁盘的怪事二、问题复现:一步步编程还原异常场景1. 准备测试源表与数据

Mac电脑如何通过 IntelliJ IDEA 远程连接 MySQL

《Mac电脑如何通过IntelliJIDEA远程连接MySQL》本文详解Mac通过IntelliJIDEA远程连接MySQL的步骤,本文通过图文并茂的形式给大家介绍的非常详细,感兴趣的朋友跟... 目录MAC电脑通过 IntelliJ IDEA 远程连接 mysql 的详细教程一、前缀条件确认二、打开 ID

MySQL的配置文件详解及实例代码

《MySQL的配置文件详解及实例代码》MySQL的配置文件是服务器运行的重要组成部分,用于设置服务器操作的各种参数,下面:本文主要介绍MySQL配置文件的相关资料,文中通过代码介绍的非常详细,需要... 目录前言一、配置文件结构1.[mysqld]2.[client]3.[mysql]4.[mysqldum