user1783933
user1783933

Reputation: 1145

Data for a view in SpringMVC from Hibernate

I have a problem to get information from a query in hibernate. In my class dao, have a function like this.

@SuppressWarnings("unchecked")
public List<String> getOcurrencesPerMonth(){
        List<String> dataOcurrences = (List<String>) getHibernateTemplate().execute (new HibernateCallback() {
            public Object doInHibernate(Session session) {
                Query query = session.createQuery("select concat(year(ror.created),'/',month(ror.created)), dt.name, count(distinct ror.id) from RawOccurrenceRecord ror, DataProvider dt" +
                        " where ror.dataProviderId = dt.id" +
                        " group by month(ror.created),ror.dataProviderId");
                return query.list();
            }
        });

    ArrayList<String> dataO = new ArrayList<String>();  

    Iterator<String> iterator = dataOcurrences.iterator();
    while (iterator.hasNext()) {
        dataO.add(iterator.next());
    }

    return dataO;
}

I pass the List to the view, and use a c:forEach in the jsp for view the result, but i only have results like Ljava.lang.Object;@1659f66

My query in MySQL is

SELECT CONCAT(YEAR(raw_occurrence_record.created),'/',MONTH(raw_occurrence_record.created)), data_provider.name, COUNT(DISTINCT raw_occurrence_record.id) FROM raw_occurrence_record, data_provider WHERE raw_occurrence_record.data_provider_id = data_provider.id GROUP BY month(raw_occurrence_record.created), raw_occurrence_record.data_provider_id;

And the result is like

2012/12 | POL | 15
2013/1  | JKL | 45
2013/1  | GTK | 30

¿How i do to get data for each column in the query in my view?

Upvotes: 0

Views: 218

Answers (1)

Affe
Affe

Reputation: 47984

That's because your query doesn't return a List<String>, it returns a List<Object[]>. [Ljava.lang.Object;@65690726 is what you get when you call toString() on an Array, which ultimately your JSP is doing when you try to print it.

You want something like this (although architecturally speaking one wouldn't generally do this in a class called *-DAO!)

for (Object[] result: dataOccurences) {
  data0.add(result[0].toString() + "|" + result[1].toString() + "|" + result[2].toString());
}

Upvotes: 1

Related Questions