天天看點

hibernate 聯合查詢,傳回多個表(對應着多個對象)的操作【元組】

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