-->
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: converting SQL query to Hibernate
PostPosted: Wed Nov 21, 2007 12:56 pm 
Newbie

Joined: Tue Oct 16, 2007 3:42 pm
Posts: 11
hi,

i'm tring to work on an assignment where we have a SQL query that involves about four tables. as we are moving to hibernate, i'm working on converting this query to hibernate. since i am new to this technology right now, i wanted to request if the following few first steps are correct:

1. write POJOs for all the tables involved in the present SQL query.
2. determine relationships among tables.
3. write hibernate mapping files based on the above two steps.
4. convert the SQL query to hibernate.

i'm familiar with the first three steps, but not too sure about the fourth step. given a simple hibernate query string, i know how to set hibernate, create the query, and run the query with the .list() method. but are there any links that describe given a working SQL query, how to come up with the string (for hibernate) with which i can make the query object? any guidance will be helpful. thanks!


Top
 Profile  
 
 Post subject: Think about pojos, rather than SQL
PostPosted: Wed Nov 21, 2007 3:42 pm 
Regular
Regular

Joined: Wed Dec 21, 2005 6:57 pm
Posts: 70
The hiberante query will have similar structure to your SQL query, where the SQL table names are now POJO names and the SQL columns are now POJO properties.

However, there are also differences. For example when you use a dot-separated property in HQL such as person.address.zip you are telling hibernate to automatically join the person and address tables and pull the zip attribute.

It can also become quite complicated because a complex Hibernate mapping can map a POJO to many tables (as in table-per-class or table per concrete class mapping styles) in which case the translation is not that simple.

The right way to approach your task is to THINK about POJOs and let Hibernate worry about how they are mapped to SQL. That's really the whole point, actually -- to be able to think in your object model and not focus on the DB.


Top
 Profile  
 
 Post subject: HQL
PostPosted: Wed Nov 21, 2007 3:47 pm 
Newbie

Joined: Thu Nov 15, 2007 1:36 pm
Posts: 12
Location: Ahsburn, VA
The number one trick with HQL is to remember that you are using objects not tables.

To help unconfuse myself, I've adopted the convention of making table names plural and entity object names singular. Thus, if I ever use the plural name in an HQL statement, I know I've done something wrong.

This is case, I have a class called Book, and a table called Books. This returns an Iterator. You will get back an Object[] and you'll need to cast the types correctly.
Code:
public static Iterator listBookInfo() {
    Session session = HibernateUtil.getSession();
    return  session.createQuery("select title, description from Book")
               .iterate();
}


Or, perhaps you'd like to return all books. In this case, you'd use the Java for-each statement to walk over the list.
Code:
for ( Book b : listBooks() ) { ... }

public static List<Book> listBooks() {
   Session session = HibernateUtil.getSession();
   return  session.createQuery("from Book").list();
}


Fetching a single object is easy, but only one had better match or you get an exception.
Code:
Book book = (Book) session.getNamedQuery("book.getByISBN")
.setString( "isbn", isbn)
.uniqueResult();

// This is how Book is defined...
@Entity
@Table( name = "Books" )
@NamedQuery( name = "book.getByISBN", query="from Book where isbn = :isbn" )
public class Book {...}


As you can see, the HQL notation is a little more terse than SQL, and you're dealing with objects. There's no straight SQL to HQL process that I'm aware of. Start with from Classname and if you need to add members and where clauses, using member names.

The trick, in a nutshell, is to use member names instead of column name, and to use entity object class names instead of table names; at that point, the SQL you know and love is very, very close.

Hope this helps,
-wls


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.