-->
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.  [ 2 posts ] 
Author Message
 Post subject: how to select only one record from db
PostPosted: Thu Feb 17, 2011 2:38 am 
Newbie

Joined: Thu Feb 17, 2011 2:30 am
Posts: 3
hi, i want fetch only one record from database irrespective of id.

please can u tell me the solution.

if use Query then i will always gives my persistence objects in the form of lists, so, i dont want it.
example:
--------
Person person=(Person)session.get(Person.class, id);

above statement returns only one row, similarly ,i want to fetch only one record from database irrespective of id.


Top
 Profile  
 
 Post subject: Re: how to select only one record from db
PostPosted: Thu Feb 17, 2011 3:22 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Code:
String sql = "SELECT TOP 1 {s.*} FROM <YourClass> s;";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity("s", <YourClass>.class);
Object = query.uniqueResult();


This code above should work.
I'm just not sure if the keyword 'TOP' belongs to standard SQL and therefore if it works on all databases.


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