2

我在 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 语句,但事实并非如此。

4

2 回答 2

2

要查看 JPA 查询的 SQL,您可以在 FINE 或更低版本上启用日志记录。

要在运行时获取特定查询的 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);

资料来源:如何获取查询的 SQL

于 2012-08-24T06:16:57.433 回答
1

作为一种解决方法,在此处查找生成的 SQL:app_serv_home\j2ee\home\log\oc4j\log.xml 参见: http ://m-hewedy.blogspot.com/2010/11/workaround-to-find-generated-sql- on-oas.html

于 2010-12-10T23:25:43.780 回答