-->
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: Query By Example and Primary Key Inclusion in Criteria
PostPosted: Thu Apr 06, 2006 4:59 am 
Regular
Regular

Joined: Tue Mar 23, 2004 2:10 am
Posts: 51
Hi,
I am using
Hibernate version:Hibernate 3.2.0.CR1
Name and version of the database you are using:Oracle 10g


I want to ask that does Query By Example (using hibernate criteria api)
includes the primary key field as criteria?.

I tried running a peice of code which has primary key set on the example instance and execution proves that it does not include the primary key as criteria.

e.g. (i know its bad design but sometimes you do need to live with legacy databases) If the primary key of my table is a String (lets say user Name)
and i am searching for a user with his/her partial name then should not QBE include the primary key in searching criteria? I think that there should be a property enablinhg this to happen.

Regards,
Shoaib Akhtar


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 12:22 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Query by partial primary key? That's a pretty weird situation. It might happen, but don't hold your breath.

You can map the primary key a second time, as a <property name="???" formula="primaryKeyColumn" insert="false" update="false"/> and use that for your searching.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 1:11 am 
Regular
Regular

Joined: Tue Mar 23, 2004 2:10 am
Posts: 51
Hi,
Thanks for u r answer. So its certain now that hibernate criteria api does not support this. I think that there is no harm in this case to treat the primary key as ordinary property. The thing u talked about is more of a workaround stuff. Anyways thanks for u r help atleast workaround is better than having nothing.
Long Live Hibernate ( a great framework). cheers,

Regards,
Shoaib Akhtar


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 1:18 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
There's a second option that I've just thought of: add an sqlRestriction:
Code:
Criteria crit = session.createCriteria(YourClass.class)
    .add( Example.create(yourObject));
  crit.add(Restrictions.sqlRestriction("idColumn like '%:partialId%', partialId, Hibernate.STRING));


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 1:19 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Heh oops, sqlRestriction doesn't take named parameters, replace ":partialId" with "?"


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.