Can I get the SQL string from a JPA Query object?

前端 未结 8 641
慢半拍i
慢半拍i 2021-01-01 12:36

May I know how can I get the sql from a JPA query? or let\'s say, convert the JPA query to a SQL string? Thank you very much!

8条回答
  •  没有蜡笔的小新
    2021-01-01 13:19

    Following Karol's answer - It is possible to retrieve the SQL before executing the statement in EclipseLink :

    Session session = em.unwrap(JpaEntityManager.class).getActiveSession();
    DatabaseQuery databaseQuery = query.unwrap(EJBQueryImpl.class).getDatabaseQuery();
    databaseQuery.prepareCall(session, new DatabaseRecord());
    Record r = databaseQuery.getTranslationRow();
    String bound = databaseQuery.getTranslatedSQLString(session, r);
    String sqlString = databaseQuery.getSQLString();
    

    To retrieve the SQL String During/After execution it is probably best to do so using persistence properties rather than in-code :

    
    
    

提交回复
热议问题