NamedQuery可选参数,数据类型不一致

NamedQuery optional parameter, inconsistent data types

我正在执行下面的命名查询:

@NamedQuery(name = Employment.EMPLOYEES,
                query = "select e from Employmente left join fetch e.person p where" +
                        " e.endDate is not null " +
                        " and (:paramFromDate is null or e.endDate >= :paramFromDate)" +
                        " and (:paramToDate is null or e.endDate <= :paramToDate)")

其中 paramFromDate 和 paramToDate 是可选的。

但是生成的查询会抛出错误;

Caused by: java.sql.SQLSyntaxErrorException: ORA-00932: inconsistent datatypes: expected DATE got BINARY

有什么建议吗?

命名查询不是动态的,但您可以尝试这个解决方案:

@NamedQuery(name = Employment.EMPLOYEES, query = Employment.NAMED_QUERY)
@Entity
public class Employment{
    private static String NAMED_QUERY= "default query";
    //+your persistent fields/properties...
}

public String buildQuery(Date paramFromDate,Date paramToDate){

    if(paramFromDate != null ){
        NAMED_QUERY="your query using paramFromDate param";
    }
    if(paramToDate != null ){
        NAMED_QUERY="your query using paramToDate param";
    }
}

//and later in your code
Employment.buildQuery(paramFromDate,paramToDate);
TypedQuery<MyEntity> query = entityManager.createQuery(Employment.NAMED_QUERY , Employment.class);