I use Spring & Hibernate and i would like to get a product with his id in my DAO.
@Repository
@Transactional
public class ProductDaoImpl implements ProductDao {
protected final Log logger = LogFactory.getLog(getClass());
@Autowired
private SessionFactory sessionFactory;
public List<Product> getProductList() {
return sessionFactory.getCurrentSession().createQuery("from Product p order by p.productName asc").list();
}
public Product getProductById(int productId) {
String hql = "from Product p where p.productId = :id";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setInteger("id", productId);
return null;
}
}
For example when i would like to get all my products i return list of them (calling function getProductList() ), but now i want to call getProductById but i don’t know how i could return something with the “Product” type.
Thanks.
In your getProductById(int productId) method: