-->
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: createSQLQuery vs HQL
PostPosted: Tue Jun 22, 2004 2:54 am 
Newbie

Joined: Tue Jun 22, 2004 2:23 am
Posts: 3
Hi,

In my project we're in the process of structuring database access logic. We've decided to try and incorporate SQL (as opposed to HQL) as our primary query language (using createSQLQuery), and thus would like to be as consistent with this as possible. The main arguments for doing this is:
    * consistent, uniform code
    * reuse of existing SQL
    * readability for non-Hibernate literates


By using createSQLQuery it is required to fetch *all* properties for the related persister classes. I.e:

Code:
   String sql =
      "SELECT {PERSON.*}" +
      " FROM PERSON P" +
      " WHERE P.SSID = :ssId"
   String aliases[] = new String[] {"P"};
   Class classes[] = new Class[] {Person.class};

   List qRes = session.createSQLQuery(sql, aliases, classes)
                      .setString("ssId", ssId)
                      .list();


In the cases where I'm only intersted in a single field from the result, fetching all columns intuitively seems to be inefficient. I might very well be wrong on this, though. My big concern is that the overhead related to this is heavy, which suggests using a mix of SQL and HQL. Any views on this would be greatly appreciated.

Regards,
Rubel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 22, 2004 3:04 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
learning hql is not so complex and is more "readable" since it's really oo.
It's also more logic to use.
SQLQuery should be used only for reuse of SQL in JDBC code for example.
You're going into very large query if you continue like this

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 22, 2004 4:18 am 
Newbie

Joined: Tue Jun 22, 2004 2:23 am
Posts: 3
It's not a matter of learning HQL, but rather more of a code consistency question.

I understand that this will result in more code. My main consern is that it might be more inefficient than using HQL to query specific columns.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 22, 2004 4:39 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
i don't think so, sqlQuery and HQL are parsed to give a SQL ... i don't think there is significant overhead at this level

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 22, 2004 4:55 am 
Newbie

Joined: Tue Jun 22, 2004 2:23 am
Posts: 3
The problem could in essence be rephrased to a more general database question:

What is more efficient:

Code:
SELECT * FROM person


or

Code:
SELECT name FROM person



(Also, how does the resulting SQL look like after being converted from HQL, i.e for a HQL statement like "SELECT p.name FROM Person p"

I guess I could have a look in hibernate log files to find out...)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 22, 2004 5:04 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
SELECT p.name FROM Person p

will only retrieve the column you need.


and of course select * from is slower than select oneColumn from... but is it really significant? it depends on your network and columns type...

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


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.