-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Validate an hql query
PostPosted: Mon May 25, 2009 8:39 am 
Beginner
Beginner

Joined: Thu Jun 30, 2005 1:04 pm
Posts: 26
Hello,

Is there a way to automatically validate the structure of named HQL query without execute it?

Let me explain:
I will seek some sort of tools (like ant hibernate tools) to load the named query, then translate it in SQL and verify its consistency with the structure of database.

I know that you can easily retrieve named queries from the Configuration object.
It should not be too complicated to translate them into SQL
Then, compare with the structure of database.

Thank you in advance for your advice.


Top
 Profile  
 
 Post subject: Re: Validate an hql query
PostPosted: Tue Nov 24, 2009 8:51 am 
Beginner
Beginner

Joined: Thu Jun 30, 2005 1:04 pm
Posts: 26
I do it, if that can help :
Code:
         Ejb3Configuration cfg = new Ejb3Configuration();
         Ejb3Configuration configured = cfg.configure("myPersistenceUnit",
               new HashMap());
         EntityManagerFactory emf = configured.buildEntityManagerFactory();
         EntityManager em = emf.createEntityManager();
         Map enabledFilters = ((SessionImpl) em.getDelegate()).getEnabledFilters();
         QueryPlanCache queryPlanCache = ((SessionFactoryImpl) ((EntityManagerFactoryImpl) emf)
               .getSessionFactory()).getQueryPlanCache();

         Map queries = configured.getNamedQueries();
         for (Iterator iter = queries.values().iterator(); iter.hasNext();) {
            NamedQueryDefinition pNamedQueryDefinition = (NamedQueryDefinition) iter
                  .next();
            String qq = pNamedQueryDefinition.getQueryString();
            HQLQueryPlan plan = queryPlanCache.getHQLQueryPlan(
                  pNamedQueryDefinition.getQuery(), false,
                  enabledFilters);
            String query = plan.getSqlStrings()[0];
            log.info(" HQL : " + pNamedQueryDefinition.getQuery());
            log.info(" SQL : " + query);
            Connection connection = ((SessionImpl) em.getDelegate())
                  .connection();
            Statement ps = connection.createStatement();
            ps.execute("explain plan for " + query + "");

         }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.