带IN语句的JPA本机查询



我正在尝试在springboot JPA 中使用nativeQuery

Query(value = "select * from sms.outgoing_message where create_ts between timestamp(?1) and timestamp(?2) and (error_code IN (?3) or (error_code = '0' and error_msg not like '%Opt out%') or (error_code = '1' and (error_msg like '%21211%' or error_msg like '%21612%' or error_msg = 'Mobile number format is wrong')))", nativeQuery = true)
List<OutgoingMessage> getBounceBackPhoneNumbers(LocalDateTime from, LocalDateTime to, List<String> listSoftBounceErrorCodes);

以上查询返回空结果。

当我尝试像3000830005300073000330006这样的错误代码而不是?3它给了我更多的结果

你知道问题出在哪里吗?

如果将LocalDateLocalDateTime与JPA一起使用,则需要自己定义到java.sql.Date或java.sql.Timestamp的映射。

在这种情况下,LocalDateTime和java.sql.Timestamp之间的转换是使用Timestamp的转换方法完成的。

import java.time.LocalDateTime;
import java.sql.Timestamp;
@Converter(autoApply = true)
public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> {
@Override
public Timestamp convertToDatabaseColumn(LocalDateTime locDateTime) {
return locDateTime == null ? null : Timestamp.valueOf(locDateTime);
}
@Override
public LocalDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
return sqlTimestamp == null ? null : sqlTimestamp.toLocalDateTime();
}
}

参考:持久本地日期本地日期时间jpa

最新更新