How to Install Hibernate and its Configuration

Last updated on Jan 14 2023
Prabhas Ramanathan

Table of Contents

Hibernate – Environment

This blog explains how to install Hibernate and other associated packages to prepare an environment for the Hibernate applications. We will work with MySQL database to experiment with Hibernate examples, so make sure you already have a setup for MySQL database.

Downloading Hibernate

It is assumed that you already have the latest version of Java installed on your system. Following are the simple steps to download and install Hibernate on your system −
• Make a choice whether you want to install Hibernate on Windows, or Unix and then proceed to the next step to download .zip file for windows and .tz file for Unix.
• Download the latest version of Hibernate from http://www.hibernate.org/downloads.
• At the time of writing this tutorial, I downloaded hibernate-distribution3.6.4.Final and when you unzip the downloaded file, it will give you directory structure as shown in the following image

hi 9

Installing Hibernate

Once you downloaded and unzipped the latest version of the Hibernate Installation file, you need to perform following two simple steps. Make sure you are setting your CLASSPATH variable properly otherwise you will face problem while compiling your application.
• Now, copy all the library files from /lib into your CLASSPATH, and change your classpath variable to include all the JARs −
• Finally, copy hibernate3.jar file into your CLASSPATH. This file lies in the root directory of the installation and is the primary JAR that Hibernate needs to do its work.

Hibernate Prerequisites

Following is the list of the packages/libraries required by Hibernate and you should install them before starting with Hibernate. To install these packages, you will have to copy library files from /lib into your CLASSPATH, and change your CLASSPATH variable accordingly.

Sr.No. Packages/Libraries
1 dom4j

XML parsing www.dom4j.org/

2 Xalan

XSLT Processor https://xml.apache.org/xalan-j/

3 Xerces

The Xerces Java Parser https://xml.apache.org/xerces-j/

4 cglib

Appropriate changes to Java classes at runtime http://cglib.sourceforge.net/

5 log4j

Logging Faremwork https://logging.apache.org/log4j

6 Commons

Logging, Email etc. https://jakarta.apache.org/commons

7 SLF4J

Logging Facade for Java https://www.slf4j.org

Hibernate – Configuration

Hibernate requires to know in advance — where to find the mapping information that defines how your Java classes relate to the database tables. Hibernate also requires a set of configuration settings related to database and other related parameters. All such information is usually supplied as a standard Java properties file called hibernate.properties, or as an XML file named hibernate.cfg.xml.
I will consider XML formatted file hibernate.cfg.xml to specify required Hibernate properties in my examples. Most of the properties take their default values and it is not required to specify them in the property file unless it is really required. This file is kept in the root directory of your application’s classpath.

Hibernate Properties

Following is the list of important properties, you will be required to configure for a databases in a standalone situation −

Sr.No. Properties & Description
1 hibernate.dialect

This property makes Hibernate generate the appropriate SQL for the chosen database.

2 hibernate.connection.driver_class

The JDBC driver class.

3 hibernate.connection.url

The JDBC URL to the database instance.

4 hibernate.connection.username

The database username.

5 hibernate.connection.password

The database password.

6 hibernate.connection.pool_size

Limits the number of connections waiting in the Hibernate database connection pool.

7 hibernate.connection.autocommit

Allows autocommit mode to be used for the JDBC connection.

If you are using a database along with an application server and JNDI, then you would have to configure the following properties −

Sr.No. Properties & Description
1 hibernate.connection.datasource

The JNDI name defined in the application server context, which you are using for the application.

2 hibernate.jndi.class

The InitialContext class for JNDI.

3 hibernate.jndi.<JNDIpropertyname>

Passes any JNDI property you like to the JNDI InitialContext.

4 hibernate.jndi.url

Provides the URL for JNDI.

5 hibernate.connection.username

The database username.

6 hibernate.connection.password

The database password.

Hibernate with MySQL Database

MySQL is one of the most popular open-source database systems available today. Let us create hibernate.cfg.xml configuration file and place it in the root of your application’s classpath. You will have to make sure that you have testdb database available in your MySQL database and you have a user test available to access the database.
The XML configuration file must conform to the Hibernate 3 Configuration DTD, which is available at http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd.

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>

<property name = "hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>

<property name = "hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>

<!-- Assume test is the database name -->

<property name = "hibernate.connection.url">
jdbc:mysql://localhost/test
</property>

<property name = "hibernate.connection.username">
root
</property>

<property name = "hibernate.connection.password">
root123
</property>

<!-- List of XML mapping files -->
<mapping resource = "Employee.hbm.xml"/>

</session-factory>
</hibernate-configuration>

The above configuration file includes <mapping> tags, which are related to hibernatemapping file and we will see in next chapter what exactly a hibernate mapping file is and how and why do we use it?
Following is the list of various important databases dialect property type –

Sr.No. Database & Dialect Property
1 DB2

org.hibernate.dialect.DB2Dialect

2 HSQLDB

org.hibernate.dialect.HSQLDialect

3 HypersonicSQL

org.hibernate.dialect.HSQLDialect

4 Informix

org.hibernate.dialect.InformixDialect

5 Ingres

org.hibernate.dialect.IngresDialect

6 Interbase

org.hibernate.dialect.InterbaseDialect

7 Microsoft SQL Server 2000

org.hibernate.dialect.SQLServerDialect

