获得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

相关文章

MyBatis设计SQL返回布尔值(Boolean)的常见方法

《MyBatis设计SQL返回布尔值(Boolean)的常见方法》这篇文章主要为大家详细介绍了MyBatis设计SQL返回布尔值(Boolean)的几种常见方法,文中的示例代码讲解详细,感兴趣的小伙伴... 目录方案一:使用COUNT查询存在性(推荐)方案二:条件表达式直接返回布尔方案三:存在性检查(EXI

Python远程控制MySQL的完整指南

《Python远程控制MySQL的完整指南》MySQL是最流行的关系型数据库之一,Python通过多种方式可以与MySQL进行交互,下面小编就为大家详细介绍一下Python操作MySQL的常用方法和最... 目录1. 准备工作2. 连接mysql数据库使用mysql-connector使用PyMySQL3.

Mysql的主从同步/复制的原理分析

《Mysql的主从同步/复制的原理分析》:本文主要介绍Mysql的主从同步/复制的原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录为什么要主从同步?mysql主从同步架构有哪些?Mysql主从复制的原理/整体流程级联复制架构为什么好?Mysql主从复制注意

如何解决Druid线程池Cause:java.sql.SQLRecoverableException:IO错误:Socket read timed out的问题

《如何解决Druid线程池Cause:java.sql.SQLRecoverableException:IO错误:Socketreadtimedout的问题》:本文主要介绍解决Druid线程... 目录异常信息触发场景找到版本发布更新的说明从版本更新信息可以看到该默认逻辑已经去除总结异常信息触发场景复

MyBatis编写嵌套子查询的动态SQL实践详解

《MyBatis编写嵌套子查询的动态SQL实践详解》在Java生态中,MyBatis作为一款优秀的ORM框架,广泛应用于数据库操作,本文将深入探讨如何在MyBatis中编写嵌套子查询的动态SQL,并结... 目录一、Myhttp://www.chinasem.cnBATis动态SQL的核心优势1. 灵活性与可

MySQL 表的内外连接案例详解

《MySQL表的内外连接案例详解》本文给大家介绍MySQL表的内外连接,结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录表的内外连接(重点)内连接外连接表的内外连接(重点)内连接内连接实际上就是利用where子句对两种表形成的笛卡儿积进行筛选,我

MySQL的ALTER TABLE命令的使用解读

《MySQL的ALTERTABLE命令的使用解读》:本文主要介绍MySQL的ALTERTABLE命令的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、查看所建表的编China编程码格式2、修改表的编码格式3、修改列队数据类型4、添加列5、修改列的位置5.1、把列

Mybatis嵌套子查询动态SQL编写实践

《Mybatis嵌套子查询动态SQL编写实践》:本文主要介绍Mybatis嵌套子查询动态SQL编写方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、实体类1、主类2、子类二、Mapper三、XML四、详解总结前言MyBATis的xml文件编写动态SQL

解决mysql插入数据锁等待超时报错:Lock wait timeout exceeded;try restarting transaction

《解决mysql插入数据锁等待超时报错:Lockwaittimeoutexceeded;tryrestartingtransaction》:本文主要介绍解决mysql插入数据锁等待超时报... 目录报错信息解决办法1、数据库中执行如下sql2、再到 INNODB_TRX 事务表中查看总结报错信息Lock

MySQL启动报错:InnoDB表空间丢失问题及解决方法

《MySQL启动报错:InnoDB表空间丢失问题及解决方法》在启动MySQL时,遇到了InnoDB:Tablespace5975wasnotfound,该错误表明MySQL在启动过程中无法找到指定的s... 目录mysql 启动报错:InnoDB 表空间丢失问题及解决方法错误分析解决方案1. 启用 inno