Uda*_*day 2 jpa criteria criteria-api
我有三个表,其中一个是 ItemCategory、ItemMaster 和 Price。我在 ItemMaster 表中引用 itemaCategoryId,就像在价格中引用 itemmasterid 一样。现在我必须按 itemcategory id 显示价格订单的内容。这是我的标准查询。
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Price> cq = cb.createQuery(Price.class);
Root<Price> root = cq.from(Price.class);
Root<ItemMaster> itemMasterRoot = cq.from(ItemMaster.class);
Root<ItemCategory> itemCategoryRoot = cq.from(ItemCategory.class);
Join<ItemMaster, ItemCategory> s=itemMasterRoot.join(ItemMaster_.category);
Join<Price,ItemMaster> y=root.join(Price_.itemMaster);
Path<Long> itemMasterId=root.get(Price_.itemMasterId);
cq.select(root).where(cb.equal(root.get(Price_.priceMasterId), priceMasterId))
.orderBy(cb.asc(itemMasterId));
TypedQuery<Price> q = entityManager.createQuery(cq);
Run Code Online (Sandbox Code Playgroud)
高于我的标准查询
如果使用多个from语句,则会得到所有实体的笛卡尔积。如果要保留关系,请改用 join:
Root<Price> price = cq.from(Price.class);
Join<Price,ItemMaster> itemMaster = price.join(Price_.itemMaster);
Join<ItemMaster, ItemCategory> itemCategory = itemMaster.join(ItemMaster_.category);
Run Code Online (Sandbox Code Playgroud)
但是看起来至少第二个连接可能没用,因为您可以category使用 getter 直接访问该属性,不是吗?:
Price aPriceResult;
ItemCategory categoryResult = aPriceResult.getItemMaster().getCategory();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18745 次 |
| 最近记录: |