Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hibernate is a Java framework that simplifies the development of Java applications for interaction with the Database. It is a lightweight open-source, Object Relational Mapping(ORM) tool. Hibernate implements the specifications of Java Persistence API(JPA) for data persistence.
So, in this article, we will cover the concept of Hibernate Configuration in Java.
What is Hibernate Configuration?
Hibernate configuration has the information on the mapping of Java classes into the database table. This information is supplied by using any of the two files:
hibernate.cfg.xml
hibernate.properties
Hibernate needs a set of configuration settings associated with the database and other related parameters called Hibernate Configuration. All this information is usually supplied as a standard Java properties file, which can be hibernate.cfg.xml or hibernate.properties.
Most of the properties have their own default values and it is not needed to explicitly specify them in the property file unless a change is required. These files (hibernate.cfg.xml and hibernate.properties) are stored in the root directory of the classpath of the application.
The configuration of hibernate.properties file can be seen by using the following script:
The configuration of hibernate.cfg.xml file can be seen by using the following script:
<?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>
<!-- database name: database_name -->
<property name = "hibernate.connection.url">
jdbc:mysql://localhost/database_name
</property>
<property name = "hibernate.connection.username">
root
</property>
<property name = "hibernate.connection.password">
root
</property>
<!-- XML mapping files-->
<mapping resource = "Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Hibernate Properties
Following is the list of some important properties, that will be required for the configuration of a database in a standalone situation:
For a database along with an application server and JNDI, the following properties need to be configured-
Dialects in Hibernate Configuration
The dialect in the hibernate.cfg.xml file tells which type of database will be used in the application. To connect with the database, the SQL dialect needs to be provided in the configuration file.
Since there are so many RDBMS available, therefore, there are different dialects available for each one of them. A few of the databases along with their dialects are:
Oracle: org.hibernate.dialect.OracleDialect
MySQL: org.hibernate.dialect.MySQLDialect
Microsoft SQL Server: org.hibernate.dialect.SQLServerDialect
A few of the JDBC properties in Hibernate are as follows:
hibernate.connection.url: It specifies the URL of JDBC.
hibernate.connection.username: It specifies the database username.
hibernate.connection.driver_class: It mentions the JDBC driver class.
hibernate.connection.password: It specifies the database password.
hibernate.connection.autocommit: It specifies the mode of the JDBC connection.
hibernate.connection.pool_size: It specifies the size of the maximum number of connections in the pool.
Hibernate Mapping File in Hibernate Configuration
Relational/object mappings are generally defined in an XML document. This mapping file basically instructs Hibernate on how to map the defined class or classes to the database tables.
Several tools are available for generating the mapping document. These include Middlegen, XDoclet, and AndroMDA for advanced Hibernate users.
Since Hibernate can operate in different environments, it needs a wide range of configuration parameters. These configurations contain the mapping information that offers different functionalities to Java classes. Usually, database-related mappings are provided in the configuration file. Hibernate facilitates providing the configurations either in an XML file such as hibernate.cfg.xml or a properties file such as hibernate.properties. An instance of the Configuration class enables specifying properties and mappings to applications. The following script is used for Mapping:
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="name_of the class" table="name_of_the_table">
<meta attribute="class-description">
Write the description here
</meta>
<id name="name" type = "data_type" column = "column_name">
<generator class="native"/>
</id>
<property name = "name_of_the _property" column = "name_of_the_column" type = "data_type"/>
</class>
</hibernate-mapping>
Let us look a little deeper at the mapping elements used in the mapping file:
The mapping document is an XML document having <hibernate-mapping> as the root element, which has all the <class> elements.
The <class> elements define specific mappings from Java classes to the database tables. The Java class name is mentioned with the name attribute of the <class> element and the database table name is mentioned with the table attribute.
The <meta> element is not a necessary element and can be used to create the class description.
The <id> element maps the unique ID attribute in class to the primary key of the database table. The column attribute conveys the column in the database table and the name attribute of the id element conveys the property in the class. The type attribute stores the hibernate mapping type, this mapping converts from Java to SQL data type.
The <generator> element within the id element generates the primary key values automatically.
The <property> element maps a Java class property to a column in the database table. The name attribute of the element tells the property in the class and the column attribute tells the column in the database table. The type attribute has the hibernate mapping type, this mapping type will convert from Java to SQL data type.
Frequently Asked Questions
Mention a few Hibernate Datasource Properties.
What are the advantages of Hibernate Framework?
A few advantages of Hibernate Framework are as follows :
The Hibernate Query Language(HQL) is database-independent.
Hibernate framework offers the facility to create the tables of the database automatically.
It supports Query cache and offers statistics about query and database status.
It is easy to fetch data from multiple tables in hibernate framework.
What are the four layers of Hibernate architecture?
The four layers of the Hibernate architecture are:
Java application layer
Hibernate framework layer
Backhand API layer
Database layer
Conclusion
This article extensively discusses the concept of Hibernate Configuration. It goes on to extensively explain Hibernate Properties, Dialects in Hibernate, JDBC properties in Hibernate, and Hibernate Mapping File. It also includes appropriate tables to make the understanding of the topic better
To hold a tight grip on the topic of Hibernate Configuration, we recommend going through the topics: ORM, Interview Questions, and JDBC. We hope that this blog has helped you enhance your knowledge, check out our articles on Coding Ninjas Blogs.
If you want to test your competency in coding, you may check out the Mock Test Series and participate in the Contests organized on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the Problems, Interview Experiences, and Interview Bundle for placement preparations.
Nevertheless, you may consider our Courses to give your career an edge over others!