begiPass
begiPass

Reputation: 2174

retrieving data returned by database query Java EE, hibernate

I have this method that return a list :

public List getPourcentageDivision() {
    List cs = null;
    try {                       

        org.hibernate.Transaction tx = session.beginTransaction();

        Query q = session.createSQLQuery("SELECT u.division,COUNT(c.id) AS nb_commandes FROM utilisateur u LEFT OUTER JOIN commande c ON c.utilisateur_id = u.id GROUP BY u.division");
        if(q.list().size() > 0)
        cs = q.list();            

        session.clear();
        session.flush();
    } catch (Exception e) {

        e.printStackTrace();

    }
    return cs;
}

like you see , every row return two attribute like it is in this query :

SELECT u.division,COUNT(c.id)

can you remember me how to access this data from this list, because I got into the habit if having List<MyClass> and it was easy to retrieve data but this time I am in front of a complex query to draw chart

How can I achieve this?

Upvotes: 0

Views: 1908

Answers (1)

Mikko Maunu
Mikko Maunu

Reputation: 42074

Elements in result list are object arrays length of 2 (number of items in select statement). First element of array is value denoted by u.division and second one matches to COUNT(c.id)

for (Object[] row: (List<Object[]>) result ) {
    Object division = row[0];
    Object count = row[1];
}

With information provided it is not possible to tell type of division. Type of returned values can be observed by adding following (works of course only for non-null values):

System.out.println(division.getClass()); //u.division
System.out.println(count.getClass()); //COUNT(c.id)

Upvotes: 1

Related Questions