(Question by Timo Salmi) Timo's comments to reply added with --

I am just trying to find out the possibilities to store arbitrary java object to a relational database. The need is quite simple, just put the object to the DB2 (sic!) database and view it using some other tool. Probably the customer wants to search for some value on a field in the stored object list later on.

I have browsed some alternatives to achieve this. A simple approach could be just serialize the object and put the serialized string into a varchar field. Searching and debugging could be difficult because the string is in binary format. JanneJalkanen: Note that you probably need to BASE64-encode the object stream first before putting it in the DB. Also, you will need to be careful about buffer overruns. RAW datatype might be more useful if the DB supports it.


Another alternative is to store the data in XML format. This is a lot more complicated alternative because there are no ready tools available to parse arbitrary Java class. Or is there?

The answer, and more discussion can be found in SerializingJavaToXML.


Another option is to make my own parser for classes. A lot of work. JanneJalkanen: Not really. If you want to store any arbitrary class, then it is a lot of work, but if you only need to store a single class of your own and/or its subclasses, then writing a serialisation system is not very difficult. You'll just need a very robust set of test cases to make sure you don't do any mistakes anywhere. The obvious idea would be to look at Enterprise JavaBeans, which have been designed to allow easy database persistence. If you want to be absolutely generic, then you're in trouble, though.

-- I suppose EJB beans is not an option here because the data comes in serialized form (read asynchronously from Java Messaging Service queue), not directly from any application. Still I have to think about that.

-- Actually Java Data Objects is exactly what I need. Unfortunately JDO is still under construction. Or is it?


You should try this O/R-mapper: Hibernate.


Whichever solution you decide on, be sure to take possible changes to the objects being stored into account. If you do not you will almost certainly loose data whenever your class definition changes.

Add new attachment

Only authorized users are allowed to upload new attachments.
« This page (revision-11) was last changed on 29-Mar-2004 15:41 by JeroenWenting