【发布时间】:2022-02-01 09:45:37
【问题描述】:
我有一个实体 A 具有以下列定义:
@Column(name = "project_address", nullable = false, length = 500)
@Convert(converter = EncodeDecodeAttribute.class)
private String projectAddress;
EncodeDecodeAttribute.class:
public class EncodeDecodeAttribute implements AttributeConverter<String, String> {
...
@Override
public String convertToDatabaseColumn(String s) {
try {
if(StringUtils.isEmpty(s))
return s;
return Base64.getEncoder().encodeToString(encryptCipher.doFinal(s.getBytes()));
} catch (IllegalBlockSizeException | BadPaddingException e) {
log.error("Error convert to database column for value {} with msg {}",s,e);
throw new IllegalArgumentException(e);
}
}
@Override
public String convertToEntityAttribute(String s) {
try {
if(StringUtils.isEmpty(s))
return s;
return new String(decryptCipher.doFinal(Base64.getDecoder().decode(s)));
} catch (IllegalBlockSizeException | BadPaddingException e) {
log.error("Error convert to entity attribute for value {} with msg {}",s,e);
throw new IllegalArgumentException(e);
}
}
}
现在我想在列 projectAddress 上实现 like 子句(通配符)和不区分大小写的搜索。
如何做到这一点?
以下是我当前的代码,它仅适用于完全匹配。
public static Specification<A> applyProjectAddressLike(String projectAddress) {
return ((root, criteriaQuery, criteriaBuilder) ->
Objects.nonNull(projectAddress) ?
criteriaBuilder.like(root.get(A_.projectAddress), projectAddress) :
criteriaBuilder.and()
);
现在如果我将“ABC%”或“%ABC”作为 projectAddress 传递,那么整个单词(包括 %)将被加密,然后 JPA 将运行查询,因此不会得到匹配的结果。
提前感谢您的帮助。
【问题讨论】:
标签: hibernate encryption spring-data-jpa jpql