1

In my spring project, I have this method in my Dao class to return a List of all rows from a table:

@SuppressWarnings("unchecked") @Transactional public List<E> findAll() { try { List<E> instance = sessionFactory.getCurrentSession().createCriteria(entity.getClass()).list(); System.out.println("returned instance with "+instance.size()+" items"); return instance; } catch (RuntimeException re) { System.out.println("returned null"); return null; } } 

this method is called from this method from my service class:

@PreAuthorize("hasPermission(#user, 'listagem_'+#this.this.name)") @Transactional public List<E> listagem() { return dao.findAll(); } 

and this last one it's called from this method from my controller:

@RequestMapping(value="listagem/{pagina}/{items}/{ordem}") @PreAuthorize("hasPermission(#user, 'listagem_'+#this.this.name)") public ModelAndView listagem(@PathVariable("pagina") String pagina, @PathVariable("items") String items, @PathVariable("ordem") String ordem) { ModelAndView mav = new ModelAndView(); mav.setViewName("privado/"+this.getName()+"/listagem"); mav.addObject("lista", serv.listagem()); mav.addObject("pagina", pagina); mav.addObject("items", items); mav.addObject("ordem", ordem); return mav; } @RequestMapping(value="listagem.json", method=RequestMethod.GET) @PreAuthorize("hasPermission(#user, 'listagem_'+#this.this.name)") public ModelAndView listagem_json(@RequestParam("pagina") String pagina, @RequestParam("items") String items, @RequestParam("ordem") String ordem) { ModelAndView mav = new ModelAndView(); mav.setViewName(this.getName()+"/listagem"); mav.addObject("lista", serv.listagem()); mav.addObject("pagina", pagina); mav.addObject("items", items); mav.addObject("ordem", ordem); return mav; } 

the problem it's the method findAll from Dao class it's returning zero elements, despite the fact I make sure the table in database is populated with at least one row.

Anyone can see what's wrong here? I used the same code in other projects, and worked with no problems. This current project it's the first I am using this generic Dao class with the generic controller and service classes.

1 Answer 1

1

How try with setResultTransformer();

List<E> instance = sessionFactory.getCurrentSession().createCriteria(entity.getClass()) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list(); 
Sign up to request clarification or add additional context in comments.

4 Comments

I try that but still got the same problem (no rows retrieved).
Please can you show the whole generic dao code as doubt on entity.getClass().
Yes, this is the complete code for the generic class: github.com/klebermo/blog.cms/blob/master/src/main/java/com/…
@KleberMota Suspect on Dao(Class<?> classe). Look into this thread answer for Generic DAO class. If still problem, may need to check Service & underlying DAO creation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.