8 Microsoft SQL Server 2005

org.hibernate.dialect.SQLServer2005Dialect

9 Microsoft SQL Server 2008

org.hibernate.dialect.SQLServer2008Dialect

10 MySQL

org.hibernate.dialect.MySQLDialect

11 Oracle (any version)

org.hibernate.dialect.OracleDialect

12 Oracle 11g

org.hibernate.dialect.Oracle10gDialect

13 Oracle 10g

org.hibernate.dialect.Oracle10gDialect

14 Oracle 9i

org.hibernate.dialect.Oracle9iDialect

15 PostgreSQL

org.hibernate.dialect.PostgreSQLDialect

16 Progress

org.hibernate.dialect.ProgressDialect

17 SAP DB

org.hibernate.dialect.SAPDBDialect

18 Sybase

org.hibernate.dialect.SybaseDialect

19 Sybase Anywhere

org.hibernate.dialect.SybaseAnywhereDialect

So, this brings us to the end of blog. This Tecklearn ‘How to Install Hibernate and its Configuration’ blog helps you with commonly asked questions if you are looking out for a job in Java Programming. If you wish to learn Spring and build a career Java Programming domain, then check out our interactive, Java and JEE Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Java and JEE Training

Java and JEE Training

About the Course

Java and JEE Certification Training is designed by professionals as per the industrial requirements and demands. This training encompasses comprehensive knowledge on basic and advanced concepts of core Java & J2EE along with popular frameworks like Hibernate, Spring & SOA. In this course, you will gain expertise in concepts like Java Array, Java OOPs, Java Function, Java Loops, Java Collections, Java Thread, Java Servlet, and Web Services using industry use-cases and this will help you to become a certified Java expert.

Why Should you take Java and JEE Training?

• Java developers are in great demand in the job market. With average pay going between $90,000/- to $120,000/- depending on your experience and the employers.
• Used by more than 10 Million developers worldwide to develop applications for 15 Billion devices.
• Java is one of the most popular programming languages in the software world. Rated #1 in TIOBE Popular programming languages index (15th Consecutive Year)

What you will Learn in this Course?

Introduction to Java

• Java Fundamentals
• Introduction to Java Basics
• Features of Java
• Various components of Java language
• Benefits of Java over other programming languages
• Key Benefits of Java

Installation and IDE’s for Java Programming Language

• Installation of Java
• Setting up of Eclipse IDE
• Components of Java Program
• Editors and IDEs used for Java Programming
• Writing a Simple Java Program

Data Handling and Functions

• Data types, Operations, Compilation process, Class files, Loops, Conditions
• Using Loop Constructs
• Arrays- Single Dimensional and Multi-Dimensional
• Functions
• Functions with Arguments

OOPS in Java: Concept of Object Orientation

• Object Oriented Programming in Java
• Implement classes and objects in Java
• Create Class Constructors
• Overload Constructors
• Inheritance
• Inherit Classes and create sub-classes
• Implement abstract classes and methods
• Use static keyword
• Implement Interfaces and use it

Polymorphism, Packages and String Handling

• Concept of Static and Run time Polymorphism
• Function Overloading
• String Handling –String Class
• Java Packages

Exception Handling and Multi-Threading

• Exception handling
• Various Types of Exception Handling
• Introduction to multi-threading in Java
• Extending the thread class
• Synchronizing the thread

File Handling in Java

• Input Output Streams
• Java.io Package
• File Handling in Java

Java Collections

• Wrapper Classes and Inner Classes: Integer, Character, Boolean, Float etc
• Applet Programs: How to write UI programs with Applet, Java.lang, Java.io, Java.util
• Collections: ArrayList, Vector, HashSet, TreeSet, HashMap, HashTable

Java Database Connectivity (JDBC)

• Introduction to SQL: Connect, Insert, Update, Delete, Select
• Introduction to JDBC and Architecture of JDBC
• Insert/Update/Delete/Select Operations using JDBC
• Batch Processing Transaction
• Management: Commit and Rollback

Java Enterprise Edition – Servlets

• Introduction to J2EE
• Client Server architecture
• URL, Port Number, Request, Response
• Need for servlets
• Servlet fundamentals
• Setting up a web project in Eclipse
• Configuring and running the web app with servlets
• GET and POST request in web application with demo
• Servlet lifecycle
• Servlets Continued
• Session tracking and filter
• Forward and include Servlet request dispatchers

Java Server Pages (JSP)

• Fundamentals of Java Server Page
• Writing a code using JSP
• The architecture of JSP
• JSP Continued
• JSP elements: Scriptlets, expressions, declaration
• JSP standard actions
• JSP directives
• Introduction to JavaBeans
• ServletConfig and ServletContext
• Servlet Chaining
• Cookies Management
• Session Management

Hibernate

• Introduction to Hibernate
• Introduction to ORM
• ORM features
• Hibernate as an ORM framework
• Hibernate features
• Setting up a project with Hibernate framework
• Basic APIs needed to do CRUD operations with Hibernate
• Hibernate Architecture

POJO (Plain Old Java Object)

• POJO (Plain Old Java Object)
• Persistent Objects
• Lifecycle of Persistent Object

Spring

• Introduction to Spring
• Spring Fundamentals
• Advanced Spring

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "How to Install Hibernate and its Configuration"

Leave a Message

Your email address will not be published. Required fields are marked *