Hibernate Auto-generate .hbm and .java

by Yuval Ararat on 15/03/2008

This is one of the best descriptions of how to use the Hibernate tools to reverse engineer a DB into ,hbm and .java files automatically.

I will post the description here if it will ever disappear.

  1. Download the hibernate-tools jar. [i]
  2. Extract the jar to plugins folder of Eclipse.
  3. Start eclipse.
  4. Click on [File -> New -> Other -> Hibernate -> Hibernate Configuration File] and create a cfg file. The following properties should be specified : jdbc url , username, password, DB schema, driver class and dialect.
  5. Click on [File -> New -> Other -> Hibernate -> Hibernate Console Configuration ] and create a new console configuration. Add the jar file that contains your DB driver in the classpath section at the bottom.
  6. Enter the name of the console configuration. Click Browse button against the Configuration file and select the cfg.xml file created in step 4.
  7. Click on [File -> New -> Other -> Hibernate -> Hibernate Reverse Engineering File(reveng.xml) ] and select the location of the file.
  8. Select the cfg.xml file created in step 4 as the Console Configuration. Click on include button and specift the schema and table name(s) to reverse engineer. Multiple table names can be specifed using sql wild char (eg: TBL_%). Click Finish.
  9. Click on the Hibernate icon in the tool bar and select the [Hibernate Code Generation ..] option.
  10. Right click on the [Hibernate Code Generation]tree node and select New.
  11. Give the name of the configuration and select the console configuartion created in setp 5 and reveng.xml created in step6-7. Also select the Output Directory,
  12. Go to the Exporters tab and check Generate domain code(.java) and Generate mappings (hbm.xml). Click run.
  13. The hibernate mapping xml files and Java classes will be created in the output directory specified in step 11.

UPDATE: [i] Some of the new versions of eclipse come with the tools pre installed, if your version has the tools preinstalled jump to step 4

{ 9 comments… read them below or add one }

Ram April 21, 2011 at 1:35 am

Yes great, this helped me generate code hbm.xml and *.java file for the new tables added to the already existing schema. Used Eclipse Helios as editor.

Laxman June 5, 2011 at 1:43 pm

Hi . I am using Eclipse Galileo, MySql 5.0, Here i have done all the steps that you mentioned, i have created hib config file, console config fiile, everything is fine even i am able to connect to database and see the tables, But the problem is it’s not generating either hbm file nor java files. i tried many times but no result.

Where it messing up, i have given Output directry …project/src

Please help me out.

Yuval Ararat June 5, 2011 at 11:38 pm

Did you start from step 4?

Laxman June 6, 2011 at 4:30 pm

Yes, i did step 4 to step 12 , but failed to generate files. please suggest

Yuval Ararat June 8, 2011 at 11:54 am

do you have any error messages in the calipso log? any errors anywhere?

pinoteres June 25, 2011 at 3:12 am

Laxman, in step 12 after you check XML Mapping, Domain code, DAO, or anything you HAVE TO add Output directory property for each of them (by Add button below, in Properties section).
I don’t know why there is Output directory field in Main pane, because nothing is generated there :?

If you still have some problems try to fallow this guide http://iablog.sybase.com/paulley/2009/02/reverse-engineering-sql-anywhere-with-hibernate-tools-part-trois

Rui June 23, 2012 at 12:39 am

Thanks !

Shailesh Padave June 20, 2013 at 8:03 am

Hi,
I want the functionality which will generate the XML file and the tags for the XML file would be the variables from Java file depending on the Annotations.

e.g

public class test{
public static void main(){
String test,test1;
}
XML File
aaa
bbb

Can we do that using Hibernate in-built functions or any API.. Need urgent help on this Topic..

Sachin July 24, 2013 at 4:07 pm

I generated the Domain Code(Models) and Mapping files. But the problem is,
they are not following the Java syntax. Eg: my database table name is : usergroups and modelname generated is Usergroups. But it should be UserGroups(acording to Java syntax, evry word first letter capital.)
I am using Mysql database.
If you help me in solving this problem, that would be great.
Thank you !

Leave a Comment

Previous post:

Next post: