包含多列的JPA查询,如果空列数据忽略查询和数据中的列

时间:2018-06-21 14:35:34

标签: java jpa spring-data-jpa spring-data

我是新的JPA。我的查询如下。如果column字段为空或为null,我可以使用LIKE运算符进行检查 或者我可以忽略该列进行查询,我不知道如何过滤查询

 String query = "SELECT REGION_ID FROM CacheableDevice cd WHERE ";

if (serialNumber != null && WebserviceUtil.isNonEmpty(serialNumber)) {
    query += "cd.SERIAL_NUMBER = ?1";

} else {
    query += "cd.SERIAL_NUMBER LIKE %?1%";
}
if (memberName != null && WebserviceUtil.isNonEmpty(memberName)) {
    query += "cd.MEMBER_NAME = ?2";

} else {
    query += "cd.MEMBER_NAME LIKE %?2%";
}
if (crmNumber != null && WebserviceUtil.isNonEmpty(crmNumber)) {
    query += "cd.CRM_NUMBER = ?3";

} else {
    query += "cd.CRM_NUMBER LIKE %?3%";
}
if (model != null && WebserviceUtil.isNonEmpty(model)) {
    query += "cd.MODEL_TYPE = ?4";

} else {
    query += "cd.MODEL_TYPE LIKE %?4%";
}


@Query(query)

如何处理这种情况?

0 个答案:

没有答案