gpt4 book ai didi

java - 获取条件查询的 JPQL/SQL 字符串表示形式

转载 作者:行者123 更新时间:2023-12-02 06:09:15 25 4
gpt4 key购买 nike

如何使用参数值获取条件查询的 SQL 字符串表示形式?

我尝试了这个,但它返回一个没有参数值的字符串给我:

String QueryString = 'SELECT * FROM CUSTOMERS WHERE lastname = ?'


query = entityManager.createNativeQuery(QueryString);

query.setParameter(1, "toto");

System.out.print(query.unwrap(JpaQuery.class).getDatabaseQuery().getSQLString());

但返回“SELECT * FROM CUSTOMERS WHERE lastname = ?”而不是“从客户中选择*,其中姓氏 = 'toto'”

最佳答案

感谢@GrootC帮助我找到解决方案。

如果解决方案对其他人有帮助:

String QueryString = 'SELECT * FROM CUSTOMERS WHERE lastname = ?';
Query query = entityManager.createNativeQuery(QueryString);
Session session = entityManager.unwrap(JpaEntityManager.class).getActiveSession();
DatabaseQuery databaseQuery = query.unwrap(org.eclipse.persistence.jpa.JpaQuery.class).getDatabaseQuery();
DatabaseRecord recordWithValues= new DatabaseRecord();

query.setParameter(1, "toto");
recordWithValues.add(new DatabaseField(Integer.toString(1)), "toto");

databaseQuery.prepareCall(session, recordWithValues);
String sqlStringWithArgs = databaseQuery.getTranslatedSQLString(session, recordWithValues);

System.out.print(sqlStringWithArgs);

====SELECT * FROM CUSTOMERS WHERE lastname = 'toto'====

关于java - 获取条件查询的 JPQL/SQL 字符串表示形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55922705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com