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

时间:2016-04-14 16:40:55

标签: hibernate hql named-query

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

@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

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

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

@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);