2010-11-23 71 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