-->
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: Converting Criteria to SQL
PostPosted: Wed Dec 09, 2009 7:48 am 
Newbie

Joined: Wed Dec 09, 2009 7:29 am
Posts: 1
Hi,

I am trying to convert criteria to Native SQL.

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.getEnabledFilters());
Field f = OuterJoinLoader.class.getDeclaredField("sql");
f.setAccessible(true);
return (String) f.get(loader);

Problem with the above code it doesn't take care of the parameters. Is there anyway to replace of the parameters?

If there is any other better way of doing this translation?

Regards,
Joy


Top
 Profile  
 
 Post subject: Re: Converting Criteria to SQL
PostPosted: Wed Dec 16, 2009 1:10 pm 
Newbie

Joined: Wed Dec 16, 2009 12:24 pm
Posts: 6
Location: Munich
I used nativ SQL from a Criteria in my last project to do aritmetic operations with Projections.sqlProjection().
With CriteriaQueryTranslator it's also possible to access nativ SQL. I described this at http://blog.bambo.it/2009/12/generic-subcriterias-for-hibernate.html and http://blog.bambo.it/2009/12/arithmetic-operations-using-generic.html.

Code:
CriteriaImpl criteriaImpl = ( CriteriaImpl ) rootCriteria;
SessionImplementor session = ( ( CriteriaImpl ) rootCriteria ).getSession();
SessionFactoryImplementor factory = session.getFactory();
String[] implementors = factory.getImplementors( criteriaImpl.getEntityOrClassName() );

CriteriaQueryTranslator translator = new CriteriaQueryTranslator( factory,
                ( CriteriaImpl ) rootCriteria, implementors[ 0 ], CriteriaQueryTranslator.ROOT_SQL_ALIAS );


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.