Home Hibernate Hibernate4 Hibernate 4 Annotations
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Hibernate 4 Annotations

Advertisement
In this tutorial you will learn about the use of annotations in Hibernate.

Hibernate 4 Annotations

In this tutorial you will learn about the use of annotations in Hibernate.

To create an example in Hibernate using Annotation reader must have aware with the following :

  • Annotation is a mechanism to provide the metadata to the JVM. Before an introduction of Annotation XML files were used to provide the metadata information but the introduction of Annotation replaced the XML. This powerful mechanism is introduced from the Java 5. It is a java class that the JVM reads it at the runtime using the mechanism reflection and do process as it is defined for.

    Hibernate also needs to provide a metadata for mapping the Object and Relational table. In Hibernate to provide a metadata, responsible for transforming the POJO class data to the database table and vice-versa, most commonly a XML file is used. Drawback of this way is that we are required to create an additional .hbm.xml file where we give all the mapping information that may cause a tiresome process. Use of Annotation in Hibernate makes it a powerful and easy way to provide the metadata transformation from the POJO class to the Relational table and vice-versa. The metadata, required to provide for transforming the POJO class data to the Relational table and vice-versa, is gathered into the POJO class as well as the code that helps the user to understand the structure of table and POJO at the same time.

  • Requirements for project setting


    • JDK version should be 5 or higher.
    • Hibernate version should be 3.3 or higher (here we are using version 4 so we don't be required to download additionally).
    • Hibernate annotation supported jar files.
      • hibernate-commons-annotations-4.0.1.Final.jar
      • hibernate-jpa-2.0-api-1.0.1.Final.jar
      • mysql-connector-java-5.1.3-rc-bin.jar
      • hibernate-core-4.0.0.Final.jar


  • Some Annotations that are used for creating a project


    • @Entity : This annotation is used to specify the class is an Entity bean.
    • @Table : Specified a table to persist the data. If this annotation is not used by default Hibernate uses the class name as a table name.
    • @Id : Specified the Entity bean Identifier property.
    • @GeneratedValue : Specified the primary key generation strategy if not used then by default AUTO strategy is used.
    • @Column : Specified the column detail that a field or property will be mapped.
    • Note : Annotations that are discussed above are of package javax.persistence

  • Creating Project using Annotation
    • Start Eclipse
    • File -> New -> project -> Java Project ->yourProjectName -> Finish
    • Add Hibernate's jar files (to read how to add jar files to the project from here).
    • Create a POJO class using annotations (above annotations may be used).
    • Create a configuration file 'hibernate.cfg.xml'.
    • Develop a code to persist the data to your database.

Click here for Hibernate 4 Annotation Example

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 17, 2012

Ask Questions?    Discuss: Hibernate 4 Annotations  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com