Quick'n'dirty persistence [closed]

若如初见. 提交于 2019-12-02 18:18:22
GHad

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

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.

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

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)

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

  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

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.

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/

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);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!