Hibernate 4 Annotations

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

Ads

Tutorials   
How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate 4.3 Hello World: Example How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial Hibernate 4 Annotations Tutorial Hibernate 4 AnnotationSessionFactoryBean hibernate versions hibernate jndi hibernate tomcat hibernate metamodel generator hibernate entity Seam Hibernate C3P0 Hibernate hibernate shard hibernate means temporal hibernate jsf hibernate spring j2ee hibernate spring hibernate encrypted password alias_to_entity_map hibernate temporal jsf spring hibernate hibernate by example hibernate repository hibernate sample code
Ads

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

Ads
Share on Google+Share on Google+

Hibernate 4 Annotations

Posted on: February 17, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Hibernate 4 Annotations  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads