<p>I have a large tree of Java Objects in my Desktop Application and am trying to decide on the best way of persisting them as a file to the file system.</p>

<p>Some thoughts I've had were:</p>

<ul>
<li><p><strong>Roll my own serializer using DataOutputStream</strong>:  This would give me the greatest control of what was in the file, but at the cost of micromanaging it.</p></li>
<li><p><strong>Straight old Serialization using ObjectOutputStream and its various related classes</strong>: I'm not sold on it though since I find the data brittle.  Changing any object's structure breaks the serialized instances of it.  So I'm locked in to what seems to be a horrible versioning nightmare.</p></li>
<li><p><strong>XML Serialization</strong>: It's not as brittle, but it's significantly slower that straight out serialization.  It can be transformed outside of my program.  </p></li>
<li><p><strong>JavaDB</strong>: I'd considered this since I'm comfortable writing JDBC applications. The difference here is that the database instance would only persist while the file was being opened or saved.  It's not pretty but... it does lend itself to migrating to a central server architecture if the need arises later and it introduces the possibility of quering the datamodel in a simpler way.</p></li>
</ul>

<p>I'm curious to see what other people think. And I'm hoping that I've missed some obvious, and simpler approach than the ones above.</p>

<hr>

<p>Here are some more options culled from the answers below:</p>

<ul>
<li><strong>An Object Database</strong> - Has significantly less infrastructure than ORM approaches and performs faster than an XML approach. thanks aku</li>
</ul>

[{'answer_id': 37274, 'author': 'Paul Tomblin', 'author_id': 3333, 'author_profile': 'https://Stackoverflow.com/users/3333', 'pm_score': 2, 'selected': False, 'text': '<p>Have a look at Hibernate as a simpler way to interface to a database.</p>\n'}, {'answer_id': 37278, 'author': 'Tim Williscroft', 'author_id': 2789, 'author_profile': 'https://Stackoverflow.com/users/2789', 'pm_score': 1, 'selected': False, 'text': '<p>XStream from codehaus.org</p>\n\n<p>XML serialization/deserialization largely without coding.\nYou can use annotations to tweak it.\nWorking well in two projects where I work.</p>\n\n<p>See my users group presentation at <a href="http://cjugaustralia.org/?p=61" rel="nofollow noreferrer">http://cjugaustralia.org/?p=61</a></p>\n'}, {'answer_id': 37279, 'author': 'Brian Matthews', 'author_id': 1969, 'author_profile': 'https://Stackoverflow.com/users/1969', 'pm_score': 3, 'selected': False, 'text': '<p>I would go for the your final option JavaDB (Sun\'s distribution of <a href="http://db.apache.org/derby" rel="nofollow noreferrer">Derby</a>) and use an object relational layer like <a href="http://hibernate.org" rel="nofollow noreferrer">Hibernate</a> or <a href="http://ibatis.apache.org" rel="nofollow noreferrer">iBatis</a>. Using the first three aproaches means you are going to spend more time building a database engine than developing application features.</p>\n'}, {'answer_id': 37300, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 3, 'selected': True, 'text': '<p><a href="http://www.db4o.com" rel="nofollow noreferrer">db4objects</a> might be the best choice</p>\n'}, {'answer_id': 37355, 'author': 'Jay Stramel', 'author_id': 3547, 'author_profile': 'https://Stackoverflow.com/users/3547', 'pm_score': 2, 'selected': False, 'text': '<p>In my experience, you\'re probably better off using an embedded database.  SQL, while less than perfect, is usually much easier than designing a file format that performs well and is reliable.</p>\n\n<p>I haven\'t used JavaDB, but I\'ve had good luck with <a href="http://www.h2database.com" rel="nofollow noreferrer">H2</a> and <a href="http://www.sqlite.org/" rel="nofollow noreferrer">SQLite</a>.  SQLite is a C library which means a little more work in terms of deployment.  However, it has the benefit of storing the entire database in a single, cross-platform library.  Basically, it is a pre-packaged, generic file format.  SQLite has been so useful that I\'ve even started using it instead of text files in scripts.</p>\n\n<p>Be careful using Hibernate if you\'re working with a small persistence problem.  It adds a lot of complexity and library overhead.  Hibernate is really nice if you\'re working with a large number of tables, but it will probably be cumbersome if you only need a few tables.</p>\n'}, {'answer_id': 39383, 'author': 'Marcio Aguiar', 'author_id': 4213, 'author_profile': 'https://Stackoverflow.com/users/4213', 'pm_score': 0, 'selected': False, 'text': "<p>I think it depends on what you need. Let's see the options:</p>\n\n<p>1) Descarded imediatelly! I'll not even justify. :)</p>\n\n<p>2) If you need a simple, quick, one-method persistence, stick with it. It will persist the complete data graph as it is! Beware of how long you'll be maintaning the persisted objects. As yourself pointed out, versioning can be a problem.</p>\n\n<p>3) Slower than (2), need extra code and can be edited by the user. I would only use it the data is supposed to be used by a client in another language.</p>\n\n<p>4) If you need to query your data in anyway, stick with the DB solution.</p>\n\n<p>Well, I think you had already answered your question :)</p>\n"}]
['https://Stackoverflow.com/questions/37271', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2443/']