-->
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.  [ 1 post ] 
Author Message
 Post subject: Applying Criteria restrictions to Native query. My solution
PostPosted: Thu Feb 15, 2007 5:28 am 
Newbie

Joined: Thu Feb 15, 2007 3:48 am
Posts: 1
I'm using native query API. But it is usual to apply dynamic filters, ordering and limiting first and maxResults of a query. Criteria interface is ideal for that. I have written small utility class to apply critetia query restrictions to native query. Here is usage code sample

CriteriaParams critPar = new CriteriaParams();
critPar.add(Restrictions.eq("P.FULLNAME", "Doe"));
critPar.addOrder(Order.asc("P.SHORTNAME"));

CriteriaSQL csql = new CriteriaSQL(critPar);
SQLQuery q = hs.createSQLQuery(csql.getQueryString("SELECT P.* FROM PERSON P, CLIENT C WHERE P.ID = C.PERSONID "))
.addEntity(Person.class);

csql.populateParams(q);
List result = q.list();

The resulting sql in this case will be
SELECT P.* FROM PERSON P, CLIENT C WHERE P.ID = C.PERSONID
AND P.FULLNAME = ? ORDER BY P.SHORTNAME ASC

So CriteriaSQL class generates full sql statement using Criteria restrictions, order, first and maxResults. It uses hibernate toSqlString functions. It also sets restriction parameters to resulting SQLQuery.
CriteriaParams is also my simple detached criteria container. It is used to store criteria parameters.

So please comment my approach. How do you solve the problem of native query and dynamic restrictions?

If someone is interested in source code I'm ready to publish it. Don't know is it Ok to post it just here in forum.


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

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.