24

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?

1
  • Check out Adisesha's answer. It's more elegant than the answers here IMO, just that you would need to use aliases with HQL. Commented Jun 20, 2020 at 10:27

6 Answers 6

50

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];
         .....
     }
Sign up to request clarification or add additional context in comments.

9 Comments

What if i have a many to many relation and the intermediate table is not modeled as class. how can i use that table in hql query?
@user93796 Hibernate also allows you to run standard SQL, which you can create as: Query q = session.createSQLQuery("select id, first_Name from Employee"); where createSQLQuery is used to run SQL; id & first_Name are database columns and Employee is database table.
@user93796 Not sure that my previous message was clear. What I meant was: if you want to use un-mapped tables in the query then SQL is better option.
thanks for helping.Let me be bit clear. I have a many to many realtion between table A and table B .Table C maps A to B.A hase set of B .And B has Set of A.Now i want to select all A which has B b1 as one of the entity in its set.Eg consider stackoverflow.I want to select all post which has java as one of its tag. In such case A table is Post , B is tags and C is relation between A and B.How will i fetch n posts of java tags using HQL
@user93796: This is way different question than the one you posted earlier. Any way, try something like: select p from Post p join p.tags t where t.tagName in ('Java'). This should do your job provided you have done the mapping properly.
|
10

You will get a list of arrays of Objects (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
}

1 Comment

is the order of the array determined by the order of the query?
4
List<Object[]> is the structure.

So you get each element like this:

List ans = q.list();
for(Object[] array : ans) {
    String firstName = (String) array[0];
    Integer id = (Integer) array[1];
}

Comments

3

You should use a new object to hold these values, just like this:

"SELECT NEW EmpMenu(e.name, e.department.name) "
                + "FROM Project p JOIN p.students e " + "WHERE p.name = :project "
                + "ORDER BY e.name").setParameter("project", projectName).getResultList()

I've got this example from http://www.java2s.com/Tutorial/Java/0355__JPA/EJBQLCreatenewObjectInSelectStatement.htm

Comments

2
Query qry=session.createQuery("select e.employeeId,e.employeeName from Employee e where e.deptNumber=:p1");
qry.setParameter("p1",30);
List l2=qry.list();
Iterator itr=l2.iterator();
while(itr.hasNext()){
Object a[]=(Object[])itr.next();
System.out.println(a[0]+"/t"a[1]);
}

Comments

1

Without iterators:

@SuppressWarnings( "unchecked" ) 
public List<Employee> findByDepartment(long departmentId){ 

    SQLQuery query = session.createSQLQuery("SELECT {emp.*} " +
                                             " FROM employee emp " + 
                                            +"WHERE emp.department_id = :departement_id");
    query.setLong("department_id",  departmentId);
    query.addEntity("emp",  Employee.class);                        
    return (List<Employee>) = query.list();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.