HQL (Hibernate Query Language)
Query createQuery(String queryString)
Create a new instance of Query for the given HQL query string.
· Object Oriented Query’s
· Independent on Database
· Uses Class name instead of table name
· Ex. Select ob.name form Student ob
· By using this we can select Multiple rows of data
Operations
1. Insert - we can’t insert user data
2. Update - we can update single column
Query q= session.createQuery(qry)
int i = q.executeUpdate();
3. Delete- we can delete single column
Query q= session.createQuery(qry)
int i = q.executeUpdate();
4. Select
· Single Row- single column select
Object = q.uniqueObject()
· Multiple Rows - Single Column select
List<String> = q.list();
· Multiple Rows - Multiple Column select
List<Object[]> = q.list();
Example
package dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import bo.StudentBo;
public class StudentDao {
public static void main(String[] args) {
String qry;
Query q;
Configuration cfg = new Configuration();
cfg.configure("res/hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
System.out.println("1.Multiple Rows- single column select");
qry = "Select ob.name from StudentBo ob";
q = session.createQuery(qry);
List<String> list =q.list();
System.out.println(" Name : "+list);
System.out.println("1.Multiple Rows- Multiple columns select");
qry = "from StudentBo ob";
q = session.createQuery(qry);
List<Object[]> li =q.list();
System.out.println(li);
transaction.commit();
session.close();
factory.close();
}
}