0

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?

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.