Generic Hibernate Application Requirements – Annotation
The objective of this example is to understand the general requirement to be followed in creating any hibernate application in Java. You may want to look at Hibernate Installation/Setup on Eclipse IDE article if Hibernate is not installed already on your system.
In general any hibernate application, must have the following 3 files,
1) Configuration XML – This file will be used to store database connection information and schema level settings.
2) Entity Class – This class will be java POJO having hibernate annotations.
3) Java file to access this configuration file and write our logic
These files are the minimum requirement to run any hibernate application, in case of complex application we may require many Entity classes.
Note : Number of Entity classes = Number of Tables in database
Hibernate Configuration file
In this mapping file Hibernate gets to know about the type of database and its connection properties as well as about the Entity class.
File: hibernate.cfg.xml
oracle.jdbc.driver.OracleDriver system admin jdbc:oracle:thin:@xxx.x.x.x:1521:XE org.hibernate.dialect.Oracle10gDialect true update
Entity class
The entity class is where the Hibernate mapping comes into play, this class tells Hibernate what table in the database it has to access, and what columns in that table it should use. For each member variable defined in Entity class you must create a getter and a setter.
File: StudentEntity.java
package entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "STUDENT") public class StudentEntity { @Id @Column(name = "ID") private int id; @Column(name = "NAME") private String name; @Column(name = "DEPARTMENT") private String department; @Column(name = "COLLEGE") private String college; // Create Getters and Setters }
As shown above, the mapping file contains several annotations, listed below are its role:
- @Entity annotation marks this class as an entity bean
- @Table annotation allows you to specify the details of the table that will be used to persist the entity in the database.
The @Table annotation provides four attributes, allowing you to override the name of the table, its catalogue, and its schema, and enforce unique constraints on columns in the table. For now we are using just table name which is STUDENT.
- @Id Annotations:– In hibernate each entity bean should have a primary key, which you annotate on the class with the @Id annotation. The primary key can be a single field or a combination of multiple fields depending on your table structure.
- @Column, The @Column annotation is used to specify the details of the column to which a field or property will be mapped. Here the name attribute in @Column permits the name of the column to be explicitly specified.
In our next article we shall learn about Generic Steps to be followed to use Hibernate in any Java application, going forward using this two articles we shall implement Hello World example of Hibernate 4 in Eclipse