获得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更新某个字段拼接固定字符串的实现

《MySQL更新某个字段拼接固定字符串的实现》在MySQL中,我们经常需要对数据库中的某个字段进行更新操作,本文就来介绍一下MySQL更新某个字段拼接固定字符串的实现,感兴趣的可以了解一下... 目录1. 查看字段当前值2. 更新字段拼接固定字符串3. 验证更新结果mysql更新某个字段拼接固定字符串 -

python连接本地SQL server详细图文教程

《python连接本地SQLserver详细图文教程》在数据分析领域,经常需要从数据库中获取数据进行分析和处理,下面:本文主要介绍python连接本地SQLserver的相关资料,文中通过代码... 目录一.设置本地账号1.新建用户2.开启双重验证3,开启TCP/IP本地服务二js.python连接实例1.

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.

Ubuntu中远程连接Mysql数据库的详细图文教程

《Ubuntu中远程连接Mysql数据库的详细图文教程》Ubuntu是一个以桌面应用为主的Linux发行版操作系统,这篇文章主要为大家详细介绍了Ubuntu中远程连接Mysql数据库的详细图文教程,有... 目录1、版本2、检查有没有mysql2.1 查询是否安装了Mysql包2.2 查看Mysql版本2.

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进

Python3.6连接MySQL的详细步骤

《Python3.6连接MySQL的详细步骤》在现代Web开发和数据处理中,Python与数据库的交互是必不可少的一部分,MySQL作为最流行的开源关系型数据库管理系统之一,与Python的结合可以实... 目录环境准备安装python 3.6安装mysql安装pymysql库连接到MySQL建立连接执行S

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S

Mysql表的简单操作(基本技能)

《Mysql表的简单操作(基本技能)》在数据库中,表的操作主要包括表的创建、查看、修改、删除等,了解如何操作这些表是数据库管理和开发的基本技能,本文给大家介绍Mysql表的简单操作,感兴趣的朋友一起看... 目录3.1 创建表 3.2 查看表结构3.3 修改表3.4 实践案例:修改表在数据库中,表的操作主要

mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)的解决方法

《mysql出现ERROR2003(HY000):Can‘tconnecttoMySQLserveron‘localhost‘(10061)的解决方法》本文主要介绍了mysql出现... 目录前言:第一步:第二步:第三步:总结:前言:当你想通过命令窗口想打开mysql时候发现提http://www.cpp