How do you select a column using Hibernate?

前端 未结 3 1628
北荒
北荒 2020-12-25 10:55

I would like to select a single column instead of a whole object, using Hibernate. So far I have this:

 List firstname = null;

 firstname = ge         


        
3条回答
  •  一向
    一向 (楼主)
    2020-12-25 11:24

    If you need to query 2 or more columns and get the values from the query, this is the way to do it:

    ....
    crit.setProjection(Projections.property("firstname"));
    crit.setProjection(Projections.property("lastname"));
    
    List result = crit.list();
    
    ...
    
    for (Iterator it = result.iterator(); it.hasNext(); ) {
        Object[] myResult = (Object[]) it.next();
        String firstname = (String) myResult[0];
        String lastname = (String) myResult[1];
    
        ....
    }
    

提交回复
热议问题