Results 1 to 12 of 12
  1. #1
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Advantage of HQL over SQL?

    As the subject line says.

    Telling that we have advantage of using shortened form of query when we retrieve all columns from a table is just too small a reason to have and use HQL.

    HQL query:
    Java Code:
     From Employee
    SQL query:
    Java Code:
    select * from Employee
    The only advantage I can see is that in HQL we don't use "select *". So in HQL we use two words less. Now, is this advantage really and advantage? If there is any other advantage let me know.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Advantage of HQL over SQL?

    You are focusing on way too simple examples. That is NEVER a good way to go about comparing different technologies, you have to seek out monstrously complicated examples to really see the merits of a technology.

    I maintain an application which has a datamodel that maps to 200 different entities, with many N:M mappings and data which has inheritance structures. When I persist a single entity that might cause hundreds of other objects to be persisted at the same time (financial application; policies, invoices, bookings, booking lines, hours, costs, etc. etc.)

    Using Hibernate, I can do all that with a single call and have data validation in place at the same time. Try doing that with plain JDBC and SQL.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Advantage of HQL over SQL?

    HQL is database agnostic.
    That is, it works with any database that Hibernate works with.
    Native SQL queries are not guaranteed to do that as each db has its own subtle variation on SQL.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Advantage of HQL over SQL?

    @gimbal2
    You have an example? Or, at least a link to an example showing the advantage? I really want to see the advantage.

    PS:I know it is not practical to show an example with 200 entities here in this forum.

  5. #5
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Advantage of HQL over SQL?

    Yes, tolls, that is one advantage I understand. More of the advantages, please.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Advantage of HQL over SQL?

    To be honest I haven't seen one on the internet yet, you'd have to find some open source product. Better is to learn enough of the tech to be able to build it yourself. That is a far more honest approach (to yourself) than comparing select * from.

    It all comes back to you: do you REALLY want to know the difference? Or do you just want to execute a single line of code and pretend you did enough effort? Its up to you.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Advantage of HQL over SQL?

    When I persist a single entity that might cause hundreds of other objects to be persisted at the same time (financial application; policies, invoices, bookings, booking lines, hours, costs, etc. etc.)

    Ok, but when I am persisting hundreds of object I still have to mention the values. In the below example there are only 3 properties firstName, lastName, salary. The same thing needs to be done for 100 columns(That is 100 values need to be mentioned) in case of 100 objects. I, anyways have to mention the values for the properties/columns. So, where is the advantage? I am asking this question based on what you answered.

    Java Code:
    String hql = "INSERT INTO Employee(firstName, lastName, salary)"  + 
                 "SELECT firstName, lastName, salary FROM old_employee";
    Query query = session.createQuery(hql);
    int result = query.executeUpdate();
    System.out.println("Rows affected: " + result);

  8. #8
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Advantage of HQL over SQL?

    Someone pls help

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Advantage of HQL over SQL?

    Generally, you don't input those values "yourself" at all. They:

    - materialize magically (as in: the software can derive them such as through calculations)
    - they are input by users through a UI

    And I hope this gives you a hint where you are continuing to go wrong; your thinking is still too narrow. You cannot judge technology by doing the bare minimum. Build an application if you want answers.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Advantage of HQL over SQL?

    Quote Originally Posted by jmohandos304 View Post
    Yes, tolls, that is one advantage I understand. More of the advantages, please.
    Are you talking about the advantage of Hibernate, or of HQL.

    Because I see your INSERT statement above and I'm a bit confused...you normally wouldn't be writing INSERTs as Hibernate does all that for you based on your mappings.
    Same with DELETE, or UPDATE.
    The only time SQL (or HQL) is needed is in more complex arrangements that are best processed by the database server, rather than in Java code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Advantage of HQL over SQL?

    Are you talking about the advantage of Hibernate, or of HQL.

    I am referring the below link:
    Hibernate Query Language (HQL) Tutorial

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Advantage of HQL over SQL?

    Well, then it's really my answer above in post #3.

    But you need to understand Hibernate in general before worrying about HQL to any large extent.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. advantage of Runnable over Thread
    By viki1719 in forum Threads and Synchronization
    Replies: 4
    Last Post: 05-18-2013, 01:13 AM
  2. Advantage of arraylist over array
    By karang in forum New To Java
    Replies: 3
    Last Post: 09-26-2012, 09:21 AM
  3. advantage of using thread
    By simorgh in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-18-2010, 05:53 AM
  4. Replies: 7
    Last Post: 09-16-2008, 06:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •