-->
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.  [ 4 posts ] 
Author Message
 Post subject: Criteria API - Using sqlRestriction with join table column
PostPosted: Fri Jul 13, 2007 1:16 pm 
Newbie

Joined: Fri Jul 13, 2007 12:59 pm
Posts: 2
Hibernate version: 3.2.4SP1

I have a need to write a sqlRestriction that uses a column on a join table. Even though I have created a alias for the table, Hibernate uses a different alias when the sql is constructed.
Below is a code snippet of what I am trying to do.

Code:
List persons = sess.createCriteria(Person.class, "p")
     .createAlias("p.company", "c")         
    .add( Restrictions.sqlRestriction("c.companyName || {alias}.name like (?)", "%Fritz%", Hibernate.STRING) )
    .list();


The above code doens't work as the alias for company is not c but something like c1_. How do I get the hibernate generated alias for the join table Passing in {c} instead of c doesn't work either.

Thanks

Rajiv


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 2:17 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Hi Rajiv

Rather than tacking on SQL restrications directly, you should use the Restriction API to create your entire query using the alias names you have defined. Hibernate will then convert your aliases into the ones it uses in the actual query.

i.e.
Code:
String likeValue = "%Fritz%";
List persons = sess.createCriteria(Person.class, "p")
     .createAlias("p.company", "c")         
    .add(Restrictions.or(
                Restrictions.like("c.companyName", likeValue),
                Restrictions.like("c.name", likeValue)))
    .list();


Mike


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 2:41 pm 
Newbie

Joined: Fri Jul 13, 2007 12:59 pm
Posts: 2
Mike this doesn't solve my problem. I use the Restrictions API for single field criteria but for this particluar case I have a need to query on 2 fields at the same time.
For example assuming person.name = Bob and person company = Intel using your approach if one were to search on 'Intel bob' no results will be returned. I want a search on intel bob to return a result.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 5:41 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Agreed. I misunderstood your intentions.

After messing with this for a while I don't think there's any need to use aliases unless you're referencing columns of the same name from different tables - i.e. there's an ambiguity with which columns the 'where clause' is refering to.

Instead of an alias try using a sub-criteria on the joined class - like this:

Code:
List persons = sess.createCriteria(Person.class)
       .createCriteria("company")
       .add(Restrictions.sqlRestriction("companyName || name like (?)",  "%Fritz%", Hibernate.STRING))
       .list();


The downside of using sqlRestrictions like this is the coupling with the database column names and lack of portability. For example, the above SQL doesn't work with my MYSQL database. I have to use the concat function instead of ||.

Perhaps a better solution would be to use HQL through the Query API. I'm assuming here that both companyName and name are properties of the Company object:

Code:
Query q = session.createQuery("from Person where  company.companyName || company.name like ?");
q.setParameter(0, "%Fritz%");
List persons = q.list();


If name is from Person and companyName from Company then its simply a matter of doing this:

Code:
Query q = session.createQuery("from Person where  company.companyName || name like ?");
q.setParameter(0, "%Fritz%");
List persons = q.list();


THe above is more portable because it doesn't reference column names directly. Hibernate changes the property style references to column names. Additionally, with the MYSQL dialect Hibernate changes the || operator to 'concat' in the resulting SQL. Amazing!

Mike


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