Back to articles list
- 5 minutes read

How You Can Use Hibernate With a Database Modeling Tool

Before writing this article, I talked to a group of software engineers who use Hibernate on a daily basis in their work. Some of them work for small companies (up to 20 developers on site) while others are employed in corporations known world-wide that employ over 100,000 IT professionals. This gave me an insight into how development with Hibernate is organized in different companies.

There are several approaches to creating SQL scripts and Hibernate O/R Mapping classes.

  • In the first one, SQL script with DDL is created and then the relational model is mapped to the object model through .hbm.xml files. Then, Java classes are generated using Hibernate Tools.

  • The second method is to create SQL script, create a database in RDBMS and use Hibernate Tools to generate Java classes with its reverse-engineering feature.

  • In the third approach, developers create Java classes with annotations and use it as an input to Hibernate Tools to generate SQL script.

What I see here is a noticeable division into two groups: database-centric and object-centric. Developers treat DB structure either as a kind of model of reality or just as some low-level data storage they don’t really want to think about.

I’m not about to argue which approach is better. That’s not the point of my article. But this division got me started thinking about how I would organize my development process if I were to use Hibernate.

My Approach & Proof-of-Concept

I’m definitely a database-centric guy. I also don’t like writing DDLs and XMLs by hand (anyone does?). That’s why I’d model a database structure with some ERD tool first, generate SQL create script and then generate my ORM classes. The only thing I’d be required to do manually would be to model the reality in well-suited graphical application. Just a minimal-effort, time-efficient and elegant way.

So let’s make a Proof-of-Concept and let’s see how it works.

1. Design Your Database With Vertabelo

As stated before, we start with modeling a database. This is a job to do in Vertabelo, of course.

Let’s model a simple 4-table database structure. The final design should be as follows:


2. Generate Create SQL Script

Next, we want to download our generated DDL script. We can do this in two ways: manually from the Vertabelo UI or automatically through the Vertabelo API. The second option is available only for “Premium” and “Team” account plans, but it’s extremely useful in this process. You’ll see this later in this article.

3. Generate O/R Mapping Classes Using Reverse-Engineering

Now, we need to create a database in RDBMS, invoke generated SQL script and use Hibernate Tools’ reverse-engineering to generate the classes. This is not a one-time job. We want to regenerate these classes every time our DB model changes. That’s why I don’t even consider doing it manually or from IDE. I’m a big fan of making things as easy as possible through automation of repeatable tasks. It’s a perfect job for Ant, Maven, Gradle or any other building tool. Since Hibernate Tools include Ant tasks, let’s do this with Ant.

Here you can download a build.xml file with a sample configuration. Let’s describe its targets briefly:

  • download_vertabelo_model – downloads generated SQL create script from Vertabelo using API
  • generate_orm – generates Hibernate ORM classes upon existing database
  • drop_db – drops an existing database
  • create_db – creates a new database
  • create_db_structure – creates a DB structure using SQL script downloaded from Vertabelo
  • clean_db – invokes three previous tasks (drops an existing database, creates a new one and then creates a DB structure using SQL script downloaded from Vertabelo)
  • clean_db_and_generate_orm – it’s a composition of “clean_db” and “generate_orm” targets
  • download_model_and_generate_orm – it wraps the whole process of handling DB changes: downloads SQL create script, cleans DB and generates ORM classes

What you need to do is to change the properties at the beginning of the file to match your configuration.

The last thing we must do before we can generate our Hibernate classes is to setup Hibernate Tools. We need to add the file (this is a little bit redundant with the data you provided in build.xml file) and download all Hibernate Tools dependencies into lib/ directory. You can see the list of required dependencies in the build.xml file (*).

Final Result

So now, the whole process from changing DB model in Vertabelo to having the code regenerated is a two-step procedure:

  1. make some changes in Vertabelo
  2. invoke:
    ant download_model_and_generate_orm

Or, if you cannot use the Vertabelo API, it consists of three steps:

  1. make some changes in Vertabelo
  2. download generated SQL script from Vertabelo UI
  3. invoke:
    ant clean_db_and_generate_orm

That’s all! As of now, the process of transforming DB changes into code is that simple.

This is how I would organize my DEV environment, I think. In fact, I’d rather use Gradle instead of Ant and switch to a newer version of Hibernate, but for a Proof-of-Concept the described configuration was pretty enough.

What is your approach? Have you come up with any better solutions? I really look forward to getting feedback from you.

(*) Getting hibernate-tools-3.4.0.CR2.jar is a little bit tricky. Hibernate Tools docs says:

"2.3. Ant

To use the tools via Ant™ you need the hibernate-tools.jar file and associated libraries. The libraries are included in the distribution from the Hibernate™ website and the Eclipse™ update site. The libraries are located in the Eclipse™ plugins directory at /plugins/org.hibernate.eclipse.x.x.x/lib/tools/. These libraries are 100% independent from the Eclipse™ platform. How to use the Hibernate Tools™ via Ant™ tasks is described in Chapter 5, Ant Tools."

The step-by-step instruction is as follows:

  1. Go to
  2. Choose JBoss Tools 4.2.3.Final (Stable)
  3. Select “Artifacts” tab
  4. Choose: Update site (including sources) bundle of all Hibernate Tools:
  5. Unpack it and find JAR file:
  6. Unpack it and go to /lib/tools directory
  7. Gotcha! hibernate-tools-3.4.0.CR2.jar file is right there.
go to top

Our website uses cookies. By using this website, you agree to their use in accordance with the browser settings. You can modify your browser settings on your own. For more information see our Privacy Policy.