2010-11-23 72 views
2

我在Eclipse中(在開發時)使用EclipseLink庫並在TopLink上部署,我需要顯示生成的sql語句。在eclipse中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

要查看SQL的JPA查詢您可以啓用測井精細或更低。

要在運行時獲取特定查詢的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