What's the easiest way to persist java objects?

五迷三道 提交于 2019-12-17 18:47:11

问题


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?


回答1:


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)




回答2:


Serialize the objects to the file system if you don't know SQL or relational databases.

You'll have to learn JDBC to use JDO, JPA, Hibernate, or anything else. Unless your POJOs are terribly complex I'd recommend starting there and working your way up.

Make sure you learn about normalization and proper design of indexes.




回答3:


The easiest way I came across as yet is db4o:

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

Plus there are really nice ways to query in other ways.




回答4:


If serialization is an option, consider using a prevalence API like prevalayer or Space4J (more recent). About object prevalence:

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

Check this article to learn more on this topic (more on Google).




回答5:


Sounds like you may want to persist to a DB. However, to avoid the complexities of a DB one simple solution for persisting POJOs to the file system is to serialize them to an XML document. The Java 1.6 API includes the JAXB framework found in the javax.xml.bind package. To use JAXB you essentially annotation your POJO and create marshal and unmarshal methods like so:

@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();
      }
   }
}

Lets say you persist an instance of Foo where mBar is 42 then this solution would produce an XML file like so:

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



回答6:


DataNucleus is the easiest way since it provides JDO and JPA APIs for persistence to pretty much any type of datastore you would ever want. Why write all of that JAXB code in one of the other replies when DataNucleus does it for you ? All backed by Java standards



来源:https://stackoverflow.com/questions/1955396/whats-the-easiest-way-to-persist-java-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!