我有“Stores”和“Item”表。Item表具有列'Category‘,'Brand’,'itemcode‘。此外,我将类别详细信息和品牌详细信息保存在另外2个名为category和Brands的表中。Stores表具有名为item和qty的locum。我的hibernate实体类是Stores、Item、Category、Brand。
我希望从商店中获得具有hibernate.Setting分类的给定商品,并根据用户的喜好确定商品的品牌名称。(这意味着,用户从swing用户界面中选择类别和品牌,然后应用程序必须设置具有给定类别和品牌名称的商品。)商品应该根据商店中的给定商品进行选择。下面是我是如何做到的:
Stores stores = new Stores();
Item item = new Item();
if (isBrandNameMode()) { //here checks if user select a brand in IF clause
item.setBrands(selectedBrand);
//selectedBrand is an brand object which user selected
}
if (isCategoryode()) {// here checks if user select a category in IF clause
item.setSizeModel(selectedSize);
//user selected
}
Criteria cr=HSession.getSession().createCriteria(Stores.class);
cr.add(Restrictions.eq("item", item));
List l=cr.list();
System.out.println("list size is: "+l.size());但在这里,所有商店的项目都会返回,但不是针对给定的项目。
我也试过用Critataria Example
stores.setItem(item);
cr.add(Example.create(stores)),但退还了商店里的所有商品。如何根据给定的商品从商店获取商品。
发布于 2011-11-15 15:21:45
您是否在Item类中定义了equals/hashcode?
发布于 2014-06-24 22:33:13
Criteria crt=session.createCriteria(Product.class);
List<Product> l=crt.list();
for(Product p:l)
{
System.out.println(p.getPrice());
System.out.println(p.getProductId());
System.out.println(p.getProName());
}https://stackoverflow.com/questions/8132023
复制相似问题