Updated March 20, 2023
Introduction to Hibernate Annotations
Hibernate annotations can be considered as metadata to link the JAVA applications to the relational database in the backend. Hibernate is used to map the traditional relational databases with front end JAVA applications. Hibernate works as a bridge to connect object-oriented language (JAVA) to relational database either with the help of a file ending with “.hbm” containing all the necessary mapping or the same mapping can be done using annotations. Annotations are embedded in JAVA thus eliminating the use of creating any special file. Annotations can be identified by “@” at the start of the entity for ex: @Entity, @table, @Id, etc.
Annotations are derived out of the primary JAVA package named javax.persistence to support its use. Annotations are based out of JPA (JAVA Persistence API) specifications. To implement JAVA annotations we have to make sure to use JDK 5.0 or above version.
Hibernate Annotations with Code Snippets
Dive deeper in some of the code snippets explained:
We need to set up some files before running a hibernate program. Outlines of steps to be followed for writing a hibernate program using annotations.
- You need to have hibernated annotations installed in your device preferably Hibernate 3.x and copy hibernate-annotations.jar, lib/ejb3-persistence.jar and lib/hibernate-common-annotations.jar to your CLASSPATH environment variable.
- To create a Maven project using Eclipse IDE. We can choose the file (top right corner) -> New-> Maven project and then select the desired workspace in the following pop screen.
- By clicking on catalog type we get options to select group ID, artifact ID, and version as per your requirements. This can be chosen from the pop-up.
- Add dependencies and configurations in the pom.xml file. You can locate the pom file in the right-side drop-down list under the project you created in step 1.
- Since oracle drivers are not there in public Maven repository so they need to be installed from outside. After installing Maven, we need to follow this path to install oracle.
- Run the command :
-
install-file -Dfile=Path/to/your/ojdbc14.jar -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=12.1.0 -Dpackaging=jar
- Now we can create a persistence class containing annotations. This class can be saved with an extension of “.JAVA”.
- We have to create one config file with an extension of “.cfg.xml”.
- Finally, create a class to store or retrieve data. This will be saved as an extension of “.JAVA”. This file will be the main triggering file which in turn invoke files that we created initially.
Query to create a table
Below is the database query to create a table.
Create table Trainee(
code int not null auto_increment,
fname VARCHAR(30),
lname VARCHAR(30),
PRIMARY KEY (code)
);
Code piece to explain annotations
package com.eduCBA.mypackage;
//this defines a code under package. This is kind of encapsulating the code in the package.
import javax.persistence.Table;
//Table annotation is to be used in the below code that is the reason we are getting the characteristics of table annotations from persistence class.
import javax.persistence.Id;
//Id annotation is to be used in the below code that is the reason we are getting the characteristics of table annotations from persistence class.
import javax.persistence.Entity;
//Entity annotation is to be used in the below code that is the reason we are getting the characteristics of table annotations from persistence class.
@Entity
//This is a way to use annotations. We prefix the element from the “@” sign. This annotation marks the class as entity bean so it will have a no-argument constructor and will have a protected access modifier.
@Table(name= "Edu_rec")
//name is the characteristic of table entity. The table is coming from the relational database. The name of the table to be stored is “Edu_rec” This annotation can be used to modify various attributes of the table like its schema, name and much more.
public class Trainee {
//encapsulating the code in public class trainee.
@Id
//This annotation is to denote primary key.
private int code;
private String fname, lname;
//declared some private integer and string data-type to store the unique identification code, first and last name to store or get data from the table saved in database. These data –types connect to table data via annotations.
public int getCode() {
// This function is created to get the unique identification code from the database table. This code is to pull out data from the database for display or some other use in the front end.
return code;
}
public void set code (int code) {
//This function is to set the unique code in case code is not there in the database. “This” pointer here points towards the current code which needs to be updated to the code which we are passing as a parameter to this function.
this.code = code;
}
public String getFname () {
// This function is created to get the first name from the database table. This code is to pull out data from the database for display or some other use in the front end.
return fname;
}
public void setFname(String fname) {
//This function is to set the first name in case code is not there in the database. “This” pointer here points towards the current code which needs to be updated to the code which we are passing as a parameter to this function.
this.fname = fname;
}
public String getLname() {
//Same as above the only difference is that we are dealing with data containing the last name in case of the first name.
return Lname;
}
public void setLname(String Lname) {
//Same as above the only difference is that we are dealing with a data containing the last name in case of the first name.
this.Lname = Lname;
}
}
Conclusion
Hibernate annotation is a magical method to embed database functions in the JAVA code itself hereby reducing the efforts, time and memory to create a separate file for the same work. These annotations are supported by JAVA persistence libraries. This is one of the flexible and useful ways when you are creating standalone data-intensive projects.
Recommended Articles
This is a guide to Hibernate Annotations. Here we discuss the introduction, Dive deeper into annotations with some code snippets. You can also go through our other suggested articles to learn more–