Java
  Home arrow Java arrow Page 3 - Introducing Hibernate
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Introducing Hibernate
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 27
    2005-12-06

    Table of Contents:
  • Introducing Hibernate
  • Object Relational Mapping: What is it?
  • Getting Started With Hibernate
  • Hibernate in the Real World

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Introducing Hibernate - Getting Started With Hibernate


    (Page 3 of 4 )

    ORM frameworks release the developer from the constraints of thinking in terms of relation while working with classes. There are many frameworks that come under this category. What makes Hibernate different from others is its simplicity and flexibility. To make one’s application Hibernate enabled, there are three prerequisites that need to be fulfilled, which are:

    1. Persistence Class:

    The persistence class is a Plain Old Java Object or POJO model. A POJO is comparable to a JavaBean, having getters and setters to access its properties that are the instance variables. The persistence class has the following characteristics:

    a. It is the Object Oriented blueprint of the table to be persisted.

    b. The attributes of the table become the instance variable of the persistence class.

    c. The data type of the instance variables is the domain of the attributes.

    d. The object of the persistence class represents the row of the table.

     2. Mapping file:

    The mapping file is an XML file that contains detailed mapping between the persistence class and the table it represents. The required elements of this XML file are:

         a. hibernate-mapping:

             Hibernate-mapping is the root element enclosing all other elements.

         b. class:

    Class is used to map the table name to the persistence class. The name attribute  is used to specify the class name, and the table attribute is used to specify the table which the class represents. For example, to map a table named ORDERS to a persistence class with a fully qualified name of com.someorg.persist.Order the class element would be:

    <class name=”com.someorg.persist.Order” table=”ORDERS”>…</class>

           c. id:

    This element is used to map the primary key of the table to an instance variable of the class. The column child element of the id can be used to map the primary key to the corresponding variable. Whether the value of the primary key can be automatically generated is declared here. The generator element can be used to tell Hibernate whether a class would be used to auto generate the ids, or that the ids are being assigned by the application. The most common approach is assigned. For example:

          <id name="id" type="string" 
                unsaved-value="null">
            <column name="id" sql-type="char(32)" 
                    not-null="true"/>
            <generator class="assigned"/>
           </id>

    tells Hibernate that the name of the primary key is “id” which is mapped to the  instance variable “id”. The data type of the variable is “string” which is mapped to the column “id” having Domain as “char(32)” represented by the sql-type attribute. The generator’s class attribute contains the value “assigned” meaning that the application itself will provide the value of the primary key.

         d. property:

    This element, along with its child element column, maps the other attributes (or columns) to the instance variables of the persistence class. The name attribute of the property contains the name of the variable as a value. The name attribute of the column element contains the name of the column to which the instance variable is mapped. The length and sql-type attributes point to the length and data type of the column. To cite an example:

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

    maps the Column “name” to instance variable “name”.

    3. Hibernate Configuration file:

    This file can be called the entry point to a Hibernate enabled application. The reason is that it contains the configuration that controls the communication with the underlying database. It is here that all the mapping files being used by the application are declared. The hibernate-configuration element forms the root element. The most common elements used in this XML file are:

    a. property:

    The name attribute of the property element can be used to configure various parameters. In the terminology of Hibernate, these parameters are known as property parameters. Database server type (MySQL, Oracle, and so on), dialect class, URL of the database, username/password et. al. are the various property parameters. To put it in code:

    <session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
    <property name="connection.url">jdbc:hsqldb:data/tutorial</property>
    <property name="connection.username">sa</property>
    :
    :
    </session-factory>

         b. mapping:

    It is through the mapping element that all the mapping files, also called hbm files, to be used by the application are declared. The resource attribute is given the value of the path to the hbm file. To elucidate:

    <mapping resource="Event.hbm.xml"/>

    That is all one needs to get started with Hibernate. In the next section I will put it all together to provide a view into the real world of Hibernate.

    More Java Articles
    More By A.P.Rajshekhar


       · HiWorking with databases is a complex thing if not hard. That is where ORM...
       · simple and easy. Nice work. thanks for the tutorial.
       · Hi Rajashekar, This is very good article about the Hibernate Frame work for me as...
     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT