Object Cases

DB Importer

HiberObjects

DB Importer

DB Importer is an Eclipse plug-in for reverse engineering database schemas for use with Java Persistence API (JPA) or Hibernate. It is free for non-commercial use. See the Order page for commercial use.

Testimony

We are using DB Importer in developing an enterprise software, with over 300 tables and more than 1000 relations. We are using SmartGWT, Hibernate and MySQL. DB Importer automatically generates all the Java mapping between Hibernate and MySQL, which regularly saves us a huge amount of work - very nice tool, indeed!

Torbjörn Nordin, VIP Software AB, Sweden

Installation

Install "DB Importer" from the update site http://objectgeneration.com/update

If you have HiberObjects installed, use that update site and install the latest version of HiberObjects at the same time as installing DB Importer.

Getting Started

JDBC drivers for MySQL, PostgreSQL and SQLite are bundled with the plug-in. If you use another database, you need to add the JDBC driver to the project classpath.

Right-click on a Java package and select DB Importer > Import from Database. A wizard will be opened.

Import wizard

Configure the JDBC connection. The VM args parameter can be used to configure memory etc for the JVM that runs the import.

Limitations with SQLite:

Depending on your database, you may be asked to select a database schema. Select schema and push Finish.

The importer will now import the database schema, save it in a file called dbimport.xml, and open this file a special editor:

DB Importer editor

The upper table displays the tables in the database, and the lower table displays the columns in the selected database table. Click on a table or column to see a preview of the Java code that will be generated.

Just push Generate all to generate the default code.

Modify the generated code

You can edit the values in the tables to modify the generated code. First, double-click on the g icon so it changes to a pencil. Then, you can edit the value. String values are edited as text. Boolean values are toggled between true and false by clicking on them.

Save the editor to update the code preview. Then, push Generate all to generate the modified code.

Edit import script

Configure the import script

All the values that are indicated with a g are generated by the Groovy script ImportDB.groovy which is created automatically in the project. You can modify this script instead of editing all the values manually. The script can also be modified in other ways to generate any Java code you can imagine. (Don't worry if you don't know Groovy, the syntax of this script is just like Java.)

This can be used to for instance:

See examples below.

If you double-click a g value (not the icon but the value), the script will be opened and the cursor located at the corresponding method in the script.

Edit import script

When you edit the script and save it, the tables and Code Preview will be updated. In this way you can experiment interactively until you get the desired results. Then push Generate all to generate the code.

The input to the script are DatabaseSchema, Table and Column objects. The source code for these classes is available here.

Should you enter an error, the table or column icon will get a red marker. Other problems such as name conflicts or illegal Java identifiers will also be indicated. You can select that table or column to see the error message. (Hint: Change the script a little at a time.)

Examples

Here are some examples of how the script can be used.

Generate @Version annotation on all fields with name "version""

if(column.name == "version") var.addAnnotation("@javax.persistence.Version")

Generate @GeneratedValue(strategy=GenerationType.AUTO) on all PK fields

var.addAnnotation("@javax.persistence.GeneratedValue", "strategy", "javax.persistence.GenerationType.AUTO");

Convert all tinyint SQL columns to Boolean or boolean Java types

addSqlType("tinyint", "Boolean", "boolean");

Let some classes implement a certain interface

if(table.name != 'system') cl.addImplements('SystemObject')

Generate both relation and attribute for a field

This can be useful with frameworks such as SmartGWT, which don't handle JPA relations very well.

if(importRelation(column)) { JavaVariable var = new JavaVariable(); var.name = underscoreToCamelCase(column.name, false); var.type = sqlType2Java(column); var.multiplicity = (column.notNullable ? JavaVariable.ONE : JavaVariable.ZERO_ONE); addVariable(column, cl, var); var.addAnnotation("@javax.persistence.Column", "name", quote(column.name)); }

Generate ordered lists for some collections

if(var.inverseName.endsWith("_list")) { var.inverse.addAnnotation("@javax.persistence.OrderBy", "value", "\"sortOrder\""); var.inverse.collectionInterface = "java.util.List"; var.inverse.collectionClass = "java.util.ArrayList"; }

Generate enum types instead of int for some fields

if(column.name.endsWith("status")) { return "com.xyz.data.StatusEnum" }

Check database design rules

// Check if all unique constraint names ends with "_UNIQUE" for(Index c : table.uniqueConstraints) { boolean primaryKeyConstraint = true; for(String columnName : c.columns) { Column column = table.findColumn(columnName); if(!column.primaryKey) { primaryKeyConstraint = false; } } if(!primaryKeyConstraint && !c.name.endsWith("_UNIQUE")) { errors.add(table, "Invalid constraint name " + c.name) } }

Import unique constraints

Unique constraints are imported by the default script to generate @Table(uniqueConstraints...) annotations.

Generate metadata

See the forum.

Test with JPA

To use the imported code, you need to add the JPA libraries and JDBC driver to the project. This can be done with for instance Maven (see How to use DB Importer with Maven.)

But a quick way to try out JPA is to create a test application. Right-click on a Java package and select DB Importer > New Test Application. A wizard will be opened.

Test application wizard

Push Finish. This will add libraries to your project and create a Java class and a persistence.xml file and open these files in Eclipse. With just some quick fixes of the Java class, this should be ready to run as an application that prints some data from your database.

Installation

Install DB Importer from the update site: http://objectgeneration.com/update

DB Importer and HiberObjects generates code that needs a JPA implementation such as Hibernate. These libraries can be found in the hibernate plugins in the distribution. They are also available from Hibernate.org.

Some examples for DB Importer are available here. This includes JPA code for FireFox bookmarks and PHPBB3.