Hibernate Criteria示例

时间:2011-11-15 05:46:25

标签: java hibernate swing desktop-application criteria

我有桌子'Stores'和'Item'。项目表包含“类别”,“品牌”,“项目代码”列。 此外,我将类别详细信息和品牌详细信息保存在另外两个名为Category and Brands的表中。商店表有名为item和qty的locums。我的hibernate实体类是Stores,Item,Category,Brand 我希望通过hibernate从商店获取给定商品的商品。根据用户的偏好设置项目的品牌和品牌名称。(这意味着,用户从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))
但返回了商店中的所有商品。如何根据给定项目从商店获取商品。

2 个答案:

答案 0 :(得分:0)

您是否在Item类中定义了equals / hashcode?

答案 1 :(得分:0)

               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()); 
            }