Hibernate Example without IDE
Hibernate is a powerful tool used to build applications that need to interact with a database. It is a Java framework that implements the ORM(Object Relational Mapping) technique.
What is ORM?
ORM stands for Object Relational Mapping. It is a technique that is used to make Java objects persistent by mapping them into a relational database. It simplifies the interaction of an application with the database.
Hibernate internally uses JDBC(Java Database Connectivity), JTA(Java Transaction API), and JNDI(Java Naming and Directory Interface). It can be used to perform all the CRUD operations on the database. Given below is an example demonstrating the use of Hibernate without using an ide.
Step by Step Implementation
First, download the jar files for Hibernate and add them to the classpath. You can download the files from this link. Save all the required files(explained below) in a separate folder. Hibernate requires a configuration file that contains the information about the database used in the application, mapping resources, etc. It is an XML file usually saved as hibernate.cfg.xml. Below is the sample for Hibernate configuration file:
Create the POJO class for the objects to be mapped into the database. POJO class stands for Plain Old Java Object class which represents the class of objects that are needed to be stored in the database. Given below is an example of the Student class:
Create the Hibernate mapping file for the Student class. It contains important mapping information for the POJO class that needs to be mapped into the database like primary key, table name, column names, various constraints to be applied on the attributes, etc. Below is the Hibernate mapping file for the Student class
Now we can create a Test class that uses Hibernate to communicate with the database.
Save Student.java and Test.java in package p1. Execute the following commands on the terminal to compile the files:
Now execute the Test class using the following command:
The following details will be stored in the database:
Please Login to comment...