-->
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.  [ 6 posts ] 
Author Message
 Post subject: load via unique (but not primary) key
PostPosted: Sat Nov 15, 2003 6:55 am 
Newbie

Joined: Sat Nov 15, 2003 6:39 am
Posts: 3
I have a table that looks roughly like:

USERS
number(20) ID
varchar2(1024) EMAIL
...

ID in this case is the primary key, but EMAIL is also a unique key. As it turns out, I need to load a user by email (as a user enters their email into a form), but I don't want to use email as the primary key (as a user might change it).

Is there an efficient way to do this in Hibernate?

Right now, I'm using iterate. In particular,

String email = strutsFoo.getEmail();
Iterator users = session.iterate("from User user where user.email = ?", email, Hibernate.STRING);

My understanding, and looking at my DB logs, show that the iterate() call makes a call to the DB _EVERY_ time it is used to get the primary key for the User, and then looks at the cache to load the user data. I'd like to remove that call to the database if possible, and I'd like to do it using the Hibernate cache vs making my own. However, I haven't found something that looks like:

session.load(User, email, "User.email");

that will look in a Hibernate cache based on the unique key and then query the database with the trivial SELECT statement to get the data.

Thanks,
speed


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 15, 2003 7:00 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
For a start you should certainly use Session.find() rather than iterate().

And in Hibernate 2.1, enable the query cache and use


Code:
session.createQuery("from User user where user.email = :email")
   .setString("email", email)
   .setCacheable(true)
   .uniqueResult();


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 17, 2003 2:03 am 
Newbie

Joined: Sat Nov 15, 2003 6:39 am
Posts: 3
So, I'm thinking I'm not enabling the query cache, as what you suggested is generating a

java.lang.NoSuchMethodError: net.sf.hibernate.Query.setCacheable(Z)Lnet/sf/hibernate/Query

exception.

Is there a doc somewhere you can point me to? Or the two lines of code I need? I saw the JCS thing, but that looked to be a bit more advanced than what I want to do, which is simply cache a query. :)

Thanks!
speed


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 17, 2003 2:28 am 
Newbie

Joined: Sat Nov 15, 2003 6:39 am
Posts: 3
Actually, nevermind... I rebuild the hibernate2.jar file from the 2.1b6 source rather than use the hibernate2.jar file that came in the 2.1b6 distribution, and all is well now. JBuilder was whining that the src was out of sync with the lib, and now I see why. Don't think it was just me as I was pretty sure about the file I was using... might want to confirm that the hibernate2.jar is the right one for b6.

Cheers,
speed


Top
 Profile  
 
 Post subject: Criteria unique result
PostPosted: Tue Nov 18, 2003 8:45 am 
Regular
Regular

Joined: Wed Sep 10, 2003 7:09 am
Posts: 63
Hi,

Is there any way to do the following query using criteria queries, ie, returning an unique result?

session.createQuery("from User user where user.email = :email")
.setString("email", email)
.setCacheable(true)
.uniqueResult();

Thanks,
Joao Rangel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 18, 2003 8:55 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Criteria queries do not currently support result set caching.


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