views:

473

answers:

8

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.

+3  A: 

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.

Mike Reedell
Based on the update to the question I'd have to say go with XStream.
Mike Reedell
+7  A: 

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

Greetz, GHad

GHad
Yes. XStream is very simple to use.
Nat
Interesting- thanks
Mike Sickler
+1 interesting indeed
KLE
+3  A: 

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();
}
Adam Paynter
A: 

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.

PeterMmm
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
A: 

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

Chris Dail
A: 

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/
Reginaldo
+1  A: 

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)

StaxMan
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
+1  A: 

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