-->
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: Howto get SQL generated for Criteria?
PostPosted: Sat Sep 04, 2010 7:30 am 
Beginner
Beginner

Joined: Tue Jul 03, 2007 8:47 am
Posts: 46
Hello,

Is there any way to get the SQL generated for Criteria-Queries - something like Criterion.toSQL()?
What I do for now is I have a criteria-query with an SQL-restriction:
Code:
Criterion restriction = Restrictions.sqlRestriction("this_.id IN (select id from Startkladde where aktuellerFlug=true order by id ASC limit "+length+" offset "+minRow+")");
Criteria query = session.createCriteria(KladdenEntry.class).add(restriction);


However instead of using hand-written SQL for the sqlRestriction, is there any way to generate the Restriction query with the Criteria API?

Thank you in advance, Clemens


Top
 Profile  
 
 Post subject: Re: Howto get SQL generated for Criteria?
PostPosted: Sun Sep 05, 2010 2:07 pm 
Beginner
Beginner

Joined: Tue Jul 03, 2007 8:47 am
Posts: 46
Ok, I found a solution but its really ugly:

Code:
package khdb.util;

import java.lang.reflect.*;
import java.util.*;
import java.util.Collections;

import org.hibernate.*;
import org.hibernate.engine.*;
import org.hibernate.hql.*;
import org.hibernate.hql.ast.*;
import org.hibernate.impl.*;
import org.hibernate.loader.*;
import org.hibernate.loader.criteria.*;
import org.hibernate.persister.entity.*;

public class HibernateUtil {

    public static String toSql(Session session, Criteria criteria){
       try{
         CriteriaImpl c = (CriteriaImpl) criteria;
         SessionImpl s = (SessionImpl)c.getSession();
         SessionFactoryImplementor factory = (SessionFactoryImplementor)s.getSessionFactory();
         String[] implementors = factory.getImplementors( c.getEntityOrClassName() );
         CriteriaLoader loader = new CriteriaLoader((OuterJoinLoadable)factory.getEntityPersister(implementors[0]),
           factory, c, implementors[0], s.getLoadQueryInfluencers());
        
         Field f = OuterJoinLoader.class.getDeclaredField("sql");
         f.setAccessible(true);
         return (String) f.get(loader);
       }
       catch(Exception e){
         throw new RuntimeException(e);
       }
     }
   
   
    public static String toSql(Session session, String hqlQueryText){
       if (hqlQueryText!=null && hqlQueryText.trim().length()>0){
         final QueryTranslatorFactory translatorFactory = new ASTQueryTranslatorFactory();
         final SessionFactoryImplementor factory =
           (SessionFactoryImplementor) session.getSessionFactory();
         final QueryTranslator translator = translatorFactory.
           createQueryTranslator(
             hqlQueryText,
             hqlQueryText,
             java.util.Collections.EMPTY_MAP, factory
           );
         translator.compile(Collections.EMPTY_MAP, false);
         return translator.getSQLString();
       }
       return null;
     }


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.