现在的位置: 首页 > 综合 > 正文

hibernate 联合查询,返回多个表(对应着多个对象)的操作【元组】

2013年11月11日 ⁄ 综合 ⁄ 共 680字 ⁄ 字号 评论关闭

 

如下返回的是元组 ,元组:数据库表中的每一条记录。
Hibernate queries sometimes return tuples of objects. Each tuple is returned as an array: 
Iterator kittensAndMothers = sess.createQuery(
            "select kitten, mother from Cat kitten join kitten.mother mother")
            .list()
            .iterator();

while ( kittensAndMothers.hasNext() ) {
    //转换为数组,数组里面包含着对应的元组。                                                
    Object[] tuple = (Object[]) kittensAndMothers.next();
    Cat kitten = (Cat) tuple[0];
    Cat mother = (Cat) tuple[1];
    ....
}

 

如下:返回的结果是元组和函数值,【对象和聚集函数 】

Iterator results = sess.createQuery(
        "select cat.color, min(cat.birthdate), count(cat) from Cat cat " +
        "group by cat.color")
        .list()
        .iterator();

while ( results.hasNext() ) {
    Object[] row = (Object[]) results.next();
    Color type = (Color) row[0];
    Date oldest = (Date) row[1];
    Integer count = (Integer) row[2];
    .....
}

 

抱歉!评论已关闭.