How to generate Hibernate mapping files & annotation with Hibernate Tools

In this article, we show you how to use Hibernate / JBoss Tools to generate Hibernate mapping files (hbm) and annotation code from database automatically.

Tools in this article

  1. Eclipse v3.6 (Helios)
  2. JBoss / Hibernate Tools v3.2
  3. Oracle 11g
  4. JDK 1.6

1. Hibernate Perspective

Open your “Hibernate Perspective“. In Eclipse IDE, select “Windows” >> “Open Perspective” >> “Others…” , choose “Hibernate“.

2. New Hibernate Configuration

In Hibernate Perspective, right click and select “Add Configuration…

In “Edit Configuration” dialog box,

  1. In “Project” box, click on the “Browse..” button to select your project.
  2. In “Database Connection” box, click “New..” button to create your database settings.
  3. In “Configuration File” box, click “Setup” button to create a new or use existing “Hibernate configuration file”, hibernate.cfg.xml.
Eclipse Hibernate Tools

See your list of your tables in “Hibernate Perspective“.

Eclipse Hibernate Tools

Sample of “hibernate.cfg.xml“, connect to Oracle 11g database.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
  <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  <property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:MKYONG</property>
  <property name="hibernate.connection.username">mkyong</property>
  <property name="hibernate.connection.password">password</property>
  <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
  <property name="hibernate.default_schema">MKYONG</property>
 </session-factory>
</hibernate-configuration>

3. Hibernate Code Generation

Now, you are ready to generate the Hibernate mapping files and annotation codes.

– In “Hibernate Perspective”, click “Hibernate code generation” icon (see below figure) and select “Hibernate Code Generation Configuration”

Hibernate Code Generation

– Create a new configuration, select your “console configuration” (configured in step 2), puts your “Output directory” and checked option “Reverse engineer from JDBC Connection“.

Hibernate Code Generation

– In “Exporter” tab, select what you want to generate, Model , mapping file (hbm) , DAO, annotation code and etc.

Hibernate Code Generation

See result

