Adam Arold
Adam Arold

Reputation: 30548

Is there a non-commercial Hibernate query checker?

We are using Hibernate at my workplace on some project and I had to modify some queryes recently. I found it really cumbersome to modify a query, run an ant smart or ant refresh and see whether my query works. When I asked one of my colleagues he told me that it is the way we use it.

Do you have any idea how can I speed up this process? I'm looking for a tool which can connect to a database (we are using PGSQL) and run my Hibernate query there and show the results without touching ant.

For example I would be able to try this:

@Query(query = "SELECT DISTINCT l FROM Line l, IN(l.workplaces) w WHERE w.workshop.sid=:wsid", params = "wsid")

Upvotes: 1

Views: 165

Answers (2)

Pablo
Pablo

Reputation: 3673

JBoss Tools for eclipse has a HQL editor that you can open from the hibernate perspective, you can test hql queries there.

Upvotes: 1

oers
oers

Reputation: 18704

We have a junit-Test for hibernate which uses the derby database as a in-memory databse. This will create the database in derby with all tables and you should be able to execute the query, to see if it is valid.

We have all queries in the orm.xml, so those queries are already checked when creating the EntityManager.

setup

  private static EntityManagerFactory emf;
  private static EntityManager em;

  @BeforeClass
  public static void before()
  {
    emf = Persistence.createEntityManagerFactory("persistenztest");
    em = emf.createEntityManager();
  }

test

  @Test public void test()
  {
    Query q = em.createQuery(YOUR_QUERY_HERE);
    List<?> list =  q.getResultList();
  }

Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
  xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

  <persistence-unit name="persistenztest" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <mapping-file>orm.xml</mapping-file>
    <properties>
      <property name="hibernate.hbm2ddl.auto" value="create"/>
      <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver" />
      <property name="hibernate.connection.url" value="jdbc:derby:memory:sa;create=true;territory=de_DE;collation=TERRITORY_BASED:SECONDARY;"/>

      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
      <property name="hibernate.cache.use_query_cache" value="false"/>
      <property name="hibernate.cglib.use_reflection_optimizer" value="false" />
    </properties>
  </persistence-unit>

</persistence>

Upvotes: 1

Related Questions