In hibernate I can do following
Query q = session.createQuery("from Employee as e); List<Employee> emps = q.list();
Now if I want to fetch int and String how can I do it?
Query q = session.createQuery(""SELECT E.firstName,E.ID FROM Employee E"; List ans = q.list();
Now what will be the structure of list?
Session session = HibernateUtil. getCurrentSession(); If you want to fetch the records from a database table try this simple query: List<Object> slist = session.
Keywords like SELECT, FROM, and WHERE, etc., are not case sensitive, but properties like table and column names are case sensitive in HQL.
To create query in the Hibernate ORM framework, there is three different types.
This is fine. Only thing you need to understand is that it will return list of Object []
as below:
Query q = session.createQuery("select e.id, e.firstName from Employee e"); List<Object[]> employees= (List<Object[]>)q.list(); for(Object[] employee: employees){ Integer id = (Integer)employee[0]; String firstName = (String)employee[1]; ..... }
You will get a list of arrays of Object
s (each one with two elements)
List< Object[] > employees = q.list(); for ( Object[] employee : employees ) { // employee[0] will contain the first name // employee[1] will contail the ID }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With