Hibernate Class Generation Using hbm2java

Hibernate Class Generation Using hbm2java

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"

    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">



<hibernate-mapping>



    <class name="Book" table="BOOK">

        <meta attribute="class-description">

            A Book business object.

            @author Duke

        </meta>

        <id name="id" type="string" unsaved-value="null" >

            <column name="BOOK_ID" sql-type="char(32)" not-null="true"/>

            <generator class="uuid.hex"/>

        </id>



        <property column="BOOK_NAME" name="name"/>

        <property column="BOOK_ISBN" name="isbn">

            <meta attribute="field-description"/>

            The unique ISBN code for this book.

            </meta>

        </property>

    </class>



</hibernate-mapping>



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;

    }

}



Prev  [1] [2] [3] [4] [5] [6] Next

Close    To Top
  • Prev Article-Java:
  • Next Article-Java:
  • Now: Tutorial for Web and Software Design > Java > JDOnJDBCnSQLJ > Java Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction