0

the problem is the following:

I am trying to make a query to tables with hibernate and sql native, the problem is that hibernate returns a list<Object[]> and I need a List of object of my class, but I can't find how to do it

here is my code:

@Override
public List<ProductDTO> getAllDao() {
    
    Session session = FactoryProduct.getSessionFactory().getCurrentSession();   
    session.beginTransaction();

 String sql= "SELECT id, product, quantity, id_warehouse, sold_quantiy, quantity - sold_quantiy AS total FROM `product` INNER JOIN sale ON id = id_product";

    SQLQuery query = session.createSQLQuery(sql);
    
    List<Object[]> rows = query.list();
    
    
    for(Object[] row : rows){
        ProductDTO dto = new ProductDTO();
        SaleDTO dto2 = new SaleDTO();
        
        dto.setIdDTO(Integer.parseInt(row[0].toString()));
        dto.setProductDTO(row[1].toString());
        dto.setQuantityDTO(Integer.parseInt(row[2].toString()));
        dto.setId_warehouseDTO(Integer.parseInt(row[3].toString()));
        dto2.setSold_quantity(Integer.parseInt(row[4].toString()));
        dto.setSaleDTO(dto2);
        
        System.out.println(dto);
    }
    
    List<Object[]> rows= List<ProductDTO> listDTO;// I need to cast this list object into list productDTO
    
    
    session.getTransaction();
    session.close();

    
    return listDTO; 
}

    
Jos
  • 3
  • 1
  • 1
    Does this answer your question? [mapping Hibernate query results to custom class?](https://stackoverflow.com/questions/37420401/mapping-hibernate-query-results-to-custom-class) – Curiosa Globunznik Oct 29 '20 at 17:02
  • @Jos What hibernate version do you use? – SternK Oct 29 '20 at 17:15
  • Hibernate 3, I have already changed the code by this, but doesn't work:List listDTO = session.createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(ProductDTO.class)).list(); – Jos Oct 29 '20 at 18:56

0 Answers0