Hibernate Code Generation
Note
The generated Hibernate mapping file and annotations code are very clean, standard and easy to modify. Try explore more features.
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: water ionizers()

  • Pingback: free movie downloads()

  • Pingback: watch free movies online()

  • Pingback: watch free movies online()

  • Pingback: Blue Coaster33()

  • Pingback: Deb's Desk – Maven 3 + Hibernate 3.6 + Oracle 11g Example (XML Mapping)()

  • Alok

    how can we change target JDK for hibernate tool manually (without using hibernate perspective window)?

  • Mahantesh

    when i tried to generate annotations using reverse engineering from JDBC connection,It just keep saying “Fetching children from Database” till 57%,.it doesn’t progress further.
    Can you figure out what is going wrong ?I did all the steps you mentioned but at generation part it is struck!

  • http://maqas.net Hasan

    Hi,
    I have a database in mssql. One of this tables is “dbo.city” and the other one is “dbo._city”. I try to generate classes but i have a duplication problem. I think it’s about name strategy.
    “dbo.city” and “dbo._city” generated with same class name.
    Can you help me?
    Thanks

  • Yusuf

    how to configure db setting? i want to use postgresql, how i can add postgresql database driver?

  • http://www.drakonia.com.mx DrakoFire

    Hi mkyong, nice tutorial! good job, I have a question, if I have my tables with the prefix “namewebapp_” in my database, but the Hibernate Tools generates NamewebappAnybean (obviously), for example: I have a table with name “namewebapp_customers”, the Hibernate Tools generates my JavaBeans with the NamewebappCustomers class name , but I want the name for my JavaBean be only Customers, Is there any configuration to do this?

    Regards!

  • http://mkyong.com shivarul

    Exception in thread “main” java.lang.Error: Unresolved compilation problem:
    The method getCurrentSession() is undefined for the type SessionFactory

    at com.person.testperson.main(testperson.java:18)

    getting this error wht to do tel me :)

    regards
    shivarul

  • http://searchingwhat.com mintu

    how i can set perspective for hibernate
    because that not showing hibernate code generation configuration in my eclipse
    thanks

  • vj

    i followed the steps you said, but while trying to generate mapping files and pojo classses it is throwing an exception org.hibernate.cfg.jdbcbinder Exception and it says Duplicate class name ‘Syscatalog’ generated for ‘org.hibernate.mapping.Table(SYS.SYSCATALOG_)’. Same name where generated for org.hibernate.mapping.Table(SYS.SYSCATALOG_)

    • http://[email protected] Akin

      did you get a fix?

    • Freddie

      I have the same problem, any idea?

  • Radek

    Hi,

    I am following this tutorial but the Hibernate Configuration does not open the table list in the Database item and no error is appear in console. I have tried connect to database with Database Development persitance in Eclipse and it works with my configuration and I have got the list of tables. I am using postgresql driver and create simple Java Project. Do have some advice?

  • kaanchanha

    very much explanatory . thanks a lot !!!

  • mohsin azeem

    hey mkyong i like your web so much i also like your way of solving a problem… greate thumbs up!!
    my Question is that.
    1. if i have added new table to DB how can i create pojo class for single table ? by hibernate tool
    2. if i change any column’s property or add new column to table how can i change it in POJO class automatically
    THANKS :)

    • http://www.mkyong.com mkyong

      Thanks for the kind words.

      1. Uses Hibernate tool.
      2. If small change like add a new column, just add the annotation manually :).

  • Jeremy

    The “hibernate.default_schema” value HAS to be all uppercase or else you will never be able to connect to the database and there will be no errors as to why (Even though the ‘Test Connection’ is successful). Took me a long time to find that out, so I’m posting it here in case someone needs the solution.

    • Anderson

      Thanks, before your tip I couldn’t see the table list.

    • Khanh

      Thank you very much:))

    • Nicks

      Thanks for this comment, even i spent a lot of time for this

  • Pingback: Maven 3 + Hibernate 3.6 + Oracle 11g Example (XML Mapping)()

  • mathaker

    Sir,

    I am learning hibernate. I have tried your tutorial in both Eclipse and NB.
    I just have two question.
    Q-1. As you said NB is slow, can you tell us how?
    Q-2. If I add one more column in DBUser table, will it reflect automatically in hbm file and pojo? If not then I have to change mannually.

    • http://www.mkyong.com mkyong

      A1 = Use Eclipse
      A2 = You need to generate again. If just added a new column, you can modify the pojo manually.

  • Prasad Isuru

    Thank You sir,Your all posts very use full …

  • srinivas pamula

    Hi sir, i am watching ur wesite,
    which is helping me a lot.
    thank u sir.

  • Ela Cazacu

    Hello,
    I am trying to run the mapping tool using a hibernate.reveng.xml file.
    The problem is that nothing gets generated and I get no error messages either.
    Do you know where I can find information (logs) about what went wrong?
    Thank you!

    • http://[email protected] Prem

      Am having the same issue. I ran the Hibernate Code Generation configurations to generate the POJO classes and the corresponding mapping files. But nothing happens. No error messages.

  • Hanifa

    Sir,
    I am having a common Database and I need to create annotations for only specific tables. How can I acheive this?

  • Hanifa

    SIr,

  • http://none ChrisX

    Hello!

    This tutorial are great, but could you do a tutorial in Netbeans?
    IMHO Eclipse is crap, more configurations than fun with pure programming.
    What do you think about?

    • http://www.mkyong.com mkyong

      Eclipse is fast, flexible and lot plugins, it’s still my favor IDE.

      IMHO, Netbeans is also flexible, features rich, but it’s slow and UI is ugly, after so many years and versions, NB is still slow.

  • Arpit

    Sir ,

    i got 2 kind of error after updating plugin. Actually tomcat server is giving me two error .below mentioned.
    ———————————————————-

    Error 1 : An internal error occurred during: “Starting Tomcat v6.0 Server at localhost”.

    java.lang.NoSuchMethodError: org.eclipse.wst.server.core.model.ServerBehaviourDelegate.getTempDirectory(Z)Lorg/eclipse/core/runtime/IPath;
    at org.eclipse.jst.server.tomcat.core.internal.TomcatServerBehaviour.getTempDirectory(TomcatServerBehaviour.java:620)
    at org.eclipse.jst.server.tomcat.core.internal.TomcatVersionHelper.getStandardBaseDirectory(TomcatVersionHelper.java:154)
    at org.eclipse.jst.server.tomcat.core.internal.Tomcat60Handler.getRuntimeBaseDirectory(Tomcat60Handler.java:112)
    at org.eclipse.jst.server.tomcat.core.internal.TomcatServer.getRuntimeBaseDirectory(TomcatServer.java:316)
    at org.eclipse.jst.server.tomcat.core.internal.TomcatServerBehaviour.getRuntimeBaseDirectory(TomcatServerBehaviour.java:103)
    at org.eclipse.jst.server.tomcat.core.internal.TomcatServerBehaviour.getRuntimeProgramArguments(TomcatServerBehaviour.java:115)
    at org.eclipse.jst.server.tomcat.core.internal.TomcatServerBehaviour.setupLaunchConfiguration(TomcatServerBehaviour.java:833)
    at org.eclipse.wst.server.core.internal.Server.setupLaunchConfiguration(Server.java:1440)
    at org.eclipse.wst.server.core.internal.Server.getLaunchConfiguration(Server.java:1526)
    at org.eclipse.wst.server.core.internal.Server.startImpl2(Server.java:3048)
    at org.eclipse.wst.server.core.internal.Server.startImpl(Server.java:3001)
    at org.eclipse.wst.server.core.internal.Server$StartJob.run(Server.java:300)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)

    ————————————————————————————
    Error 2 :An internal error occurred during: “Publishing to Tomcat v6.0 Server at localhost…”.

    java.lang.NoSuchMethodError: org.eclipse.wst.server.core.model.ServerBehaviourDelegate.getTempDirectory(Z)Lorg/eclipse/core/runtime/IPath;
    at org.eclipse.jst.server.tomcat.core.internal.TomcatServerBehaviour.getTempDirectory(TomcatServerBehaviour.java:620)
    at org.eclipse.jst.server.tomcat.core.internal.TomcatVersionHelper.getStandardBaseDirectory(TomcatVersionHelper.java:154)
    at org.eclipse.jst.server.tomcat.core.internal.Tomcat60Handler.getRuntimeBaseDirectory(Tomcat60Handler.java:112)
    at org.eclipse.jst.server.tomcat.core.internal.TomcatServer.getRuntimeBaseDirectory(TomcatServer.java:316)
    at org.eclipse.jst.server.tomcat.core.internal.TomcatServerBehaviour.getRuntimeBaseDirectory(TomcatServerBehaviour.java:103)
    at org.eclipse.jst.server.tomcat.core.internal.PublishOperation2.(PublishOperation2.java:50)
    at org.eclipse.jst.server.tomcat.core.internal.PublishTask.getTasks(PublishTask.java:34)
    at org.eclipse.wst.server.core.internal.PublishTask.getTasks(PublishTask.java:89)
    at org.eclipse.wst.server.core.internal.Server.getTasks(Server.java:1186)
    at org.eclipse.wst.server.core.model.ServerBehaviourDelegate.getTasks(ServerBehaviourDelegate.java:1055)
    at org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publish(ServerBehaviourDelegate.java:834)
    at org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publish(ServerBehaviourDelegate.java:708)
    at org.eclipse.wst.server.core.internal.Server.publishImpl(Server.java:2731)
    at org.eclipse.wst.server.core.internal.Server$PublishJob.run(Server.java:278)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)

    ————————————————————————
    please sir help me what to do ..please solve my error & guide me.

  • Mythili

    Thank You.
    Your website with it’s well documented steps on so many areas is very helpful.

    Regards,
    Mythili

  • tomek

    Hi, i have problem with domains in my db, Hibernate dont see them, so i cant generate anything. For example i have column USER_ID : public.id

  • saurabh jain

    sir I m yu great fan. I learn many things from yu site.

    thanks sir vo bhi bada wala…

  • MIchael

    HI,

    I am currently working on a project. Which uses JSPs and stored procedure to populate the dropdown. I want to use hibernate. Ca I know what modificatioins I need to do ?

    Thanks..

  • Lusine

    How to generate annotated hibernate entities?

    • http://www.mkyong.com mkyong

      In Hibernate tools, clicked on the “generateejb3 annotation”. See figure above

  • Sayeesh

    This is exactly what i was looking for..

    Thank you very much… Very neatly described…

  • Koki

    I have a question.
    How do I automatically generate tag on hibernate mapping file with hibernate tools?

    sql-insert
    sql-delete
    sql-update

    • http://www.mkyong.com mkyong

      Don’t think so, Hibernate does not provide options to configure it.

  • http://ano.com ano

    can u tell me about setting up using the oracle 10g

    • http://www.mkyong.com mkyong

      Er…what you want to know? It should be not much different , just configure your db setting.

  • Pingback: How to install Hibernate Tools in Eclipse IDE | Hibernate()

  • Simple

    Thank you, it’s interesting.
    Can we change the strategy of code generation for inheritance?

    • http://www.mkyong.com mkyong

      Not really, Hibernate tool always generate consider *best* strategy based on your tables structure, it’s quite straightforward at the moment.

      Some manual works may need involve if you are not satisfied with the generated code.

      • Marvin

        Thanks for your article. I checked Annotations(jdk 1.5+) and I got java file and hbm file generated fine, but there is no annotations for java file. Did I miss something? How can I get annotations on java file.

        Marvin

        • Prakash

          Hi Marvin ,
          I too have same problem.. did you get it fixed ?
          mkyong any solutions for this problem ?

        • Francisco

          In Hibernate tools, use “generate ejb3 annotation”.

  • Pingback: Quick start Maven + Hibernate + MySQL Example | Hibernate()