17

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
  • +1 for question I might need soon
    – KLE
    Sep 2, 2009 at 12:24

9 Answers 9

10

I prefer XStream, a simple library to serialize objects to XML and back again. Only one Jar needed, fast and very easy to use.

2
  • Yes. XStream is very simple to use.
    – Nat
    Jun 10, 2009 at 15:16
  • JAXB does not even need a jar. Its shipped with the Jvm.
    – Andrejs
    Mar 20, 2012 at 15:58
5

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.

1
  • Based on the update to the question I'd have to say go with XStream. Jun 10, 2009 at 15:29
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();
}
3

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)

1
  • 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, 2009 at 18:27
2

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

2
  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

1

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.

2
  • 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. Jun 10, 2009 at 15:39
  • When talking about serialization, check JBoss Serialization which has several technical advantages over JDK's impl. Jul 21, 2011 at 1:34
1

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/
1

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);

Not the answer you're looking for? Browse other questions tagged or ask your own question.