gpt4 book ai didi

java - Hibernate 条件示例

转载 作者:行者123 更新时间:2023-11-29 09:18:10 25 4
gpt4 key购买 nike

我有表 'Stores' 和 'Item' 。 Item 表包含“Category”、“Brand”、“itemcode”列。此外,我将类别详细信息和品牌详细信息保存在另外 2 个名为类别和品牌的表中。 Stores 表具有名为 item 和 qty 的位置。我的 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);<br/>
cr.add(Example.create(stores))
但退回了商店中的所有元素。如何根据给定的商品从商店中获取商品。

最佳答案

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

关于java - Hibernate 条件示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8132023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com