What is the easiest way to save Java objects?

I now have a Java program whose classes are currently POJOs and are stored in volatile memory. They must be saved. As I understand it, there are two popular options: JDO and Java Persistence API. For someone who knows little about SQL, Torque, etc., What is the easiest way to add persistence to my program data?

+7
java jdo persistence apache-torque
source share
6 answers

The traditional way to serialize a file system is to use Java Serialization . However, you need to implement Serializable everywhere.

An easier solution is to serialize to XML (and then dump it to the file system) using XStream . You do not need to implement any interfaces, and most of everything is serialized and deserialized without further intervention. If necessary, you can configure serialization. The only problem I've ever encountered is serializing an inner class without intentionally serializing the containing outer class (this is due to the implicit this reference)

+7
source share

Serialize objects in the file system if you do not know SQL or relational databases.

You need to learn JDBC to use JDO, JPA, Hibernate or something else. If your POJOs are terribly complicated, I would recommend starting there and working on you.

Make sure you learn about normalizing and developing indexes properly.

+6
source share

The easiest way I've come across is db4o :

 ObjectContainer db = Db4o.openFile(location); db.store(myObject); List<MyObject> myObjects = db.query(MyObject.class); 

In addition, there are interesting ways to query in other ways.

+5
source share

If serialization is an option, consider using a prevalence API such as prevalayer or Space4J (later). About the prevalence of the object:

Prevalence is a concept started by Klaus Westefeld on how to store data in a real object-oriented manner using only snapshots, transaction logs, and serialization.

Check out this article to learn more about this topic (more about Google).

+5
source share

It looks like you might want to save the database. However, to avoid the complexity of the database, one simple solution for storing POJOs in the file system is to serialize them in an XML document. The Java 1.6 API includes the JAXB framework found in the javax.xml.bind package. To use JAXB, you essentially annotate your POJO and create marshals and non-marshal methods, for example:

 @XmlRootElement(name="Foo") public class Foo { @XmlElement(name="Bar") public int mBar; public static void marshal(Foo foo, OutputStream out) IOException { try { JAXBContext jc = JAXBContext.newInstance(Foo.class); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(qaConfig, out); } catch (JAXBException ex) { throw new IOException(ex); } finally { out.close(); } } public static Foo unmarshal(InputStream in) throws IOException { try { JAXBContext jc = JAXBContext.newInstance(Foo.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); return (Foo)unmarshaller.unmarshal(in); } catch (JAXBException ex) { throw new IOException(ex); } finally { in.close(); } } } 

Suppose you save an instance of Foo where mBar is 42, then this solution will create an XML file, for example:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Foo> <Bar>42</Bar> </Foo> 
+2
source share

DataNucleus is the easiest way because it provides the JDO and JPA APIs for storing almost any type of data warehouse you will ever need. Why write all this JAXB code in one of the other answers when DataNucleus does it for you? Everything is backed by Java standards.

+2
source share

All Articles