Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jpa 在条件查询中联接两个表_Jpa_Criteria_Criteria Api - Fatal编程技术网

Jpa 在条件查询中联接两个表

Jpa 在条件查询中联接两个表,jpa,criteria,criteria-api,Jpa,Criteria,Criteria Api,我有三个表,一个是ItemCategory,ItemMaster和Price。我在ItemMaster表中引用itemaCategoryId,在price中引用itemmasterid。现在我必须按itemcategory id显示价格订单的内容。这是我的条件查询 CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Price> cq = cb.createQuery(Price.cla

我有三个表,一个是ItemCategory,ItemMaster和Price。我在ItemMaster表中引用itemaCategoryId,在price中引用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);
CriteriaBuilder cb=entityManager.getCriteriaBuilder();
CriteriaQuery cq=cb.createQuery(Price.class);
Root=cq.from(Price.class);
根itemMasterRoot=cq.from(ItemMaster.class);
根itemCategoryRoot=cq.from(ItemCategory.class);
Join s=itemMasterRoot.Join(ItemMaster\uU2.category);
Join y=root.Join(Price.itemMaster);
路径itemMasterId=root.get(Price\ux.itemMasterId);
cq.select(root).where(cb.equal(root.get(Price\uu.priceMasterId),priceMasterId))
.orderBy(cb.asc(itemMasterId));
TypedQuery q=entityManager.createQuery(cq);
在我的条件查询上方

如果使用多个语句,则得到所有实体的笛卡尔乘积。如果要保留关系,请改用“联接”:

Root<Price> price = cq.from(Price.class);
Join<Price,ItemMaster> itemMaster = price.join(Price_.itemMaster);
Join<ItemMaster, ItemCategory> itemCategory = itemMaster.join(ItemMaster_.category);
如果使用多个语句,则得到所有实体的笛卡尔积。如果要保留关系,请改用“联接”:

Root<Price> price = cq.from(Price.class);
Join<Price,ItemMaster> itemMaster = price.join(Price_.itemMaster);
Join<ItemMaster, ItemCategory> itemCategory = itemMaster.join(ItemMaster_.category);

为什么在价格上有itemMasterId和itemMaster属性?它们的意思是一样的吗?不是。价格表只引用itemmasterid。我必须按itemcategoryid显示价格表和订单的内容,这是我需要的。但不知道如何加入标准。为什么在价格中有itemMasterId和itemMaster属性?它们的意思是一样的吗?不是。价格表只引用itemmasterid。我必须按itemcategoryid显示价格表和订单的内容,这是我需要的。但我不知道如何正确地加入标准。最后,我使用您的代码编写了这样的代码,但给出了errorcq.multiselect(itemcegory).where(cb.equal(root.get(Price.priceMasterId),priceMasterId)).orderBy(cb.asc(itemcegory.get(itemcegory.id));为什么要多选?使用select.finally我得到了@perissf。谢谢你的回复。这是我写的。cq.select(root).where(cb.equal(root.get(Price\priceMasterId),priceMasterId)).orderBy(cb.asc(itemCategoryJoin.get(ItemCategory\uMasterid));最后,我使用您的代码编写了这样的代码,但给出了errorcq.multiselect(itemcegory).where(cb.equal(root.get(Price.priceMasterId),priceMasterId)).orderBy(cb.asc(itemcegory.get(itemcegory.id));为什么要多选?使用select.finally我得到了@perissf。谢谢你的回复。这是我写的。cq.select(root).where(cb.equal(root.get(Price\priceMasterId),priceMasterId)).orderBy(cb.asc(itemCategoryJoin.get(ItemCategory\uMasterid));