<p>I'm looking for a general solution for upgrading database schema with ORM tools, like JPOX or Hibernate. How do you do it in your projects?</p>

<p>The first solution that comes to my mind is to create my own mechanism for upgrading databases, with SQL scripts doing all the work. But in this case I'll have to remember about creating new scripts every time the object mappings are updated. And I'll still have to deal with low-level SQL queries, instead of just defining mappings and allowing the ORM tools to do all the job...</p>

<p>So the question is how to do it properly. Maybe some tools allow for simplifying this task (for example, I heard that Rails have such mechanism built-in), if so please help me decide which ORM tool to choose for my next Java project.</p>

[{'answer_id': 64162, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 0, 'selected': False, 'text': '<p>I think your best bet is to use an ORM-tool that includes database migration like <a href="http://subsonicproject.com/" rel="nofollow noreferrer">SubSonic</a>:</p>\n\n<p><a href="http://subsonicproject.com/2-1-pakala/subsonic-using-migrations/" rel="nofollow noreferrer">http://subsonicproject.com/2-1-pakala/subsonic-using-migrations/</a></p>\n'}, {'answer_id': 64165, 'author': 'Jason Cohen', 'author_id': 4926, 'author_profile': 'https://Stackoverflow.com/users/4926', 'pm_score': 0, 'selected': False, 'text': "<p>We ended up making update scripts each time we changed the database.  So there's a script from version 10 to 11, from 11 to 12, etc.. Then we can run any consecutive set of scripts to skip from some existing version to the new version.  We stored the existing version in the database so we could detect this upon startup.</p>\n\n<p>Yes this involved database-specific code!  One of the main problems with Hibernate!</p>\n"}, {'answer_id': 64225, 'author': 'Adrian', 'author_id': 7426, 'author_profile': 'https://Stackoverflow.com/users/7426', 'pm_score': 0, 'selected': False, 'text': '<p>When working with Hibernate, I use an installer class that runs from the command-line and has options for creating database schema, inserting base data, and dynamically updating the database schema using <a href="http://www.hibernate.org/hib_docs/v3/api/org/hibernate/tool/hbm2ddl/SchemaUpdate.html" rel="nofollow noreferrer">SchemaUpdate</a>. I find it to be extremely useful. It also gives me a place to put one-off scripts that will be run when a new version is launched to, for example, populate a new field in an existing DB table.</p>\n'}, {'answer_id': 64312, 'author': 'MarcE', 'author_id': 7262, 'author_profile': 'https://Stackoverflow.com/users/7262', 'pm_score': 2, 'selected': False, 'text': '<p>I don\'t see why ORM generated schemas are any different to other DB schemas - the problem is the same. Assuming your ORM will spit out a generation script, you can use an external tool to do the diff</p>\n\n<p>I\'ve not tried it but google came back with <a href="http://www.red-gate.com/products/SQL_Compare/index.htm" rel="nofollow noreferrer">SQLCompare</a> as one option - I\'m sure there are others.</p>\n'}, {'answer_id': 64647, 'author': 'killdash10', 'author_id': 7621, 'author_profile': 'https://Stackoverflow.com/users/7621', 'pm_score': 4, 'selected': True, 'text': '<p><a href="http://www.liquibase.org" rel="noreferrer">LiquiBase</a> is an interesting open source library for handling database refactorings (upgrades).  I have not used it, but will definitely give it a try on my next project where I need to upgrade a db schema.</p>\n'}, {'answer_id': 162933, 'author': 'Paul Croarkin', 'author_id': 18995, 'author_profile': 'https://Stackoverflow.com/users/18995', 'pm_score': 2, 'selected': False, 'text': '<p>We hand code SQL update scripts and we tear down the schema and rebuild it applying the update scripts as part of our continuous build process. If any hibernate mappings do not match the schema, the build will fail.</p>\n'}, {'answer_id': 5132384, 'author': 'Faisal Feroz', 'author_id': 62848, 'author_profile': 'https://Stackoverflow.com/users/62848', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://www.dbmaintain.org" rel="nofollow">DbMaintain</a> can also help here.</p>\n'}, {'answer_id': 28055590, 'author': 'gaboroncancio', 'author_id': 2658613, 'author_profile': 'https://Stackoverflow.com/users/2658613', 'pm_score': 2, 'selected': False, 'text': '<p>You can check this <a href="http://flywaydb.org/#features" rel="nofollow">feature comparison</a> of some database schema upgrade tools.</p>\n\n<p>A comparison of the number of questions in SOW of some of those tools:</p>\n\n<ul>\n<li><a href="https://code.google.com/p/mybatis/wiki/Migration" rel="nofollow">mybatis</a> (1049 questions tagged)</li>\n<li><a href="http://www.liquibase.org/" rel="nofollow">Liquibase</a> (663 questions tagged)</li>\n<li><a href="http://flywaydb.org/" rel="nofollow">Flyway</a> (400 questions tagged)</li>\n<li><a href="http://dbdeploy.com/" rel="nofollow">DBDeploy</a> (24 questions tagged).</li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/64148', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6856/']