Hibernate tool to create basic mappings based on a mysql db

11,887

Solution 1

Hibernate 3 has a toolset called Hibernate Tools that provides an Eclipse plugin and an Ant task that both support Reverse Engineering:

Reverse Engineering: The most powerful feature of Hibernate Tools is a database reverse engineering tool that can generate domain model classes and Hibernate mapping files, annotated EJB3 entity beans, HTML documentation or even an entire JBoss Seam application in seconds!

...

Ant task: The Hibernate3 tools include a unified Ant task that allows you to run schema generation, mapping generation, or Java code generation as part of your build.

For the Eclipse plugin, have a look at this section of the documentation. For the Ant task, have a look at the section about reverse engineering and optionally how to control it.

Note that other IDEs also provide support for reverse engineering (see Hibernate Reverse Engineering for Netbeans 6.5 and Generating Persistence Mappings from Database Schema for IntelliJ).

Solution 2

I just happened to stumble across this problem and I think I have found a perfect tool for the job.

http://hibernatepojoge.sourceforge.net/


Features : (in case you are lazy to go through the provided link)

  • Java objects representing each table using annotations for use with Hibernate.
  • A JUnit test case per table that uses the objects generated to create, populate, save, retrieve and compare results
  • DAO per class
  • The appropriate enumeration files
  • Spring and hibernate configuration
  • DAO layers
  • A data factory class per schema to return a pre-populated object with random data (for boundary checking, database population, etc)

Also supports:

  • Join tables including those with additional fields in link tables
  • Polymorphism/inheritance support
  • Composite Keys
  • One-To-One, many-to-one, many-to-many, etc
  • Multiple schema support (4 modes)
  • Natural Keys
  • Enumerations (including those entries which cannot be mapped cleanly onto the java world)
  • A whole bunch of more stuff (see sample.xml)

I would also like to say that setting this up is quite straight forward; You just have to have a hibernate configuration file and the jar file downloaded from the site. Then it's just a matter executing a jar file, passing the config.xml as a parameter!

Share:
11,887
Blankman
Author by

Blankman

... .. . blank

Updated on June 04, 2022

Comments

  • Blankman
    Blankman almost 2 years

    Are there any tools that can look at a database, and generate the basic mapping files?

    It would be great if it could take a database, and create the actual model (java classes) with annotations, but not sure if that exists?

  • mrgenco
    mrgenco over 6 years
    Thanks a lot..This is exactly what i was looking for.