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

时间:2019-04-30 14:04:46

标签: java eclipselink jpql

如何获取具有parmetervalue的条件查询的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'的客户那里选择* *”

1 个答案:

答案 0 :(得分:0)

感谢@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'====