-->
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: Some basic help to get me going
PostPosted: Fri Jul 28, 2006 6:11 am 
Newbie

Joined: Fri Jul 28, 2006 5:59 am
Posts: 7
Hello all. I'm a new user with hibernate. I've spent a fair amount of time reading the documentation, and doing the examples, but just need a bit of help to point me in the right direction.

Basically I'm learning hibernate by converting my existing application (which uses lots of bespoke sql via JDBC) to hibernate - the idea being that all my sql is handled in this way. I have a couple of questions.

1) I've succesfully created my main tables, associated objects etc, so that I can do a basic insert, and basic list to return the data. That's the easy bit. But in my app I have many occasions where I have simple bespoke sql statements - something along the lines of "select name from user where x='1' and y='2'" - you get the idea. What's the best way of handling this kind of thing in hibernate? Is it best to do this a) programmatically, avoiding all sql, or b) to create sql (in which case, what advantage would it offer over what I have already?)

2) Is one of the aims of hibernate to remove all sql from our code - or is it accepted that there will still be a lot of user defined code where necessary?

Thanks for some pointers, apologies if this is too basic for the forum but I just need a push in the right direction!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 28, 2006 6:24 am 
Beginner
Beginner

Joined: Tue Jul 04, 2006 11:29 am
Posts: 20
Location: Cambs, UK
Hibernate uses a query language called HQL (Hibernate Query Language). It's similar to SQL but is slightly different. Because of this, Hibernate is able to use the same HQL regardless of the database that is run and it's supported SQL. There will always be some kind of query language in the code but I think the idea is just to make it as easy as possible to make database changes without having to dive into your code everytime.

So to use where clauses, you have code like the following:
Code:
List result = session.createQuery("from User as u where u.userID = ?").setString(0, userID).list;


There's a number of different ways of doing it. I personally use named queries as it means I can change my queries a bit without having to dive into the code too much.

Have a read of the following sections in the manual, that should tell you what you need to know:
http://www.hibernate.org/hib_docs/v3/re ... e-querying
http://www.hibernate.org/hib_docs/v3/re ... ryhql.html


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 28, 2006 6:50 am 
Newbie

Joined: Fri Jul 28, 2006 6:41 am
Posts: 6
u can even use the criterion classes. check them out. Restriction, expression etc... see the reference, it's helpfull and easy to understand


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 28, 2006 7:11 am 
Newbie

Joined: Fri Jul 28, 2006 5:59 am
Posts: 7
Leesy, thanks so much for this response, it's just what I needed to get going. I know it's all there, but it takes some finding!

Leesy wrote:
So to use where clauses, you have code like the following:
Code:
List result = session.createQuery("from User as u where u.userID = ?").setString(0, userID).list;



Of course, this worked for me first time. However I'm just trying to make sure I understand what's going on here.

Is the 'User' in the HQL referring to the User table in the database, or to my User java class? Or is it implicitly both, taken from the hbm.xml mappings?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 31, 2006 5:58 am 
Beginner
Beginner

Joined: Tue Jul 04, 2006 11:29 am
Posts: 20
Location: Cambs, UK
It's the class that maps to a DB table. Same with the column names - use the names from your *.hbm.xml file, not the column names.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 31, 2006 8:10 am 
Newbie

Joined: Fri Jul 28, 2006 5:59 am
Posts: 7
Hi again. Things are going pretty well, but I've stumbled on a complicated query. The query below is an example of something which contains several different elements, and I must admit I'm struggling to convert this into HQL. I would really appreciate any help.

Code:
select tableA.id, tableA.name, tableB.user_id,
sum(tableB.val1), sum(tableB.val2), count(tableB.id)
where tableB.user_id=tableA.id
and tableA,name='x'
group by tableB.user_id
order by sum(tableB.val1) desc, sum(tableB.val2), tableA.name


So basically it's a select with some kind of join, which selects specific rows from more than one table. How can this handled in Hibernate, as this cannot return a particular object type? So far, all my queries have returned a List of one of my objects, or one particular data type. Whereas this needs to return varying values from two tables.

Any advice on a) how to get this into HQL, and b) how to process the return values, greatly appreciated!


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.