I'm working on a spring boot rest project. I have a web service that is searching a text in multiple fields. I'm using java specification to generate query.
Resulting query is like that
select *
from V_AGENCY agencyview0_
where
agencyview0_.nationcode like '%ABCDEFGHIKLMN%
or agencyview0_.username like '%ABCDEFGHIKLMN%'
The problem is that I'm getting java.sql.DataTruncation: Data truncation exception, because nationcode field has valid length 10 on database. Why I'm getting this exception? I'm not trying to insert a value to this field.
org.firebirdsql.jdbc.field.FBWorkaroundStringField class throws that error.
public void setString(String value) throws SQLException {
byte[] data = this.setStringForced(value);
if (value != null) {
assert data != null : "Expected non-null data here";
if (data.length > this.fieldDescriptor.getLength() && !this.isSystemTable(this.fieldDescriptor.getOriginalTableName()) && (value.length() > this.fieldDescriptor.getLength() + 2 || value.charAt(0) != '%' && value.charAt(value.length() - 1) != '%')) {
throw new DataTruncation(this.fieldDescriptor.getPosition() + 1, true, false, data.length, this.fieldDescriptor.getLength());
}
}
}
UPDATE - Spring Boot codes added
Controller:
@GetMapping(value = {PATH_SEARCH, PATH_LIST, PATH_VIEW + "/" + PATH_SEARCH, PATH_VIEW + "/" + PATH_LIST}, params = {PARAM_TEXT, PARAM_FIELD})
public List<T> searchInMultipleFields(
@RequestParam(name = PARAM_START, required = false) String start,
@RequestParam(name = PARAM_LIMIT, required = false) String limit,
@RequestParam(name = PARAM_TEXT) String text,
@RequestParam(name = PARAM_FIELD) List<String> fields
) {
OoSpecificationsBuilder<T> builder = new MultipleSearchSpecificationBuilder<>();
for (String field : fields) {
builder.with(field, ":", text.toUpperCase());
}
Specification<T> spec = builder.build();
return mService.getAll(getValueOf(start), getValueOf(limit, MAX_PAGE_SIZE), spec);
}
Service:
@Override
public List<T> getAll(int aStart, int aSize, Specification<T> aSpec) {
return getRepository().findAll((Specification) aSpec, generatePageRequest(aStart, aSize)).getContent();
}
JpaSpecificationExecutor:
Page<T> findAll(@Nullable Specification<T> var1, Pageable var2);
Specification:
public class MultipleSearchSpecification<T extends BaseModel> implements Specification<T> {
private SearchCriteria criteria;
public MultipleSearchSpecification(SearchCriteria aCriteria) {
criteria = aCriteria;
}
@Override
public Predicate toPredicate
(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
if (root.get(criteria.getKey()).getJavaType() == String.class) {
return builder.like(
root.get(criteria.getKey()), "%" + criteria.getValue() + "%");
}
return null;
}}
Do you know any workaround for this issue?