-->
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.  [ 3 posts ] 
Author Message
 Post subject: Need Help with selecting a value from DB
PostPosted: Wed Jul 02, 2008 2:56 am 
Newbie

Joined: Wed Jun 25, 2008 5:24 am
Posts: 7
Hi All,

I tried to retrieve a value from DB using session.get(), but in session.get we can pass only the id of the table and we can retrieve according to table's id, but in my case i want to retrieve value by passing a string which is not the id, how can i do this without writing query.

Thanks,
vyrav.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 02, 2008 5:21 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
if you want to fetch for e.g. a person from a db without knowing it's id but knowing it's name you have to write a query for this.
something like this probably will do
Code:
Query query=session.createQuery("select p FROM Person p where p.name=?");


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 11:08 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Indeed, you'll want to look at the Hibernate Query Language HQL:


http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=08masteringhqlandnamedqueries

Code:
Session session = HibernateUtil.beginTransaction();
String hql="from User where loginName = 'mj' ";
Query query = session.createQuery(hql);
Object o  = query.uniqueResult();
User u = (User)o;
System.out.println(u.getLoginName());
System.out.println("\n\n");
HibernateUtil.commitTransaction();


You could also look at the Hibernate Criteria API for more complex queries as well:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=09howtousethecriteriaapi

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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