views:

1947

answers:

5

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?

+4  A: 

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.

stevedbrown
does there exist a solution (=plugin, whatever) for IntelliJ IDEA 8?
Chris
I added a link to Hibero.
stevedbrown
yeah, thxn. if it works how they describe it, it's exactly what i need, but: "Since 1 January 2008 .. Hibero™ have been discontinued for business reasons.", so what now?
Chris
I guess I'll delete that bookmark then, haven't used IntelliJ for a while (not a choice, just a budget).
stevedbrown
I don't have enough rep to edit the post but the correct url to Hibernate Tools is: http://www.hibernate.org/subprojects/tools.html
Jörgen Lundberg
+1  A: 

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

anders.norgaard
A: 

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.
}
Jack Leow
with quick/fast i meant the 'change-test-change-test' ... iteration.
Chris
+4  A: 

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.

Chris
More info about HQL support in IntelliJ is here: http://www.jetbrains.com/idea/features/java_hibernate.html
cliff.meyers
A: 

Try http://code.google.com/p/hibernatemock/

Hql/criteria testing is very simple with HibernateMock

CelinHC