Hibernate annotations tutorial
In the former Hibernate tutorial we have shown how to create a simple standalone application XML configuration files to map Entities. In this short tutorial we will learn how to replace the Hibernate's XML class mapping files with simple JPA annotations.
Hibernate, like all other object/relational mapping tools, requires metadata that governs the transformation of data from one representation to the other. Hibernate Annotations provides annotation-based mapping metadata.
Hibernate EntityManager implements the programming interfaces defined by the JPA persistence specification and together with Hibernate Annotations offers a complete JPA persistence solution on top of the Hibernate Core.
So, in order to map your database structure you can use Hibernate core (without annotations) or Hibernate with JPA annotations. Depending on the type of your business and technical needs, you can use either, however, using Hibernate with JPA annotations you can at any time switch to JPA compliant solution by simply replacing hibernate.cfg.xml with persistence.xml.
1. Start by downloading Hibernate distribution from the source.
2. Unzip the distribution and create a new Java Project
In the first example, we will test Hibernate annotations using a standalone application. For this purpose we will create a Java Project using Eclipse. Then we will map the class "Application" which maps the table "APPS", stored in an Oracle database.
Here's the class hibernate.objects.Application.java
As you can see @Entity declares the class as an entity (i.e. a persistent POJO class), @Id declares the identifier property of this entity. Since the table name does not correspond to the class name, we are using the @Table annotation to reference the table name.
The same happens for the column idApplicazione which maps the id_applicazione with the @Column annotations. All other columns bear the same database column name.
In order to start an Hibernate Session we will create an utility class named util.HibernateUtil.java
Notice: If you boot Hibernate yourself, you need to use the AnnotationConfiguration class instead of the Configuration class.
Now, all you have to do, is creating the file hibernate.cfg.xml (we'll place it in the src folder for convenience). This file contains the JDBC configuration and merely the list of Entities mapped:
That's all. Finish by adding the required libraries to your project. If you are running Hibernate as standalone you will need the Hibernate core libraries, some JBoss client logging libraries and of course your JDBC libraries.
Add Hibernate and JBoss libraries
Here's a snapshot of the Eclipse libraries needed:

The following class will test your Hibernate engine with a simple Query:

