views:

683

answers:

10

What are the lightweight options one has to persist Java objects ?

I'm looking for the easiest possible solution. I don't want anything fancy featurewise just something simple that works with reasonnably simple objects (some collections, relationships and simple inheritance) and doesn't bring too much apparent complexity (if any) to the existing codebase.

The options I'm aware of include Hibernate and frameworks such as EMF but they sound (and have been experienced to be) too complex and time-consuming. I'd like something out of the box, preferably file oriented than dababase oriented, that I can just put on top of my plain old java objects.

This is a newbie question, thanks in advance for any tutorial-like, context clarifying guidance.

+4  A: 

The ObjectOutputStream and ObjectInputStream can do this. It's built into Java and allows you to serialize/deserialize objects quite easily.

The bad thing about these is that if you change your objects you can't import existing old objects. You'll have to think of a way to stay compatible with existing objects that might already be saved on a user's computer, such as adding a version number to your classes and creating the ability to convert old objects to new ones.

More info here:

EDIT: You may also consider adding the following attribute to all your classes right off the bat. This may allow you to then add attributes to classes and still be able to load old Object files. In RMI-land this works, but I'm not sure about files. Don't ever remove attributes though.

static final long serialVersionUID = 1L;
sjbotha
+4  A: 

db4o is an object database so there's no schema setup and it adapts well to changes in object structure over time.

Allain Lalonde
I really like db4o, so nice and simple.
Fabian Steeg
+2  A: 

One simple approach is to serialize (as in Serializable) your objects to disk.

http://www.devx.com/Java/Article/9931/1954

Willie Wheeler
The only problem with serialization is that if you change your objcet at all it falls flat on its face.
Milhous
A: 

I have to say the initial learning curve for Hibernate is relatively shallow. You should be able to get a test system up and running in less than a day. It's only when you want the more advanced features where it starts to get steeper. I would recommend you definitely take a look, I mean what's a day if you end up not choosing it?

Having said that, have you considered just serializing your objects directly to disk if you just want something quick n dirty?

MrWiggles
Lots of things Hibernate is, but lightweight is not one of them. And as far as out of the box persistence goes, you need to create a database schema before even starting.
Allain Lalonde
Except Hibernate does this for you so you don't even need to think about it. If you want it to be, Hibernate *can* be lightweight
MrWiggles
+3  A: 

See this similar question Light-weight alternative to hibernate. The most light weight framework I know of is iBatis

kgiannakakis
A: 

Hibernate with annotations is not that hard.......

l_39217_l
+2  A: 

Try Simple XML persistence http://simple.sourceforge.net. Its really simple and requires no configuration. Just take your existing POJOs and annotate them, then you can read and write them to a file. Cyclical object graphs are supported as well as all Java collections. It can be used like so.

Serializer serializer = new Persister();
MyObject object = serializer.read(MyObject.class, new File("file.xml));

and writing is just as easy

serializer.write(myInstance, new File("file.xml"));

This is an extremely lightweight approach, no dependancies other than standard Java 1.5. Compared with other object to XML technologies such as JAXB, XStream, Castor, which are dependant on a whole host of other projects Simple XML is very lightweight and memory efficient.

ng
+3  A: 

If you are looking at something simple you might also want the data in a format you can read (not a binary file or database). If that is the case, you should look at JAXB (Java's XML Binding) that is part of Java 6 and later. There are other technologies that may be able to do this better such as XML Beans but this one is built in.

Take a look at this page from Java's API. It is pretty straight forward to serializing and deserializing Java objects.

http://java.sun.com/javase/6/docs/api/javax/xml/bind/JAXBContext.html

Basically you use the following:

    JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );

    // unmarshal from foo.xml
    Unmarshaller u = jc.createUnmarshaller();
    FooObject fooObj = (FooObject)u.unmarshal( new File( "foo.xml" ) );

    // marshal to System.out
    Marshaller m = jc.createMarshaller();
    m.marshal( fooObj, System.out );

Just make sure your FooObject has the @XmlRootElement annotation. Java bean properties are automatically interpreted but you can use other annotations to control how the XML looks and model more complex objects.

Chris Dail
Pretty slick. I didn't know about this.
Allain Lalonde
Have you looked at Xstream. It is a little simpler.
Milhous
A: 

The simplest option I know of is using Xstream to make an xml file out of ANY java obect.

Milhous
+1  A: 

You could try XStream which is an open source library. All it does is turn your POJOs into XML which you can then saved to disk. It is very easy to use and requires only a few lines of code to use.

willcodejavaforfood