Java CriteriaBuilder和/或条件未应用必需/正确的括号
我是这个董事会的新成员。我试图找到解决办法,但找不到解决办法。如果有,请与我分享Java CriteriaBuilder和/或条件未应用必需/正确的括号,java,sql,spring,hibernate,spring-data-jpa,Java,Sql,Spring,Hibernate,Spring Data Jpa,我是这个董事会的新成员。我试图找到解决办法,但找不到解决办法。如果有,请与我分享 CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder(); CriteriaQuery<DmdInventory> criteriaQuery = criteriaBuilder.createQuery(ItemInventory.class); Root<ItemInventory&
CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();
CriteriaQuery<DmdInventory> criteriaQuery = criteriaBuilder.createQuery(ItemInventory.class);
Root<ItemInventory> itemRoot = criteriaQuery.from(ItemInventory.class);
Predicate first = criteriaBuilder.equal(itemRoot.get("margin"), 1.0);
first = criteriaBuilder.and(first, criteriaBuilder.equal(itemRoot.get("costWithoutExpense"), 1.0));
first = criteriaBuilder.and(first, criteriaBuilder.equal(itemRoot.get("markup"), 1.0));
Predicate second = criteriaBuilder.and(criteriaBuilder.equal(itemRoot.get("lastImportCost"), 2.0));
second = criteriaBuilder.and(second, criteriaBuilder.equal(itemRoot.get("expenses"), 2.0));
second = criteriaBuilder.and(second, criteriaBuilder.equal(itemRoot.get("profit"), 2.0));
Predicate third = criteriaBuilder.and(criteriaBuilder.equal(itemRoot.get("totalCost"), 2.0));
third = criteriaBuilder.and(third, criteriaBuilder.equal(itemRoot.get("cost"), 2.0));
third = criteriaBuilder.and(third, criteriaBuilder.equal(itemRoot.get("quantity"), 2.0));
Predicate allPredicate[] = { first, second, third };
Predicate finalPredicate = criteriaBuilder.or(allPredicate);
criteriaQuery.where(finalPredicate);
List<ItemInventory> items = this.entityManager.createQuery(criteriaQuery).getResultList();
预期结果
select * from
item_nventory inven
where
inven.last_import_cost=2.0
and inven.expenses=2.0
and inven.profit=2.0
or (inven.total_cost=2.0
and inven.cost=2.0
and inven.quantity=2 )
or (inven.margin=1.0
and inven.cost_without_expense=1.0
and inven.markup=1.0)
我的最终要求如下
select * from
item_nventory inven
where
inven.last_import_cost=2.0
and inven.expenses=2.0
and inven.profit=2.0
or ((inven.total_cost=2.0
and inven.cost=2.0
and inven.quantity=2 ) or (inven.quantity_on_hand>0))
or (inven.margin=1.0
and inven.cost_without_expense=1.0
and inven.markup=1.0)
请在这方面帮助我。请提供错误的完整堆栈跟踪。我没有收到任何错误。除了最后一行返回语句之外,我还提供了方法的代码。运行代码时生成的SQL。同时,我还提供了我正在查看的输出。看起来您的
预期结果中有一个输入错误:在下。。。投资利润=2.0(或投资总成本=2.0…
你的意思是…投资利润=2.0或投资总成本=2.0…
?在最终要求中也是一样的谢谢你让我知道我的打字错误。我更正了SQL。我希望要求现在就清楚了。
select * from
item_nventory inven
where
inven.last_import_cost=2.0
and inven.expenses=2.0
and inven.profit=2.0
or ((inven.total_cost=2.0
and inven.cost=2.0
and inven.quantity=2 ) or (inven.quantity_on_hand>0))
or (inven.margin=1.0
and inven.cost_without_expense=1.0
and inven.markup=1.0)