Homemade vs. Java Serialization

前端 未结 14 729
离开以前
离开以前 2021-02-04 06:16

I have a certain POJO which needs to be persisted on a database, current design specifies its field as a single string column, and adding additional fields to the table is not a

相关标签:
14条回答
  • 2021-02-04 06:53

    Consider changing your schema. Even if you find a quick way to serialize a POJO to a string how do you handle different versions? How do you migrate the database from X->Y? Or worse from A->D? I am seeing issues where we stored a serialize object into a BLOB field and have to migrate a customer across multiple versions.

    0 讨论(0)
  • 2021-02-04 06:53

    Have you looked into JAXB? It is a mechanism by which you can define a suite of java objects that are created from an XML Schema. It allows you to marshal from an object hierarchy to XML or unmarshal the XML back into an object hierarchy.

    0 讨论(0)
  • 2021-02-04 06:54

    You might try Protocol Buffers, it is a open-source project from Google, it is said to be fast (generates shorter serialized form than XML, and works faster). It also handles addition of new field gently (inserts default values).

    0 讨论(0)
  • 2021-02-04 06:54

    I'd say your initial approach is not all that bad if your POJO consists of Strings and primitive types. You could enforce escaping of the delimiter to prevent corruptions. Also if you use Hibernate you encapsulate the serialization in a custom type.

    If you do not mind another dependency, Hessian is supposedly a more efficient way of serializing Java objects.

    0 讨论(0)
  • You might try Preon. Preon aims to be to binary encoded data what Hibernate is to relational databases and JAXB to XML.

    0 讨论(0)
  • 2021-02-04 06:56

    XStream or YAML or OGNL come to mind as easy serialization techniques. XML has been the most common, but OGNL provides the most flexibility with the least amount of metadata.

    0 讨论(0)
提交回复
热议问题