Hibernate Query Language – HQL

Hibernate Query Language – HQL  :

Description about HQL :-

  • HQL is the Object Oriented query language for Hibernate Framework.
  • HQL is a case-sensitive.
  • HQL with a simple and easy-to-use language.
    • While SQL use “SELECT * FROM USERS” to fetch all records in USERS table, But
    • for HQL we use “FROM USERS” .

1.) HQL Insert Query Language.

  • HQL also support INSERT INTO – example:
  • In HQL, only the INSERT INTO … SELECT … is supported; there is no INSERT INTO … VALUES. HQL only support insert from another table.
    • Query query = session.createQuery(“INSERT into USERS(userId, username)” + “SELECT bk.userId,bk.username FROM  BK_USERS bk“);

2.) HQL Update Query Language.

  • Example of HQL update query
 Transaction trans =session.beginTransaction();  
    Query query=session.createQuery("update Patient set PatientName=:pname where id=:pid");  
    query.setParameter("pname","franko Nando");  
    query.setParameter("pid",01);  
      
    int status=query.executeUpdate();  
    System.out.println(status);  
    trans.commit();