• JSP
  • Struts
  • jQuery
  • RubyonRails
  • Java IO
  • Servlet
  • Core Java
  • JavaScript
  • PHP

  • <

    Hibernate Query Language(HQL)


    Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but instead of operating on tables and columns, HQL works with persistent objects and their properties. HQL queries are translated by Hibernate into conventional SQL queries, which in turns perform action on database.
    Although you can use SQL statements directly with Hibernate using Native SQL, but I would recommend to use HQL whenever possible to avoid database portability hassles, and to take advantage of Hibernate's SQL generation and caching strategies.
    Keywords like SELECT, FROM, and WHERE, etc., are not case sensitive, but properties like table and column names are case sensitive in HQL.

    HQL Select Query Example

    Query query = session.createQuery("from Stock where stockCode = :code ");
    query.setParameter("code", "7277");
    List list = query.list();

    Query query = session.createQuery("from Stock where stockCode = '7277' ");
    List list = query.list();


    HQL Update Query Example

    Query query = session.createQuery("update Stock set stockName = :stockName" +
    " where stockCode = :stockCode");
    query.setParameter("stockName", "DIALOG1");
    query.setParameter("stockCode", "7277");
    int result = query.executeUpdate();

    Query query = session.createQuery("update Stock set stockName = 'DIALOG2'" +
    " where stockCode = '7277'");
    int result = query.executeUpdate();


    HQL Delete Query Example

    Query query = session.createQuery("delete Stock where stockCode = :stockCode");
    query.setParameter("stockCode", "7277");
    int result = query.executeUpdate();

    Query query = session.createQuery("delete Stock where stockCode = '7277'");
    int result = query.executeUpdate();


    HQL Insert Query Example

    "insert into Object (id, name) select oo.id, oo.name from OtherObject oo";


    Insert a stock record from another backup_stock table. This can also called bulk-insert statement.

    Query query = session.createQuery("insert into Stock(stock_code, stock_name)" +
    "select stock_code, stock_name from backup_stock");
    int result = query.executeUpdate();















    © copyright 2017-2021 Completedone pvt ltd.