-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to add sql where clause to Criteria programmatically?
PostPosted: Mon Feb 02, 2004 7:46 pm 
Beginner
Beginner

Joined: Wed Oct 08, 2003 4:22 pm
Posts: 29
Hello,

I'm trying to port some code from OJB over to Hibernate. OJB had an "addSql" method on its Criteria class which enabled one to add an aribtrary filter.

For example,

Code:
        Criteria criteria = new Criteria();
        String accessFilter = " AccessControl.Id = " + id + " AND AccessControl.Access = 'PUBLIC' ";
        criteria.addSql(accessFilter);


As you can see, we used this for an instance-level public/private authorization scheme. I looked through the Hibernate reference guide, but the SQL query mechanism (covered in Ch 15) seems to target complete SQL queries, whereas I just want to add a SQL where clause to a Hibernate query.

Is there a way to do this?

TIA,

Alex


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2004 7:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can do this quite easily by creating a custom criterion like this:

[code]
criteria.add(new Criterion() {
public String toSQLString(..) {
return "AND x=y";
}
public TypedValues[] getTypedValues(...) {
return new TypedValues[0];
}
}[code][/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2004 7:50 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Arh, sorry, formating failure


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2004 8:01 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Actually, there is an Expression.sql(), exactly for this purpose. (But probably creating a new Criterion is more elegant.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2004 1:25 pm 
Beginner
Beginner

Joined: Wed Oct 08, 2003 4:22 pm
Posts: 29
Thanks for the rapid reply! Implementing it as we speak. Once again, nice API.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.