What's the easiest way to persist java objects?

James picture James · Dec 23, 2009 · Viewed 21k times · Source

Right now I have java program whose classes are currently POJOs and stored in volatile memory. These need to be persisted. As I understand it two popular choices are JDO and the Java Persistence API. For someone who know little about SQL, Torque, etc, which is the easiest way to add persistence to my program's data?

Answer

Brian Agnew picture Brian Agnew · Dec 23, 2009

The traditional way to serialise to the filesystem is to use Java Serialisation. However you need to implement Serializable everywhere.

A simpler solution is to serialise to XML (and then dump to the filesystem) using XStream. You don't need to implement any interfaces, and most everything serialises and deserialises without further intervention. You can further customise the serialisation if required. The only problem I've ever had is serialising an inner class without intentionally serialising the containing outer class (this is due to the implicit this reference)