EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Hibernate Annotations

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Java Technology Tutorial » Hibernate Annotations

hibernate annotions

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  1. Run the command :
  2. install-file -Dfile=Path/to/your/ojdbc14.jar -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=12.1.0 -Dpackaging=jar
  1. Now we can create a persistence class containing annotations. This class can be saved with an extension of “.JAVA”.
  2. We have to create one config file with an extension of “.cfg.xml”.
  3. 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.

Popular Course in this category
Java Training (40 Courses, 29 Projects, 4 Quizzes)40 Online Courses | 29 Hands-on Projects | 285+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.8 (9,026 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)JavaFX Training (1 Courses)JMeter Testing Training (3 Courses)

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–

  1. Hibernate Framework
  2. Hibernate Versions
  3. Hibernate Validator
  4. Hibernate Tools

Java Training (40 Courses, 29 Projects, 4 Quizzes)

40 Online Courses

29 Hands-on Projects

285+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
Hibernate Tutorial
  • Basic
    • What is Hibernate
    • What is Java Hibernate
    • Hibernate Versions
    • Hibernate Architecture
    • Hibernate Framework
    • Hibernate Tools
    • Hibernate Annotations
    • Hibernate Mapping
    • Hibernate Many to One
    • Hibernate Many to Many
    • Hibernate Dialect
    • Hibernate Session
    • Hibernate SessionFactory
    • Lazy Loading in Hibernate
    • Hibernate Criteria
    • Hibernate Persist
    • Hibernate EntityManager
    • Caching in Hibernate
    • Cascade in Hibernate
    • Hibernate Generator
    • Hibernate Validator
    • Spring Hibernate Integration
    • Hibernate Query Language
    • Hibernate Named Query
    • Hibernate Interview Questions
    • HQL
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Java Training (40 Courses, 29 Projects, 4 Quizzes) Learn More