Java 如何测试 HQL 查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1043554/
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-11 22:38:30  来源:igfitidea点击:

How to test HQL queries?

javahibernatetestinghqlenvironment

提问by Chris

I'm searching for a fast (really fast) way to test changes to hibernate queries. I have a huge application with thousands of different HQL queries (in XML files) and 100+ mapped classes and i dont want to redeploy the whole application to just test one tiny change to a query.

我正在寻找一种快速(非常快速)的方法来测试对休眠查询的更改。我有一个巨大的应用程序,其中包含数千个不同的 HQL 查询(在 XML 文件中)和 100 多个映射类,我不想重新部署整个应用程序来仅测试对查询的一个微小更改。

How would a good setup look like to free me from redeployment and enable a fast query check?

一个好的设置如何让我免于重新部署并启用快速查询检查?

采纳答案by Chris

With Intellij IDEA 8.1.3 the mechnism of choice is called 'Facet'. To instantly test HQL queries:

在 Intellij IDEA 8.1.3 中,选择的机制称为“Facet”。立即测试 HQL 查询:

  1. create a data source Tools -> Data Source, Add Data Source, define driver, username and password of yor development db
  2. in case you dont have already a hibernate.cfg or you configure your session factory in a different way than via xml: create a hibernate.cfg file referencing all XML mapping's (define a name for the session factory, just for easier handling)
  3. in 'Project Structure' add Facet to your module of choice and assign the recently defined data source to the new facet
  4. switch to Java EE View
  5. Open Hibernate Facets - Node
  6. Right click Session factory and choose "Open HQL Console"
  7. enter HQL query in console ...and your're done.
  1. 创建数据源 Tools -> Data Source, Add Data Source, 定义你的开发数据库的驱动程序、用户名和密码
  2. 如果您还没有 hibernate.cfg 或者您以与通过 xml 不同的方式配置会话工厂:创建一个引用所有 XML 映射的 hibernate.cfg 文件(为会话工厂定义一个名称,只是为了更容易处理)
  3. 在“项目结构”中,将 Facet 添加到您选择的模块中,并将最近定义的数据源分配给新的 facet
  4. 切换到 Java EE 视图
  5. 打开 Hibernate Facets - 节点
  6. 右键单击会话工厂并选择“打开 HQL 控制台”
  7. 在控制台中输入 HQL 查询......你就完成了。

sorry for this RTFM question.

抱歉这个 RTFM 问题。

回答by stevedbrown

You can use hibernate tools in eclipseto run queries. This will allow you to run HQL whenever you want to try something.

您可以在 Eclipse 中使用休眠工具来运行查询。这将允许您在想要尝试某事时运行 HQL。

If you're using IntelliJ, there is Hibero.

如果您使用的是 IntelliJ,则有Hibero

There is a standalone editorfrom sun, but I haven't tried it.

sun有一个独立的编辑器,但我没试过。

回答by anders.norgaard

I test my HQL queries in unit-tests with the HSQLDB database. Just create an entity manager, cast it to a hibernate session and query away.

我使用 HSQLDB 数据库在单元测试中测试我的 HQL 查询。只需创建一个实体管理器,将其转换为休眠会话并进行查询即可。

    final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("tacs-test", props);

    final EntityManager entityManager = entityManagerFactory.createEntityManager();

    return (Session)entityManager.getDelegate();

Best Anders

最好的安德斯

回答by Hyman Leow

You said the quickest way, I'm not sure if you meant the quickest way to get going, or the quickest way to perform ongoing tests, with some initial investment to get the tests implemented. This answer is more the latter.

您说的是最快的方式,我不确定您的意思是最快的开始方式,还是执行持续测试的最快方式,以及一些初始投资来实施测试。这个答案更多是后者。

The way I've done this before was to implement some simple integration testing with JUnitand DBUnit.

我以前这样做的方法是使用JUnitDBUnit实现一些简单的集成测试。

In essence, you'll be using DBUnit to set up your test database with a known and representative set of data, and then plain JUnit to exercise the methods containing your HQL queries, and verify the results.

从本质上讲,您将使用 DBUnit 使用一组已知且具有代表性的数据来​​设置您的测试数据库,然后使用普通的 JUnit 来执行包含您的 HQL 查询的方法,并验证结果。

For instance,

例如,

Set up your database first to contain only a fixed set of data e.g.,

首先设置您的数据库以仅包含一组固定的数据,例如,

Product Name, Price
Acme 100 Series Dynamite, 0
Acme 200 Series Dynamite, 0
Acme Rocket, 0

This is something you'd do in your JUnit test case's setup() method.

这是您在 JUnit 测试用例的 setup() 方法中要做的事情。

Now let's assume you have a DAO for this entity, and there's a "findProductWithPriceGreaterThan(int)" method. In your test, you'd do something like:

现在让我们假设您有这个实体的 DAO,并且有一个“findProductWithPriceGreaterThan(int)”方法。在您的测试中,您会执行以下操作:

public void testFindProductWithPriceGreaterThanInt() {
    ProductDAO dao = new HibernateProductDAO();
    //... initialize Hibernate, or perhaps do this in setup()

    List products = dao.findProductWithPriceGreaterThan(110);
    assertEquals(2, products.size());
    //... additional assertions to verify the content of the list.
}

回答by Marouane Gazanayi

In the eclipse Market, you can search for JBoss Tools and choose only Hibernate tools from the given list.

在 eclipse Market 中,您可以搜索 JBoss Tools 并从给定列表中仅选择 Hibernate 工具。

回答by Mahes

I wrote a simple tool to test & preview HQL, this is just one java class with main method.

我写了一个简单的工具来测试和预览 HQL,这只是一个带有 main 方法的 java 类。

you can find the code here: https://github.com/maheskrishnan/HQLRunner

你可以在这里找到代码:https: //github.com/maheskrishnan/HQLRunner

here's the screen shot...

这是屏幕截图...

enter image description here

在此处输入图片说明

回答by James

In eclipse

在日食

  1. Install Hibernate tools(Jboss)
  2. Switch to hibernate perpective
  3. Open/click Hibernate Configuration window
  4. Rt Click on the window and Add Configuration
  5. Rt Click on the window click/open HQL editor
  6. Type and execute your HQL queries and get your result in the Hibernate Query result window
  1. 安装 Hibernate 工具(Jboss)
  2. 切换到休眠透视
  3. 打开/单击休眠配置窗口
  4. Rt 单击窗口并添加配置
  5. Rt 单击窗口单击/打开 HQL 编辑器
  6. 键入并执行您的 HQL 查询并在 Hibernate 查询结果窗口中获得您的结果

Follow this link for more info http://docs.jboss.org/tools/OLD/2.0.0.GA/hibernatetools/en/html/plugins.html

点击此链接了解更多信息http://docs.jboss.org/tools/OLD/2.0.0.GA/hibernatetools/en/html/plugins.html