In previous post Dirty checking in hibernate example we learnt what dirty checking concept in hibernate.
In this post we will understand most of the important properties available in hibernate.cfg.xml file
Let’s take hibernate configuration file from our previous example where we have learnt about Dirty checking in hibernate
hibernate.cfg.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:4406/test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <mapping class="com.infotech.entities.Employee"/> </session-factory> </hibernate-configuration> |
First line in the above XML document specifies XML version and encoding type
1 |
<?xml version='1.0' encoding='utf-8'?> |
Second line specify XML DTD(document type definition) type here basically hibernate-configuration-3.0.dtd validates this xml file.
1 2 3 |
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> |
DTD without version(recommended approach)
Here we can follow standard practice like we should not mention DTD version.
1 2 3 |
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration.dtd"> |
Now let’s explore one by one every property.
hibernate.connection.driver_class
This property takes value of JDBC driver class.Here we are using MYSQL database so we have provided JDBC driver class for MySQL database.
For Example for MySQL database we set JDBC Driver class as below in hibernate config file.
1 |
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property> |
This property value may be different for other databases.
hibernate.connection.url
This property takes the value of database URL
For example in above config file I have specified database URL for MYSQL as below
1 |
<property name="hibernate.connection.url">jdbc:mysql://localhost:4406/test</property> |
Let’s understand meaning of every literal in database URL:jdbc:mysql://localhost:4406/test
jdbc->It is the standard JDBC API through which hibernate applications use to interact with a database.
mysql->It is the database name i.e in case of oracle database it’s value would be oracle
localhost->it is the server name on which MySQL server is running, we may also use IP address.
4406->port number on which MYSQL database server is running
test->it is database name.
This property value may be different for other databases.
hibernate.connection.username
username name for MYSQL database
hibernate.connection.password
Password of MySQL database given at the time of installation
hibernate.dialect
We set a dialect property so that Hibernate knows which SQL variation it has to generate to your database.
For Example for MySQL database we set dialect as below in hibernate config file.
1 |
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> |
This property value may be different for other databases.
hibernate.hbm2ddl.auto
The possible values for hbm2ddl.auto are:
- create
- validate
- update
- create-drop
Create
If the value is CREATE then hibernate first drops the existing database table/tables, then creates a new table/tables and then executes operations on newly created table.
The only problem with the value “create” is, we loose existing table data.
validate
If we specify value as validate then hibernate only validates whether the table and columns are exists or not. If the table doesn’t exist in that case hibernate throws an exception.
update
If we specify value as update then, Hibernate checks for the table and columns. If table doesn’t exist in database then it creates a new table and if a column doesn’t exist it creates new column as well.
But in the case of value “update” hibernate doesn’t drop any existing table, here we doesn’t lose existing table or data.
create-drop
If the value is create-drop then, Hibernate first checks for a table and do the necessary operations and finally drops the table after all the operations are completed.
The value “create-drop” is given for unit testing/POC kind of functionalities in the hibernate.
hibernate.show_sql
This property enables the logging of all the generated SQL statements to the console(Eclipse console/logging file)
hibernate.format_sql
This property formats the generated SQL statement to make it more readable.
Finally register all your entity classes using using below XML <mapping >tag:
<mapping class=”qualifier name of you entity class”/>
let’s say i have only one entity class so i have registered as below in hibernate config file
<mapping class=”com.infotech.entities.Employee”/>
That’s all about hibernate config file.
You May Also Like:
Latest hibernate distribution Zip file download link
Hibernate 5 distribution binary details
Create SessionFactory in Hibernate5 using hibernate.cfg.xml
Create SessionFactory in Hibernate5 without hibernate.cfg.xml
Save and persist an entity example in hibernate
Hibernate CRUD(Create,Read,Update and Delete) example
Dirty checking in hibernate example
Why to use hibernate dialect?
Hibernate hbm2ddl property
What are the benefits of using hibernate?
If you have any feedback or suggestion please feel free to drop in below comment box.