Convert a source class object to a specific class object

I work in Hibernate and Spring. I developed a method that returns a List type. When I query a database using hibernate, it creates a specific type of object. I want to iterate over the list of a raw type object and print the property of the object.

Here I attached my method

public List getAnalyticsbyid(String userId) { Session session=sessionFactory.openSession(); String querystring="SELECT DISTINCT bounces ,visits, landingPagePath FROM AnalyticsDataFeedBean where userId='"+userId+"' ORDER BY bounces DESC"; Query query=session.createQuery(querystring).addEntity(AnalyticsDataFeedBean.class); query.setMaxResults(10); return query.list(); } 
+4
source share
1 answer

Your HQL query returns List<Object[]> , you can change the method signature to

 public List<Object[]> getAnalyticsbyid(String userId) 

And in the place where this method is called, iterates over the list and prints the details

 List<Object[]> list = getAnalyticsbyid("user"); for (Object[] objects : list) { for (Object object : objects) { System.out.print(object); System.out.print("\t"); } System.out.println(); } 

If you want to display it as a table in the jsp file, start with the following snippet

 <table> <tr> <th>bounces</th> <th>visits</th> <th>landingPagePath</th> </tr> <c:forEach items="${analytics}" var="objects"> <tr> <c:forEach items="${objects}" var="object"> <td><c:out value="${object}"/></td> </c:forEach> </tr> </c:forEach> </table> 
+1
source

Source: https://habr.com/ru/post/1391812/


All Articles