简体   繁体   English

测试休眠查询

[英]test Hibernate queries

I'm looking for a simple way to test Hibernate HQL criteria queries. 我正在寻找一种测试Hibernate HQL条件查询的简单方法。 I've tried using IntelliJ's Hibernate Console support, but I've run into problems . 我尝试使用IntelliJ的Hibernate Console支持,但是遇到了问题

Is there a standalone tool that provides a simple way to test HQL queries? 是否有一个独立的工具提供一种简单的方法来测试HQL查询? A simple console program that creates the session factory and executes a query passed as an argument would suffice. 一个简单的控制台程序就足够了,它创建会话工厂并执行作为参数传递的查询。

您可以使用H2 (内存中的JDBC)数据库以及容器的码头 ,创建一个容器和上下文,这些容器和上下文将在构建的单元测试阶段(或从JUnit)执行休眠查询。

您可以尝试使用JUnit方法。

Using a console would be my first choice for just playing with HQL (eg Hibernate Tools for eclipse). 仅使用HQL(例如,用于Eclipse的Hibernate Tools )时,使用控制台将是我的首选。 But if that doesn't work, I would just use JUnit. 但是,如果那不起作用,我将只使用JUnit。 My team uses that strategy to test the HQL queries that we use in production code, and occasionally to help write queries in the first place. 我的团队使用该策略来测试我们在生产代码中使用的HQL查询,并偶尔帮助编写查询。

The test setup involves setting up an in-memory database (we use HSQLDB , but there's others). 测试设置涉及建立内存数据库(我们使用HSQLDB ,但还有其他数据库)。 Insert data either with Hibernate or with raw SQL. 使用Hibernate或原始SQL插入数据。 Then configure a Hibernate SessionFactory to connect to it, and run your HQL. 然后配置一个Hibernate SessionFactory连接到它,并运行您的HQL。

We also use this to test other kinds of Hibernate settings or behaviors, and has the side benefit of being a full test suite on Hibernate for our purposes so that we can upgrade and have confidence that nothing we need has changed in an unexpected way. 我们还使用它来测试其他类型的Hibernate设置或行为,并具有为我们的目的而成为完整的Hibernate测试套件的好处,以便我们可以升级并确信我们所需的一切都不会以意想不到的方式发生变化。

I doubt it's possible to have such a tool as a separate app, you'll need to specify a hibernate configuration (which might be in Spring Context), and it should be able to find the classes and their HBM files. 我怀疑是否可以将这样的工具作为单独的应用程序使用,您需要指定一个休眠配置(可能在Spring Context中),并且它应该能够找到类及其HBM文件。 Moreover you might use different UserTypes or anything like that from other JARs, so I wouldn't even search for it. 此外,您可能会使用不同的UserType或其他JAR中的类似内容,因此我什至不会搜索它。

For me the best tool is a unit testing framework + debugging facilities of your IDE - you can just stop at some point where you have a session created and do whatever you want in this mode. 对我而言,最好的工具是IDE的单元测试框架+调试功能-您可以停在创建会话的某个位置,然后在此模式下执行任何操作。 In IntelliJ for instance, aside of usual expressions, you can put code fragments during debugging which might help you with Criteria API. 例如,在IntelliJ中,除了常用的表达式外,您可以在调试过程中放置​​代码片段,这可能会帮助您使用Criteria API。

上次我必须执行此类工作时,我使用DbUnit来测试我的数据访问层,在该数据访问层中我将JPA 2.0与Hibernate一起使用。

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

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