Java 执行 JPQL 查询的工具?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1615494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:51:27  来源:igfitidea点击:

Tool to Execute JPQL Queries?

javajpajpql

提问by Rintoul

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.

是否有任何类型的工具允许人们“直接”对数据库执行 JPQL 查询?我想直接在窗口中输入 JPQL 查询并执行它们。

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?

当然,它可能需要我做相当多的配置,以便它知道我的 JPA 实体等,但我想它可以完成......有人知道这样的工具吗?

Thanks.

谢谢。

回答by shartte

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

您是否检查过 NetBeans、Eclipse 或 IntelliJ 是否按照您的意愿行事?

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

特别是对于 Eclipse 的 Hibernate 插件,这应该是可能的(如果您使用 Hibernate 作为您的 JPA 后端)。

回答by Pascal Thivent

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

在 Eclipse Dali 获得通用 JPQL 编辑器之前,您可以使用Hibernate Tools。Hibernate Tools 与 Dali 一起工作,并提供了一个 HQL/JPQL 查询编辑器,它使用 Hibernate 来执行查询。

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).

另一种方法是使用JPA 查询工具[JQT],一个交互式 JPA 查询编辑器和运行器。它可能更接近您要查找的内容(作为独立应用程序运行)。

alt text

替代文字

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

更新:我删除了为 NetBeans 建议的工具,该项目处于非活动状态并且不提供任何内容。

回答by Miguel Ping

You also have Hibernate ToolSuite, which is based on NetBeans

您还有基于 NetBeans 的Hibernate ToolSuite

回答by Jan

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

我想我尝试了所有的工具和 IDE。

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

最后,我解决了以下代码...

The queryEditor runs as a UnitTest.

queryEditor 作为 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.

假设您已经有了 UnitTests,您就可以避免配置 PersistenceContexts、DataSources、驱动程序、库等的麻烦……它还允许您将 Entity 实例作为参数传递。

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

完成后,将查询字符串复制到@NamedParameter 定义并将@Test 设置为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<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("mpeId", 1L);
        performQuery(query, resultClass, parameters);
    }

    private <T> void performQuery(String jplQuery, Class<T> type, Map parameters) throws Exception {
        Query query = this.em.createQuery(jplQuery, type);

        Set<Map.Entry<String, Object>> rawParameters = parameters.entrySet();
        for (Map.Entry<String, Object> entry : rawParameters) {
            query.setParameter(entry.getKey(), entry.getValue());
        }
        List<T> 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 < row.length; i++) {
                dumpObject(row[i], resultString);
            }
            resultString.append("]");
        } else if (o instanceof Long ||
                   o instanceof Double ||
                   o instanceof String) {
            resultString.append(o.getClass().getName() + ": " + o);
        } else {
            resultString.append(ReflectionToStringBuilder.toString(o, ToStringStyle.SHORT_PREFIX_STYLE));
        }
    }

回答by Jan

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

看起来 IntelliJ 10 将使用 JPQL 编辑器补充 HSQL 编辑器。(假设分支 96.xxx 是版本 10)

回答by Miguel Ping

My google-fuhas 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

google-fu想出了一个Jpa 查询工具的工作链接:http: //www.f1cd.ru/soft/base/j/jpa_query_tool/jpa_query_tool_06/jqt-0.6b.zip

回答by Marco

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.

您可能想尝试Vestigo。它是一个支持 JDO (JDOQL) 和 JPA (JPQL) 的查询工具和浏览器,可以独立使用,也可以作为插件安装到 Eclipse IDE 中。

回答by Ondra ?i?ka

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

2013 年新闻:NetBeans 7.3 将有一个内置的 JPQL 查询工具:

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

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

https://blogs.oracle.com/geertjan/resource/run-jpql-query-4.png

https://blogs.oracle.com/geertjan/resource/run-jpql-query-4.png