I'm using spring redis and I'm getting this error when I try to retrieve the entity from redis. The issue is in lastModifiedTime field in AuditedEntiy class.
{error: "No converter found capable of converting from type [byte[]] to type [java.sql.Timestamp]"}
I already added a converter from byte array to Timestamp, but the issue persist. Current implementation has one endpoint to store the entity in redis and another one to store the stored redis entity in a relational DB(mysql) and update the id in the redis entity(we maintan same id for the entity stored in redis and in the DB), after that step, when we try to get the entity from redis i get the error mentioned above.
The redis instance is hosted on amazon a weird thing that's happening is when I run my local spring boot application it works, but on amazon hosted instance I get this error.
DB ENTITY:
@MappedSuperclass
@Audited
@EntityListeners(AuditingEntityListener.class)
public abstract class AuditedEntity {
@CreatedBy
@LastModifiedBy
protected Long lastModifiedBy;
@CreatedDate
@LastModifiedDate
@Temporal(TIMESTAMP)
protected Date lastModifiedTime;
public Long getLastModifiedBy() {
return lastModifiedBy;
}
public void setLastModifiedBy(Long lastModifiedBy) {
this.lastModifiedBy = lastModifiedBy;
}
public Date getLastModifiedTime() {
return lastModifiedTime;
}
public void setLastModifiedTime(Date lastModifiedTime) {
this.lastModifiedTime = lastModifiedTime;
}
}
@Entity
@Audited
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
public @Data class DomainEntity extends AuditedEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@EqualsAndHashCode.Include
private Long id;
@NotNull
private String name;
private String description;
private Boolean locked = Boolean.FALSE;
@Version
private Integer version;
@NotNull
private Long tenant;
}
REDIS ENTITY:
@RedisHash("pricingAnalysis")
@JsonIgnoreProperties(ignoreUnknown = true)
public @Data class DomainEntityDto {
@Id
@JsonSerialize(using = LongIdToStringSerializer.class)
private Long id;
private String name;
private String description;
private Boolean locked;
private Integer version;
@CustomDateFormat("MM/dd/yyyy hh:mm a z")
@JsonSerialize(using = DateSerializer.class)
@JsonDeserialize(using = DateDeserializer.class)
private Date lastModifiedTime;
private String lastModifiedBy;
private Long tenant;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
private LocalDate observationFrom;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy hh:mm a")
private LocalDateTime observationTo;
public PricingAnalysisDto updateObservationPeriod() {
Optional<DateRangeDto> observationPeriod = Optional.ofNullable(getSearch())
.map(SearchDto::getParams).map(ParamsDto::getTrade).map(TradeParamsDto::getObserved)
.filter(obs -> obs.getFrom() != null && obs.getTo() != null);
if (observationPeriod.isPresent()) {
this.setObservationFrom(observationPeriod.get().getFrom().toInstant()
.atZone(ZoneId.systemDefault()).toLocalDate());
this.setObservationTo(observationPeriod.get().getTo().toInstant()
.atZone(ZoneId.systemDefault()).toLocalDateTime());
}
return this;
}
}
BYTES TO DATE CONVERTER ADDED:
@Component
@ReadingConverter
public class BytesToDateConverter implements Converter<byte[], Timestamp> {
@Override
public Timestamp convert(final byte[] source) {
String value = new String(source);
return new Timestamp(Long.parseLong(value));
}
}