Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the lightweight options one has to persist Java objects [closed]

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.

like image 409
Yann Semet Avatar asked Jan 16 '09 15:01

Yann Semet


People also ask

What is a lightweight persistent domain object?

lightweight means that it's available outside of the JEE (JPA) container. means you can use it with any J2se application (tomcat, spring or standalone java app, etc...). It's because underlying implementation is often provided by a standalone ORM framework like hibernate.

How do you persist an object in Java?

Serialization is the process of saving an object's state to a persistence store and also rebuilding the object from the saved information when needed in the future. With Serialization, you can serialize (persist) any object that you don't need or that may have been used in some other application or may be used later.

What is a persistence in Java?

Data Persistence is a means for an application to persist and retrieve information from a non-volatile storage system. Persistence is vital to enterprise applications because of the required access to relational databases.


2 Answers

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

like image 66
Allain Lalonde Avatar answered Oct 19 '22 18:10

Allain Lalonde


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.

like image 28
Chris Dail Avatar answered Oct 19 '22 17:10

Chris Dail