简体   繁体   English

如何测试 HQL 查询?

[英]How to test HQL queries?

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?一个好的设置如何让我免于重新部署并启用快速查询检查?

With Intellij IDEA 8.1.3 the mechnism of choice is called 'Facet'.在 Intellij IDEA 8.1.3 中,选择的机制称为“Facet”。 To instantly test HQL queries:立即测试 HQL 查询:

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

sorry for this RTFM question.抱歉这个 RTFM 问题。

You can use hibernate tools in eclipse to run queries.您可以在 Eclipse 中使用休眠工具来运行查询。 This will allow you to run HQL whenever you want to try something.这将允许您在想要尝试某事时运行 HQL。

If you're using IntelliJ, there is Hibero . 如果您使用的是 IntelliJ,则有 Hibero

There is a standalone editor from sun, but I haven't tried it. sun 有一个 独立的编辑器,但我没试过。

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...这是屏幕截图...

在此处输入图片说明

I test my HQL queries in unit-tests with the HSQLDB database.我使用 HSQLDB 数据库在单元测试中测试我的 HQL 查询。 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最好的安德斯

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 .我以前这样做的方法是使用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 eg,首先设置您的数据库以仅包含一组固定的数据,例如,

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.这是您在 JUnit 测试用例的 setup() 方法中要做的事情。

Now let's assume you have a DAO for this entity, and there's a "findProductWithPriceGreaterThan(int)" method.现在让我们假设您有这个实体的 DAO,并且有一个“findProductWithPriceGreaterThan(int)”方法。 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.
}

In eclipse在日食

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM