如何测试HQL查询?

发布于 2024-07-25 09:14:18 字数 156 浏览 3 评论 0原文

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

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

找个人就嫁了吧 2024-08-01 09:14:18

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

如果您使用 IntelliJ,则有 Hibero

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

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

If you're using IntelliJ, there is Hibero.

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

风筝在阴天搁浅。 2024-08-01 09:14:18

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

  1. 创建数据源工具->; 数据源,添加数据源,定义您的开发数据库的驱动程序、用户名和密码,
  2. 以防您还没有 hibernate.cfg 或您以不同于 xml 的方式配置会话工厂:创建引用所有 XML 的 hibernate.cfg 文件 在“项目结构”中定义映射(为会话工厂定义一个名称,只是为了更容易处理)
  3. 将 Facet 添加到您选择的模块,并将最近定义的数据源分配给新的 Facet
  4. 切换到 Java EE 视图
  5. 打开 Hibernate Facets - 节点
  6. 右键单击会话工厂并选择“打开 HQL 控制台”
  7. 在控制台中输入 HQL 查询
    ...你就完成了。

很抱歉回答这个 RTFM 问题。

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

  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.

sorry for this RTFM question.

风流物 2024-08-01 09:14:18

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

我之前完成此操作的方法是使用 JUnitDBUnit

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

例如,

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

Product Name, Price
Acme 100 Series Dynamite, $100
Acme 200 Series Dynamite, $120
Acme Rocket, $500

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

现在假设您有一个针对该实体的 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.
}

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 JUnit and DBUnit.

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.

For instance,

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

Product Name, Price
Acme 100 Series Dynamite, $100
Acme 200 Series Dynamite, $120
Acme Rocket, $500

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

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:

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.
}
墨小墨 2024-08-01 09:14:18

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

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

    final EntityManager entityManager = entityManagerFactory.createEntityManager();

    return (Session)entityManager.getDelegate();

最好的
安德斯

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.

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

    final EntityManager entityManager = entityManagerFactory.createEntityManager();

    return (Session)entityManager.getDelegate();

Best
Anders

紧拥背影 2024-08-01 09:14:18

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

您可以在此处找到代码:https://github.com/maheskrishnan/HQLRunner

这是屏幕截图。 ..

在此处输入图像描述

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

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

here's the screen shot...

enter image description here

池木 2024-08-01 09:14:18

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

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

川水往事 2024-08-01 09:14:18

在 Eclipse 中

  1. 安装 Hibernate 工具(Jboss)
  2. 切换到 hibernate 视角
  3. 打开/单击 Hibernate 配置窗口
  4. Rt 单击窗口并添加配置
  5. Rt 单击窗口单击/打开 HQL 编辑器 输入
  6. 并执行 HQL 查询并在 Hibernate 查询中获取结果结果窗口

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文