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 hibernate.hbm2ddl.auto property, as shown in Listing 1.

Listing 1. Configuring Hibernate to update your database schema automatically

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

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

@Entity
public class Client implements Serializable {


    @Id
    @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
Join the discussion
Be the first to comment on this article. Our Commenting Policies
See more