4

I am using Redis as my database with Spring data, with no custom value or hash value serializer. I assume the standrad JdkSerializationRedisSerializer should be used then for objects serialization. It looks like transient fields of some objects are serialized to the database.

Transient field modifier, transient method annotations, @JsonIgnore properties - all of them don't seem to affect the serialization of the field.

How can I overcome this issue?

Matt
  • 129
  • 2
  • 13

3 Answers3

1

I know this is two years too late for poor Matt, but we recently ran into this issue, as well. What seems to work is:

  1. Set a serialization ID on the Java class. Eclipse and IntelliJ will both generate one for you. -1 doesn't seem to work.
  2. Use the transient Java modifier, not the Spring @Transient tag.

:

public class CachedValues implements Serializable {
    private static final long serialVersionUID = -914745617137492359L;
    private Long id;
    private transient String nonRedisField;
    ...
}
Steve Gelman
  • 874
  • 9
  • 16
0

I had this issue and whilst I initially had marked the field as transient, which I believed would make it transient ;-) it didnt when it came to spring data redis.

What worked for me was annotating the field:

    ...
    import org.springframework.data.annotation.Transient;
    ...
    @Transient
    private ReadWriteLock lock = new ReentrantReadWriteLock();

the pojo did not have to implement Serializable and the field did not have to marked as transient. I guess its still good Java practice to do this, but it was not a requirement.

theINtoy
  • 3,388
  • 2
  • 37
  • 60
  • Having posted the above, I have since read this answer which might explain it a bit better: https://stackoverflow.com/a/35641998/1279002 – theINtoy Oct 09 '19 at 11:50
0

We had a same issue, I used transient(java keyword) and org.springframework.data.annotation.Transient annotation as well but it did not work, however using com.fasterxml.jackson.annotation.JsonIgnore did the trick.

It actually depends on how the underline redis is implemented and what serializer or deserializer it uses.

STA
  • 30,729
  • 8
  • 45
  • 59