Hibernate is based on the Java technologies and it is used in the Java applications for developing the data access layer for the application.
Steps to create Hibernate based applications
Step 1: Create database and tables
Step 2: Create POJO class
Step 3: Create mapping meta-data
Step 4: CRUD operations
Configuring the Hibernate environment
I am using Eclipse IDE to perform configuration of Hibernate environment. Install Eclipse and add required JDK version of Java. Create a Java Project in Eclipse and follow the steps below:-
Add required Jar files to Java Project
Downloading the latest version of Hibernate, It includes the libraries necessary to run the Hibernate based applications.
Note: Please download only the required version of hibernate jar file from official website at http://hibernate.org/orm/downloads/
Installing Hibernate is very easy process, you just have to unzip and then copy the library files from its lib directory. If you are using Eclipse, you have to copy the files into lib directory of your project. After coping the file in your project's lib directory, add the lib files in the class build path as shown in above figure.
For copying the file into build path:
Right click on the project > Java Build Path > select "Libraries" > Add JARs > then select the jar files from your project, this will make the jar files available in your project.
Hibernate Configuration files
The Hibernate framework uses the hibernate.cfg.xml and other optional files for configuring the ORM runtime. The things you will have to change is the dialect, database url, database username and database password.
Here are the important properties of the file:
hibernate.connection.driver_class - The JDBC Driver class
hibernate.connection.url - Database URL
hibernate.connection.username - Database user name
hibernate.connection.password - Password to connect to database
hibernate.connection.pool_size - Default connection pool size
show_sql - If it is true it will show all the sql generated by Hibernate
dialect - Database dialect
Setup the MySQL database
I have used MySQL Database for developing the tutorial. MySQL is free database which works well with the Hibernate framework. We can use other database by changing few settings in the xml file and connect to the supported database.
Download the MySQL database and then setup of your computer. After installing MySQL database you should create database and table in the database. Add database driver into your Java Project. See in figure 1: I have added ojdbc6.jar file in my Java Project.
Note: remember the user name, password and port number of MySQL database.
Create a POJO class
Add the following code in package “com.user.bean”
Create connect class to connect your database in package “com.user.utill”
Write the code for Main class in package “com.user.main”
After executing the Main class you will get successful Insertion
You can use the Hibernate to perform operations like select, insert, update and delete the records in the table. Hibernate automatically generates the SQL query automatically to perform these operations against database.