选择多个值的Hibernate查询

时间:2022-04-22 01:30:50

In hibernate I can do following

在hibernate中,我可以执行以下操作

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?

如果我想获取int和String怎么做呢?

Query q = session.createQuery(""SELECT E.firstName,E.ID FROM Employee E";
List ans = q.list();

Now what will be the structure of list?

列表的结构是什么?

6 个解决方案

#1


36  

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];
         .....
     }

#2


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
}

#3


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

我已经从http://www.java2s.com/Tutorial/Java/0355__JPA/EJBQLCreatenewObjectInSelectStatement.htm获得了这个示例。

#4


3  

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];
}

#5


3  

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]);
}

#6


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();
}

#1


36  

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];
         .....
     }

#2


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
}

#3


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

我已经从http://www.java2s.com/Tutorial/Java/0355__JPA/EJBQLCreatenewObjectInSelectStatement.htm获得了这个示例。

#4


3  

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];
}

#5


3  

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]);
}

#6


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();
}