ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Hibernate Class Generation Using hbm2java
Pages: 1, 2, 3, 4, 5, 6

A Simple Class Generation Example

Let's start with a very simple example. Suppose we want to map a simple table called BOOK, as described here:

Column      |         Type          | Modifiers
 BOOK_ID    | character(32)         | not null
 BOOK_TITLE | character varying(80) | not null
 BOOK_ISBN  | character varying(20) | not null

To generate this class, we could use the following Hibernate mapping file. Note how meta-attributes can be used to add comments or fine-tune class generation.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"


    <class name="Book" table="BOOK">
        <meta attribute="class-description">
            A Book business object.
            @author Duke
        <id name="id" type="string" unsaved-value="null" >
            <column name="BOOK_ID" sql-type="char(32)" not-null="true"/>
            <generator class="uuid.hex"/>

        <property column="BOOK_NAME" name="name"/>
        <property column="BOOK_ISBN" name="isbn">
            <meta attribute="field-description"/>
            The unique ISBN code for this book.


Using this mapping file, the hbm2java will generate a class that looks something like this:

 * A Book business object.
 * @author Duke
public class Book {

    private String id;
    private String name;
    private String isbn;

    public Book() {

    public String getId() {
        return id;

    private void setId(String id) {
        this.id = id;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

     * The unique ISBN code for this book.
    public String getIsbn() {
        return isbn;

    public void setIsbn(String isbn) {
        this.isbn = isbn;

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow