<p>I am currently using Hibernate Tools 3.1; I customized naming convention and DAO templates. The database (SQL Server 2005) in early development phase and I'm in charge of rebuilding the mappings, entities, DAOs, configuration, whatever. Each time I have to reverse-engineer the tables and so I lose every customization I made on the mappings (*.hbm.xml files) like adjusting the <strong>identity</strong> columns, picking the fields used in <strong>equals</strong> and <strong>toString</strong>. I was considering to write the diff XML in a file and the "merge" that onto the generated mapping (see <a href="https://stackoverflow.com/questions/80609/merge-xml-documents">my related question</a>) but I was wondering... is there any best practice/tool for dealing with these annoying, unavoidable, critical tasks?</p>

[{'answer_id': 80763, 'author': 'Aaron Jensen', 'author_id': 11229, 'author_profile': 'https://Stackoverflow.com/users/11229', 'pm_score': 3, 'selected': True, 'text': "<p>I'd strongly recommend against continual reverse engineering. Reverse engineering is a great one time thing, but changes need to be managed as changes to both the hbm and the database.</p>\n\n<p>We use migrations to manage db changes, and we include the associated changes in the hbm. If Hibernate has it (I believe it does) you may want to look into annotations instead of an hbm, they can be quite a bit easier to maintain.</p>\n"}, {'answer_id': 5388745, 'author': 'Shane', 'author_id': 193435, 'author_profile': 'https://Stackoverflow.com/users/193435', 'pm_score': 1, 'selected': False, 'text': "<p>This is two and a half years late, but I'll offer a dissenting opinion. You should be able to make any customizations you need to the mapping files through the hibernate.reveng.xml file or a custom ReverseEngineeringStrategy. For the classes themselves, you should always generate to base classes and extend them with classes containing custom code.</p>\n\n<p>For example, generate com.company.vo.generated.CustomerGenerated and extend it with com.company.vo.custom.Customer. Code generation should overwrite all classes in the generated package but never in the custom package (although you can have Hibernate Tools generate these custom classes in the target directory so that you can copy and paste blanks into the custom directory as needed). This way you can override methods for equals, toString, etc in the custom classes and not lose your changes when you regenerate. Also note that the best practice is to not check in generated code into SCM.</p>\n\n<p>There are some great examples on this site of how to achieve this using Maven, the Hibernate3 plugin, and the build helper plugin. Most of these have very helpful answers by Pascal Thivent. This method is working beautifully for me, and while there is a bit of a learning curve it's a wonderful thing to be able to propagate database changes to the app with a single Maven command.</p>\n"}]
['https://Stackoverflow.com/questions/80697', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4690/']