This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes getting results from uniqueResult() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "getting results from uniqueResult() " Watch "getting results from uniqueResult() " New topic
Author

getting results from uniqueResult()

Aaron Parker
Greenhorn

Joined: Jan 31, 2004
Posts: 27
How do I get results out of the uniqueResult() method of the Query class. I can get a result into it, but I can't find anything that explain how to get stuff out!

Object results;
results = query.uniqueResult();

PS to moderator, I posted this in Servlets, too as that's what I'm working with, but was unsure where to post.

Thanks.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

I had to Google "uniqueResult Query Java" to find out that you were talking about Hibernate. We've actually got a whole forum dedicated to Hibernate and its cousins here. I'm going to move this post there, and I'm sure then you'll get an answer.


[Jess in Action][AskingGoodQuestions]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
I haven't used Hibernate's Query objects yet, but from the JavaDocs I gather it's similar to the "getFirst(List)" method I wrote for our DAOs.
Convenience method to return a single instance that matches the query, or null if the query returns no results.
This is used when the query you are issuing is expected to return a single object/value.

For example, if you have defined and mapped a Person class with a unique "name" attribute, querying for a Person with a given name will always return zero or one result. In this case you don't want to have to get a List back and extract the first element from it. Query.uniqueResult() will do this for you.

Simply cast the result to the class for which you are querying, Person in this example.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: getting results from uniqueResult()
 
Similar Threads
[hibernate] How to load lazy collections
NX: Exam Results - email or in Database?
Passed SCEA with 91% marks.
Case Insensitive Hibernate Queries
getting results from uniqueResult()