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

霸气de小男生 提交于 2019-12-17 15:25:09

问题


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 composed of many classes, so much that deployment takes a considerable ammount of time when the log verbosity is on the FINE level, since EclipseLink outputs its analysis of the whole model.

Is there a way to get the SQL without resorting to log level FINE (like Hibernate does)?


回答1:


Put the following properties in your persistence.xml:

 <property name="eclipselink.logging.level.sql" value="FINE"/>
 <property name="eclipselink.logging.parameters" value="true"/>

The latter is helpful, so that the values of the parameter are shown.

An alternative is using log4jdbc or log4jdbc-remix.




回答2:


The log generation for EclipseLink seems quite difficult to set, according to this thread.

It mentions a persistence.xml file with log level you can adapt:

<property name="eclipselink.weaving" value="static" />
<property name="eclipselink.logging.level.sql" value="FINEST" />
<property name="eclipselink.logging.level" value="FINEST" />
<property name="eclipselink.logging.level.cache" value="FINEST" />

But some other settings may be needed.

As Martin documents below, "EclipseLink/Examples/JPA/Logging" documents those properties.




回答3:


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



来源:https://stackoverflow.com/questions/2374395/is-it-possible-to-output-generated-sql-using-eclipselink-without-having-to-incre

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!