-1

https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html

When and why JPA entities should implement the Serializable interface?

According to the Java specification, to write an object in jvm to a file, etc., it is said that serializable must be implemented.

However, this is clearly an incorrect explanation. So far, I have never implemented serializable when putting data into redis cache, session, or RDB.

Even if you don't implement serializable, it is normally written outside the JVM memory, such as a file, so I don't understand why you need to implement this. As I said before, I've never used serializable and it's been working fine so far.

JavaMe
  • 11
  • 2
  • I recommend you to add some code to your post for everyone can help you out properly with your issue. – Jorge Luiz May 25 '22 at 04:13
  • I don't think you're reading this correctly. Java's standard, default serialization is implemented with the `Serializable` interface. Objects can certainly be serialized in other ways. So...it sounds like you already know the answer, insofar as you have a question. You don't need to implement `Serializable` for your purposes. You've demonstrated that for yourself. – Louis Wasserman May 25 '22 at 04:36
  • No code to paste. Spring jpa, redis, mybatis I have never implemented Serializable using any framework library. – JavaMe May 25 '22 at 04:53
  • The specification and parts of it that are referred to are not JPA - they are EJB and restrictions on using entities within a container where 'detached' entities can be passed around via java serialization. Calling remote EJBs and passing java objects around them requires they implement the serializable interface. If you don't do that, they don't need to implement serializable. Redis caches, sessions and RDBs don't use java serialization, and JPA/Orms use their own mappings to serialize the data into databases, so it isn't a JPA requirement. – Chris May 25 '22 at 13:47

1 Answers1

0

You only need the Serializable interface to read or write objects using Java's built-in serialization mechanism, such as reading or writing with the ObjectInputStream, ObjectOutputStream, ObjectInput, and ObjectOutput standard library classes specifically.

There are other ways of performing serialization that do not rely on this built-in mechanism. That's what those libraries you mentioned are using.

When you see the word "serialize" in the Java documentation, it is usually talking about this specific built-in serialization mechanism, not all serialization in general.

Tenfour04
  • 59,798
  • 9
  • 62
  • 120
  • As per your answer there is absolutely no need to implement Serializable. Because session, db, cache serialization/deserialization works fine even without implementing this. – JavaMe May 25 '22 at 04:54
  • That is exactly what his answer is stating... It clearly states that the serialization is only needed when using the default java serialization mechanisms of `ObjectInputStream`/`ObjectOutputStream`. Or to put it more clearly if you only use the JDK for storing something on disk you need `Serializable` if you use an external mechanism (caching, session, db) you don't need it. (Which is **exactly** as the answer describes). – M. Deinum May 25 '22 at 05:38
  • The answer is not complete though as it occurs without direct use of Object streams and writers. It is used within containers as well, which is why it was mentioned in JSR 220 in relation to JPA entities. Any java object passed to/from remote EJBs should implement serializable. – Chris May 25 '22 at 13:51
  • 1
    @Chris Good point. I removed the text that implied that was an exhaustive list of ways to use it. You're welcome to expand on the answer, although I'm not sure it will be helpful for what the OP specifically wants to know. – Tenfour04 May 25 '22 at 14:31