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
Java JPA标准生成器中的If/Case语句_Java_Jpa_Jpa 2.0_Criteria Api - Fatal编程技术网

Java JPA标准生成器中的If/Case语句

Java JPA标准生成器中的If/Case语句,java,jpa,jpa-2.0,criteria-api,Java,Jpa,Jpa 2.0,Criteria Api,我想建立一个查询,在不同的实体上搜索日期。我的结构是: 合同有日期(不可撤销) 员工有日期(不可为空) 员工可能有合同id(可为空) 如果员工有合同,我希望检索合同日期。如果员工没有合同,那么我想返回员工日期 到目前为止,我的代码是: if (inputDate!= null) { ParameterExpression<Date> exp = criteriaBuilder.parameter(Date.class, "inputDate"); criteria

我想建立一个查询,在不同的实体上搜索日期。我的结构是:

  • 合同有日期(不可撤销)
  • 员工有日期(不可为空)
  • 员工可能有合同id(可为空)
如果员工有合同,我希望检索合同日期。如果员工没有合同,那么我想返回员工日期

到目前为止,我的代码是:

if (inputDate!= null) {
    ParameterExpression<Date> exp = criteriaBuilder.parameter(Date.class, "inputDate");
    criteria.add(criteriaBuilder.or(
        criteriaBuilder.isNull(employee.get("contract")),
        criteriaBuilder.lessThanOrEqualTo(employee.<Date>get("creationDate"), exp),   criteriaBuilder.lessThanOrEqualTo((employee.join("contract").<Date>get("fromDate")), exp) ));}
if(inputDate!=null){
ParameterExpression exp=criteriaBuilder.parameter(Date.class,“inputDate”);
添加(criteriaBuilder.or)(
criteriaBuilder.isNull(employee.get(“合同”)),
criteriaBuilder.lessThanOrEqualTo(employee.get(“creationDate”),exp),criteriaBuilder.lessThanOrEqualTo((employee.join(“合同”).get(“fromDate”)),exp));}
但这似乎不起作用。我似乎总是进入我不期望的isNull

我很高兴再深入探讨一下,但我想我的问题是,这是否是正确的做法。它是?我在criteriaBuilder中也看到了selectCase,所以这可能是一个更好的解决方案

任何指示都会受到极大的欢迎


谢谢

这里有一个解决方案,不确定它是否有效,但我们会在您的帮助下设法使它工作:):

ParameterExpression inputDateExp=criteriaBuilder.parameter(Date.class,“inputDate”);
谓词employeeCreationDate=criteriaBuilder.lessThanOrEqualTo(
employee.get(“creationDate”),inputDateExp);
谓词contractStartDate=criteriaBuilder.lessThanOrEqualTo(
employee.join(“合同”).get(“fromDate”)、inputDateExp);
标准.添加(
criteriaBuilder.selectCase().when(employee.get(“合同”).isNull(),employeeCreationDate)。否则(contractStartDate));

我不明白为什么用“inputDate”作为表达式而不是日期?另外,我建议将
标准
重命名为
c
,将
标准生成器
重命名为
cb
,这样可以节省空间,而且我觉得更舒适。

我想你要做的是使用
案例
,并且在时只将
谓词
用作
案例#的第一个参数
Case
是一个
表达式
,您希望将其传递到
CriteriaQuery#multiselect

CriteriaQuery<Employee> query = criteriaBuilder.createQuery(Employee.class);
Root<Employee> employee = query.from(Employee.class);
query.multiselect
(
    criteriaBuilder.selectCase()
        .when(criteriaBuilder.isNull(employee.get("contract")), employee.get("creationDate"))
        .otherwise(employee.join("contract").get("fromDate"))
);
CriteriaQuery query=criteriaBuilder.createQuery(Employee.class);
Root employee=query.from(employee.class);
query.multiselect
(
criteriaBuilder.selectCase()
.when(criteriaBuilder.isNull(employee.get(“合同”)),employee.get(“creationDate”))
。否则(雇员。加入(“合同”)。获取(“起始日期”))
);

你好,JMelnik,谢谢你的回复。这段代码的问题是selectCase返回一个表达式而不是谓词。它实际上是在返回谓词,因此尝试将整个事件的大小写为谓词。不幸的是,它仍然没有编译,我觉得很奇怪,因为我正在铸造。你知道这可能是什么吗?Thanks@user846476我想我们需要做的是在案例陈述中比较日期。太好了,谢谢你给我指出这个。这应该是公认的答案。
CriteriaQuery<Employee> query = criteriaBuilder.createQuery(Employee.class);
Root<Employee> employee = query.from(Employee.class);
query.multiselect
(
    criteriaBuilder.selectCase()
        .when(criteriaBuilder.isNull(employee.get("contract")), employee.get("creationDate"))
        .otherwise(employee.join("contract").get("fromDate"))
);