Is it possible to output generated SQL using EclipseLink without having to increase log verbosity?

前端 未结 3 815
迷失自我
迷失自我 2020-12-02 13:36

I want to output the SQL generated by EclipseLink to the console, during development. However, I could only do so using the logging level FINE. I have a complex domain model

3条回答
  •  猫巷女王i
    2020-12-02 14:04

    To get the SQL for a specific Query at runtime you can use the DatabaseQuery API.

    Query query = em.createNamedQuery("findMe"); 
    Session session = em.unwrap(JpaEntityManager.class).getActiveSession(); 
    DatabaseQuery databaseQuery = ((EJBQueryImpl)query).getDatabaseQuery(); 
    databaseQuery.prepareCall(session, new DatabaseRecord());
    
    String sqlString = databaseQuery.getSQLString();
    

    This SQL will contain ? for parameters. To get the SQL translated with the arguments you need a DatabaseRecord with the parameter values.

    DatabaseRecord recordWithValues= new DatabaseRecord();
    recordWithValues.add(new DatabaseField("param1"), "someValue");
    
    String sqlStringWithArgs = 
             databaseQuery.getTranslatedSQLString(session, recordWithValues);
    

    Source: How to get the SQL for a Query

提交回复
热议问题