Quick'n'dirty persistence [closed]

你说的曾经没有我的故事 提交于 2019-12-03 05:04:24

问题


I often find myself needing a quick ( in terms of code ), lightweight ( in term of runtime, dependencies) persistence solution for simply a bunch of objects, mainly between application restarts.

Usually I resort to some Java serialisation hack, but I wonder if there's something better out there.

Have you used something similar?


To make it clear, a JPA-based solution is not lightweight in my book, and a JDBC-based one is not quick.


Update: I favour configuration-less frameworks over those which require configuration. For instance the Java serialisation solution requires a implements Serializable and it works. A JPA solution, either with annotations or with mapping files would be too heavyweight.

Update 2: Just to make it clear, I don't think Serialisation is a hack. It's actually a very powerful mechanism, just that I'm using it in a hackish way when doing persistence.


回答1:


I prefer XStream: Only one Jar needed, fast and very easy to use




回答2:


Try iBatis. I've used it recently on a project where I wanted a JDBC abstraction w/o ORM/spring/container/etc.

Easy to setup, only a couple of small jars, and the config is very flexible. It won't give you the change-the-db-at-a-moments-notice flexibility of hibernate, et. al. but it is fairly lightweight.




回答3:


Perhaps db4o would work for you. Don't let the name fool you, it can be embedded. According to its tutorial:

Object yourObject = ...;

String fileName = ...; // where you wish to store the objects
ObjectContainer container = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), fileName);
try {
    container.store(yourObject);
} finally {
    container.close();
}



回答4:


I would add Jackson (json) and JAXB (xml); in addition to Xstream that was already mentioned. Jackson works well with just one caveat: if you do have extensive polymorphic storage, deserialization can get tricky. That's where Xstream works better.

And I agree in that Java serialization is not good for persistence -- it is good for transferring serialized objects, but persistence has temporal dimension, and classes change over time; and that's where problems start (I'm sure you know that, but since others seem to be boggling as to why peristing-using-serialization is bad, thought I'll mention it). It's also much easier to eyeball things serialized using textual formats (json, xml) than binary ones. And finally, if you need space efficiency, compression (like gzip) works wonders, data size after compression tends to be identical, independent of format (assuming same amount of information)




回答5:


You should look at JAXB. It is part of Java since JRE 6. It is pretty easy to use and allows you to drive the XML schema from your Java object model. The best part is that you don't need any extra jar files or libraries as it is part of Java. Check out the javax.xml.bin package.

Info on the JAXB Project itself: https://jaxb.dev.java.net

Link to the JavaDocs in the JRE: http://java.sun.com/javase/6/docs/api/javax/xml/bind/package-frame.html




回答6:


  1. For object collections try JDBM (one jar). Gives you a map that is stored on disk.

    DB db = new DBMaker(fileName).build();

    Map<String,MyObject> map = db.createTreeMap("mapName")

    map.put("obj1", myObject1);

  2. For config JAXB is really simple and does not have dependencies. Stores in XML.

    JAXB.marshall(myObject, new File("config.xml") ); // saved. that's it




回答7:


You look for

persistence solution for simply a bunch of objects

So why Java build-in serialization is a hack ?

If you are lazy maybe serialise with JSON will help you.




回答8:


Prevayler sounds like a good option for you.

It's based on Java serialization and it's pretty damn fast.

More info at:

  • http://www.prevayler.org/
  • http://www.prevayler.org/wiki/



回答9:


Check out BeanKeeper - that's the quickest, dirtiest and easiest ORM/persistence I've ever seen. Certainly beats iBatis that's for sure.

Store myStore = new Store((DataSource) ctx.lookup("jdbc/myds"));

package com.acme.bookstore;
public class Book
{
  private String title;
  private Author author;

  ...setters, getters...
}

package com.acme.bookstore;
public class Author
{
   private String firstName;
   private String lastName;
   private Date birthDate;

   ...setters, getters...
 }

store.save(book);


来源:https://stackoverflow.com/questions/976203/quickndirty-persistence

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