Tool to Execute JPQL Queries?

匿名 (未验证) 提交于 2019-12-03 01:07:01

问题:

Is there any sort of tool available which allows one to execute JPQL queries against a database "directly"? I would like to type JPQL queries directly into a window and execute them.

Of course it would probably require me to do quite a bit of configuration so that it would be aware of my JPA entities, etc., but I guess it could be done... Anyone know of such a tool?

Thanks.

回答1:

Until Eclipse Dali gets a generic JPQL editor you can use Hibernate Tools. Hibernate Tools works with Dali and provides a HQL/JPQL query editor which uses Hibernate to execute the queries.

An alternative would be to use the JPA Query Tool [JQT], an Interactive JPA query editor and runner. It might be closer to what you're looking for (runs as a standalone application).

Update: I removed the tool suggested for NetBeans, the project is inactive and doesn't provide anything.



回答2:

News from 2013: NetBeans 7.3 will have a built-in JPQL query tool:

https://blogs.oracle.com/geertjan/entry/test_jpql_with_netbeans_ide



回答3:

I think I tried all the tools and IDE's.

In the end, I settled with following bit of code...

The queryEditor runs as a UnitTest.

Assuming you already have UnitTests, you avoid the hassle of configuring PersistenceContexts, DataSources, Drivers, libraries, etc... It also alows you to pass Entity instances as parameters.

And when you're done, copy the query-String to a @NamedParameter definition and set the @Test to enable=false.

import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.testng.annotations.Test;  import javax.persistence.Query; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set;  public class TestGeneric extends AbstractModelTest {      private static final Logger logger = LoggerFactory.getLogger(TestGeneric.class.getName());      /**      * This is not a test.  Just a convenience method to edit queries      */     @Test(enabled = true)     public void queryEditor() throws Exception {         String query = "SELECT mpe " +                        "  FROM ActiveProduct apt JOIN apt.supportedProduct spt JOIN apt.account act JOIN act.merchantProfile mpe" +                        " WHERE mpe.id = :mpeId ";         Class resultClass = MerchantProfile.class;         Map parameters = new HashMap();         parameters.put("mpeId", 1L);         performQuery(query, resultClass, parameters);     }      private  void performQuery(String jplQuery, Class type, Map parameters) throws Exception {         Query query = this.em.createQuery(jplQuery, type);          Set> rawParameters = parameters.entrySet();         for (Map.Entry entry : rawParameters) {             query.setParameter(entry.getKey(), entry.getValue());         }         List resultList = query.getResultList();          if (resultList.size() > 0) {             int count = 0;             StringBuffer resultString;             for (Object o : resultList) {                 resultString = new StringBuffer(++count + " - ");                 dumpObject(o, resultString);                 logger.info(resultString.toString());             }         } else {             logger.info("Empty result list");         }     }      private void dumpObject(Object o, StringBuffer resultString) throws Exception {         if (o == null) {             resultString.append("NULL");         } else if (o instanceof Object[]) {             Object[] row = (Object[]) o;             resultString.append("[");             for (int i = 0; i 


回答4:

Looks like IntelliJ 10 will complement the HSQL editor with an JPQL editor. (Assuming that branch 96.xxx is version 10)



回答5:

You might want to try Vestigo. It is a query tool & browser supporting both JDO (JDOQL) and JPA (JPQL) and can be used stand-alone or installed into your Eclipse IDE as a plug-in.



回答6:

Did you check whether NetBeans, Eclipse or IntelliJ do what you want?

Especially with the Hibernate Plugin for Eclipse, this should be possible (if you use Hibernate as your JPA Backend).



回答7:

You also have Hibernate Tool Suite, which is based on NetBeans



回答8:

My google-fu has come up with a working link for Jpa Query Tool: http://www.f1cd.ru/soft/base/j/jpa_query_tool/jpa_query_tool_06/jqt-0.6b.zip



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