Hibernate query help
I have a Product Class which has one-to-many relationship with ProductInventory Class. For a particular product id, I have around 300 inventory.
If I want to fetch a product based on its id and inventory withing a specified date range, how do i execute a query?
I can go with either hql or criteria..But I'm not sure how to implement it as i'm a beginner in hibernat开发者_运维知识库e. Please help.
HQL Example,
String hql = "FROM Product p WHERE p.id = :id AND p.inventory.date BETWEEN :date1 AND :date2";
Query query = session.createQuery(hql);
query.setParameter("id", id);
query.setParameter("date1", date1);
query.setParameter("date2", date2);
List<Product> products = query.list();
for(Product product : products) {
System.out.println(product.getId() + "...");
List<ProductInventory> inventories = product.getProductInventory();
for(ProductInventory inventory : inventories) {
System.out.println(inventory.get...);
}
}
I will recommend to use criteria and for date range you can use
criteria.add(Restrictions.between(column_name, minValue,maxValue)).list();
For more reference visit here
精彩评论