Hibernate Annotations

Hibernate Annotations
Techiio-author
Written by Sagar RabidasFebruary 14, 2022
10 min read
Hibernate
2 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 2 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

Let's start the discussion with Hibernate Annotations

Hibernate Annotations

Hibernate annotations can be taken into consideration as metadata to link the java programs to the relational database within the backend. Hibernate is used to map the conventional relational databases with the front-give-up java packages. Hibernate works as a bridge to connect item-oriented language (java) to relational databases both with the assist of a report finishing with “.Hbm” containing all the important mapping or the identical mapping may be finished using annotations. Annotations are embedded in java accordingly doing away with using developing any unique record. Annotations may be diagnosed through “@” at the start of the entity forex: @entity, @table, @identification, and many others.

Annotations are derived out of the number one Java package named java. Staying power to support its use. Annotations are primarily based on JPA (java staying power API) specifications. To enforce java annotations we ought to make certain to use JDK 5.0 or the above model.

Hibernate Annotations with Code Snippets

Dive deeper into 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 the 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 invokes 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,
name VARCHAR(30),
name  VARCHAR(30),
PRIMARY KEY (code)
);

Code piece to explain annotations

package com.eduCBA.package;

//this defines a code under the package. This is kind of encapsulates 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 the 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 the persistence class.

import java. 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 the persistence class.

@Entity

//This is a way to use annotations. We prefix the element from the “@” sign. This annotation marks the class as an 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 the public class trainee.

@Id

//This annotation is to denote the 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 the 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 the 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 the case of the first name.

return Lname;
}
public void setLname(String Lname) {

//Same as above the only difference is that we are dealing with data containing the last name in the case of the first name.

this.Lname = Lname;
}
}

Hidernate
HibernateAnnotations
Java
2 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 2 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Sagar Rabidas
Software Developer
Techiio-followerTechiio-followerTechiio-followerTechiio-followerTechiio-follower
+8 more
300 Blog Posts
14 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance