Alex Rivera | Logout

Quick'n'dirty persistence

Asked 2009-06-10T15:08:00.223
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.

Edit
Report

3 Answers

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

answered 2009-06-10T16:17:43.810
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.

answered 2009-06-10T15:19:52.383
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);
answered 2009-07-30T01:53:44.607

Your Answer