Custom schema generation with Hibernate annotations

Automated schema generation meets legacy database naming conventions

For a Java developer, Hibernate 3 annotations offer a terrific way to model the domain layer. Without much effort, you can get Hibernate to generate your database schema automatically, dispensing entirely with SQL scripts. Back in the real world, however, you still need to account for the sometimes obscure naming conventions used by your database administrator. In this article, Java Power Tools author John Ferguson Smart shows you how to generate database schemas automatically with Hibernate while still making your DBA happy.

Hibernate 3 annotations are a great way to manage your database persistence. With annotations, you don't need to bother with XML mapping files, and you can leverage the usually sensible default behavior to drastically reduce the amount of code you need to maintain. Even better, Hibernate comes with a great tool that lets you generate the database schema automatically. So Hibernate creates and updates the database schema all by itself, and you don't have to worry about arcane SQL scripts.

First steps: Updating your database schema

Getting Hibernate to update your database schema automatically is easy enough. All you need to do is to set the property, as shown in Listing 1.

Listing 1. Configuring Hibernate to update your database schema automatically

      <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
      <property name="">create-drop</property>
      <!-- Persistent classes -->
      <mapping class="com.mycompany.myapp.domain.Client"/>
      <mapping class="com.mycompany.myapp.domain.Order"/>

Setting this property to create-drop will create a brand-new database each time you start the application, which is great for integration tests, but not so hot anywhere else. If you set this value to update, on the other hand, Hibernate will only create the database if it doesn't already exist, and will update any existing tables to match your current domain model.

Now, by default, Hibernate will generate a set of tables and fields that look very much like your Java classes, which, from your point of view as a Java developer, is just fine. Consider for example the simple annotated persistent class in Listing 2.

Listing 2. A simple persistent class

public class Client implements Serializable {

    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String firstName;

    private String lastName;

For this class, Hibernate would by default generate an SQL schema along the lines of what you see in Listing 3.

Listing 3. The SQL table generated for the Client class using the default Hibernate options

create table Client (
        id bigint generated by default as identity (start with 1),
        firstName varchar(255),
        lastName varchar(255),
        primary key (id)
1 2 3 4 Page 1
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.