Whether using Hibernate or MyBatis in conjunction with spring for development or otherwise, persistent classes in the system tend to be serialized, implements Serializable. I'm still curious, why should I do this? Always only know a general, learning hibernate and MyBatis, some of the books have a few words of introduction, some books cited examples or and spring integration with the example also did not realize serialization, sometimes in your project, if not implement Serializable interface, The system may also be normal without errors.
Finally have this time to find out for yourself this reason, turn over the book, look at the webpage, for this problem data is still relatively small, but I also learned some, on the PO to realize the serialization has a deeper understanding, why to achieve the serialization of the reasons summarized as follows:
1. Hibernate when using a component as a composite primary key, the component class must satisfy one condition: Implement the Java.io.serializable interface.
2. When the cache is configured in MyBatis, the persistence layer needs to be serialized. The cache element <cache> has a ReadOnly property, and the ReadOnly property can be set to TRUE or false. Read-only caching will return the same instance for all callers. Therefore, they cannot be modified, which can greatly improve performance. The writable cache will return a copy of the cached object by serialization. This will be slow, but more secure. So the default value is False.
3, persistent storage, save the state of the object in the storage media so that the exact same copy can be recreated later.
4, by the value of marshaling, especially in the distributed system. If the object is marked as Serializable, the object is automatically serialized, transferred from one application domain to another application domain, and then deserialized to produce an exact copy of the object in the second application domain.
Reprint please indicate-java my life (Chen Leixing) Source: http://blog.csdn.net/chenleixing/article/details/43833413
Why is Spring persistent class PO or javabean often serialized?