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

前端 未结 8 595
慢半拍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 :

    <property name="eclipselink.logging.parameters" value="true"/>
    <property name="eclipselink.logging.level" value="FINE"/>
    
    0 讨论(0)
  • 2021-01-01 13:26

    For Eclipselink: you can extract the SQL the following way:

    query.unwrap(EJBQueryImpl.class).getDatabaseQuery().getSQLString()
    

    It works only after the query has been executed.

    0 讨论(0)
提交回复
热议问题