我必须编写一个JPA查询来选择MySQL数据库中的数据。
我有这部分代码:
SELECT NEW com.fiat.sc.um.mdm.bean.Vendor
(e.fgpVndrNumber, e.companyName1, e.vndrCountry,
e.city, e.postalCode, e.street, e.vatRegistrNum,
e.vendorTypeDesc, e.companyName2, e.taxNumber2,
e.taxNumber1, e.organizationCode)
FROM ScVendor e
我想在where子句中添加:
WHERE str(e.fgpVndrNumber) LIKE '%123%'
或
to_char(e.fgpVndrNumber) LIKE '%123%'
简而言之,我必须在String中转换一个Integer。
但我得到了一个错误。
建议?
修改
这是完整的Java代码:
String selAndForm = "SELECT NEW com.fiat.sc.um.mdm.bean.Vendor(e.fgpVndrNumber, e.companyName1, e.vndrCountry,"
+ " e.city, e.postalCode, e.street, e.vatRegistrNum, e.vendorTypeDesc, e.companyName2, e.taxNumber2, "
+ "e.taxNumber1, e.organizationCode) FROM ScVendor e ";
String where = null;
if (isNotNull(companyName)) {
if (searchCriteriaCompName != null) {
if (searchCriteriaCompName.equals("contains")) {
companyName = "%" + companyName + "%";
}
if (searchCriteriaCompName.equals("startWith")) {
companyName = companyName + "%";
}
if (searchCriteriaCompName.equals("endWith")) {
companyName = "%" + companyName;
}
}
if (where == null) {
where = "WHERE lower(e.companyName1) like '" + companyName.toLowerCase() + "'";
} else {
where = where + " AND lower(e.companyName1) like '" + companyName.toLowerCase() + "'";
}
}
if (isNotNull(immediateParent)) {
if (searchCriteriaParentCode != null) {
if (searchCriteriaParentCode.equals("contains")) {
immediateParent = "%" + immediateParent + "%";
}
if (searchCriteriaParentCode.equals("startWith")) {
immediateParent = immediateParent + "%";
}
if (searchCriteriaParentCode.equals("endWith")) {
immediateParent = "%" + immediateParent;
}
if (searchCriteriaParentCode.equals("equals")) {
// do nothing
}
}
if (where == null) {
where = "WHERE e.immediateParent LIKE '" + immediateParent + "'";
} else {
where = where + " AND e.immediateParent LIKE '" + immediateParent + "'";
}
}
if (isNotNull(supplierCode)) {
logger.debugT("supplierCode=" + supplierCode);
if (searchCriteriaParentCode != null) {
if (searchCriteriaSuppCode.equals("contains")) {
supplierCode = "%" + supplierCode + "%";
}
if (searchCriteriaSuppCode.equals("startWith")) {
supplierCode = supplierCode + "%";
}
if (searchCriteriaSuppCode.equals("endWith")) {
supplierCode = "%" + supplierCode;
}
if (searchCriteriaSuppCode.equals("equals")) {
// do nothing
}
}
if (where == null) {
where = "WHERE e.fgpVndrNumber.toString() like '" + supplierCode + "'";
} else {
where = where + " AND e.fgpVndrNumber.toString() like '" + supplierCode + "'";
}
}
StringBuffer SQL = new StringBuffer();
SQL.append(selAndForm);
SQL.append(where);
SQL.append(" AND e.deletedAt IS NULL");
logger.debugT(SQL.toString());
Query query = eManager.createQuery(SQL.toString());
fgpVendorNumber是一个整数。我想用String转换它。
答案 0 :(得分:0)
搜索部分号码无需显式投射。
以下查询格式应该没有语法或逻辑错误。
select ...
from ...
where e.fgpVndrNumber like '%123%'
答案 1 :(得分:0)
答案 2 :(得分:0)
在JPA JPQL中尝试将整数与字符串进行比较会导致错误。
我能够将Integer转换成可以进行比较的String:
WHERE substring(xInteger, 0) like '%stringFilter%'
答案 3 :(得分:0)
我可以在我的JPA查询中使用TRIM:
WHERE TRIM(e.fgpVndrNumber as varchar) LIKE '%123%'