Take the 2-minute tour ×

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.

share|improve this question

closed as off-topic by Raedwald, rgettman, Stefano Sanfilippo, 一二三, Jarrod Roberson Oct 4 '13 at 2:26


  • "Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – Raedwald, rgettman, Stefano Sanfilippo, 一二三, Jarrod Roberson

+1 for question I might need soon –  KLE Sep 2 '09 at 12:24

9 Answers 9

up vote 9 down vote accepted

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

share|improve this answer
Yes. XStream is very simple to use. –  Nat Jun 10 '09 at 15:16
Interesting- thanks –  Mike Sickler Jul 30 '09 at 2:31
+1 interesting indeed –  KLE Sep 2 '09 at 12:25
JAXB does not even need a jar. Its shipped with the Jvm. –  Andrejs Mar 20 '12 at 15:58

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.

share|improve this answer
Based on the update to the question I'd have to say go with XStream. –  Mike Reedell Jun 10 '09 at 15:29

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 {
} finally {
share|improve this answer

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)

share|improve this answer
Forgot to mention: Jackson requires no configuration whatsoever in most cases. You can use annotations if you want, configure etc; but for the most part it's zero-config similar to how XStream works. –  StaxMan Jun 11 '09 at 18:27

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

share|improve this answer
  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

share|improve this answer

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.

share|improve this answer
Serialisation is not a hack. My way of working with it is. So I'm looking for how others handle this kind of situation, draw from their experience. –  Robert Munteanu Jun 10 '09 at 15:39
When talking about serialization, check JBoss Serialization which has several technical advantages over JDK's impl. –  Ondra Žižka Jul 21 '11 at 1:34

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/
share|improve this answer

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...

share|improve this answer
