Serialize objects containing objects java

2020-07-22 03:54发布

I try to serialize an object that contains an other object which is itself serializable.

Is there a way to make it work ? The pointer to the other object seems to be lost when I serialize the first object.

7条回答
【Aperson】
2楼-- · 2020-07-22 04:07

You might be happy using SOJO. It serializes the entire hierarchy of a Java object pretty easily.

查看更多
相关推荐>>
3楼-- · 2020-07-22 04:11

I exactly feel the same, the inner object value will be also serializable until and unless it is being marked transient. If the inner object class was not serializable then it would throw a compile time error.

Kindly let us know if u did solve it and if my understanding is wrong.

查看更多
SAY GOODBYE
4楼-- · 2020-07-22 04:16

Here are two rules concerning persistent objects:

  • Rule #1: The object to be persisted must implement the Serializable interface or inherit that implementation from its object hierarchy
  • Rule #2: The object to be persisted must mark all nonserializable fields transient

Check the article "Discover the secrets of the Java Serialization API". It's include source code and a good explanation of the serialization process.

Source code answering "How do I serialize a tree of objects?" is also available at http://docs.oracle.com/javase/8/docs/technotes/guides/rmi/faq.html#tree

To know some "under the hood" details check this excellent article: The Java serialization algorithm revealed.

查看更多
不美不萌又怎样
5楼-- · 2020-07-22 04:19

The "pointer" is lost indeed. After deserialization new objects are constructed.

Before you provide your code - check whether your 2nd object isn't defined as transient - this stops java serialization.

查看更多
仙女界的扛把子
6楼-- · 2020-07-22 04:20

It should work just fine, so long as everything's serializable. What do you mean by the pointer (by which I assume you mean reference) being "lost"?

Could you provide a short but complete program which demonstrates the problem? Admittedly Java binary serialization can be fiddly at times, but this much should work fine out of the box.

查看更多
再贱就再见
7楼-- · 2020-07-22 04:20

https://github.com/gadieichhorn/prototype-jpa

I created this github project that includes some serialisation into a @Lob please check it out and let me know if you have any comments.

  • maven
  • jpa - eclipse link - derby
  • slf4j - logback
  • junit - hamcrest

feedback is welcomed.

查看更多
登录 后发表回答