1. Get the Hibernate Tools Eclipse plugin
(i) Available here: http://www.hibernate.org/subprojects/tools.html
2. Create a Java Project.
3. Right-Click on src dir, and create following files (use new> Hibernate > etc…), under the src dir:
(i) Hibernate Configuration File: hibernate.cfg.xml
(ii) Hibernate Properties: hibernate.properties
(iii) Hibernate Reverse Engineering File: hibernate.reveng.xml
4. Create Hibernate Console Configuration:
Menu: View > Hibernate Configurations > Add/New Configuration
5. Right-Click on your Project > select Properties. Add hibernate3.jar, mysql-connector*.jar, commons-logging*.jar to classpath.
6. Menu > Run > Hibernate Code Generation > Hibernate Code Generation Configurations > Create a New Config here (give it a name)
(i) Choose appropriate options.
(ii) On the Exporters Tab if you select ‘Generate EJB3 annotations’ then you domain java classes will have annotations like:
@Table(name = “city”, catalog = “test1”)
AND your hibernate.cfg.xml file will mappings pointing to domain classes.
<mapping class="com.some.abc.City" />
IF On the Exporters Tab if you DO NOT select ‘Generate EJB3 annotations’ then you domain java classes will NOT have annotations.
AND your hibernate.cfg.xml file will mappings pointing to hbm.xml resources like this:
<mapping resource="com/some/abc/City.hbm.xml" />
IF you select use Java 5 syntax – then Lists, Arrays will use java 5 features like generics for lists arrays etc.
(iii) Apply and Run.
In all the above, provide information that you have like, Database Type, Driver etc; Remember you can always revisit and correct later too.
1. In Hibernate Code Gen Config:Under Exporter tab: Select what you want to generate. Choosing – Generic Exporter, HQL Query Execution Exporter options gives errors for me, so i leave that deselected.
2. If reverse Engineering from MySQL you get errors like: root@localhost does not have permission, then you should give permissions to the root(or whatever user) like this:
grant all privileges on *.* to root@localhost;
3. On MySQL, if you want Hibernate Reverse Engineering Tool to understand relation between tables and generate code that reflects this relationships – you must use InnoDB and foreign keys should exist. The Hibernate Code Gen Config screen, has checkbox options (on first tab itself) related to this.
4. IMPORTANT: If you make changes to the generated code, and try reverse engineering again – it appears that the tool ovewrites the code with newly generated code!!
Hibernate Tools Reference Guide – Reverse Engineering