在eclipse中的toplink中显示生成的SQL

时间:2010-11-23 11:25:44

标签: eclipse eclipselink toplink oas show-sql

我在eclipse中使用EclipseLink库(在开发时)并在TopLink上部署,我需要显示生成的sql语句。

我正在使用以下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>

我知道它应该显示生成的sql语句,但事实并非如此。

2 个答案:

答案 0 :(得分:2)

要查看JPA查询的SQL,您可以启用FINE或更低版本的登录。

要在运行时获取特定Query的SQL,可以使用DatabaseQuery API。

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

这个SQL会包含什么?用于参数。要使用参数转换SQL,需要带有参数值的DatabaseRecord。

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

来源:How to get the SQL for a Query

答案 1 :(得分:1)

作为解决方法,请在此处查找生成的SQL: app_serv_home \ J2EE \家\日志\ OC4J \ log.xml 看到: http://m-hewedy.blogspot.com/2010/11/workaround-to-find-generated-sql-on-oas.html