Show generated SQL in toplink in eclipse

强颜欢笑 提交于 2020-01-15 06:56:06

问题


I am using EclipseLink libraries in eclipse (at dev time) and deploy on TopLink, I need to show the generated sql statement.

I am using the following persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="myPUnit" transaction-type="JTA">
        <provider>
            oracle.toplink.essentials.PersistenceProvider
        </provider>
        <jta-data-source>jdbc/dcds</jta-data-source>
        <properties>
            <property name="toplink.cache.shared.default" value="false"/>
            <property name="toplink.logging.level" value="FINE" />
        </properties>
    </persistence-unit>
</persistence>

I know it should show generated sql statements, but this is not the case.


回答1:


To see the SQL for a JPA Query you can enable logging on FINE or lower.

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

Session session = em.unwrap(JpaEntityManager).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.

Session session = em.unwrap(JpaEntityManager).getActiveSession();
DatabaseQuery databaseQuery = ((EJBQueryImpl)query).getDatabaseQuery();
String sqlString = databaseQuery.getTranslatedSQLString(session, recordWithValues);

Source: How to get the SQL for a Query




回答2:


As a workaround, find generated SQLs here: app_serv_home\j2ee\home\log\oc4j\log.xml see: http://m-hewedy.blogspot.com/2010/11/workaround-to-find-generated-sql-on-oas.html



来源:https://stackoverflow.com/questions/4255541/show-generated-sql-in-toplink-in-eclipse

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