Providing a hibernate configuration using an XML file
In the preceding discussion, you learned how to create a class and provide a mapping to hibernate. These mappings show the relationship between the Java class and the database table.
Still, hibernate requires some information about the database, host, and port, on which the application is running. It also requires information about the username and password to access the database. Hibernate uses this set of configurations to connect to the database.
This is a traditional way to provide the hibernate configuration; however here, we need to create an XML file, generally called hibernate.cfg.xml
, in the classpath. There is no strict rule to name it hibernate.cfg.xml
; we can give it a custom name instead of hibernate.cfg.xml
, in which case, we need to instruct hibernate to load the configuration from the particular file. Otherwise, hibernate looks for the file named hibernate.cfg.xml
in the classpath.
How to do it...
Now, we will create the XML file that shows the configuration for MySQL:
- Enter the following code in
hibernate.cfg.xml
to show the configuration for the applications:... <hibernate-configuration> <session-factory> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/kode12 </property> <property name="hibernate.connection.username"> root </property> <property name="hibernate.connection.password"> root </property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <!-- List of XML mapping files --> <mapping resource="Employee.hbm.xml"/> <mapping resource="Department.hbm.xml"/> </session-factory> </hibernate-configuration>
How it works...
Here, we will take a look at only the basic configuration parameters. Let's understand the meaning of each property:
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
: This property helps hibernate to generate database-specific SQL statements. This is an optional property. According to hibernate documentation, hibernate will be able to choose the correct implementation ofdialect
automatically using the JDBC metadata returned by the JDBC driver.<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
: Using this property, we can provide the Fully Qualified Name (FQN) of the javadriver
name for a particular database. Thedriver
class is implemented using Java and resides in the JAR file and contains the driver that should be placed in our classpath.<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/kode12</property>
: Using this property, we can provide the physical location of the database; however, the connection URL may vary from database to database. Here, we will use the MySQL database, so the URL showsjdbc:MySQL://<host/computer-name/ip>:<port>/<database name to connect>
.<property name="hibernate.connection.username">root</property>
: Using this property, we can provide the username to access a particular database.<property name="hibernate.connection.password">root</property>
: Using this property, we can provide the password to access a particular database.<property name="show_sql">true</property>
: The possible value for this property is eithertrue
orfalse
. This is an optional property. Hibernate logs all the generated queries that reach the database to the console if the value ofshow_sql
is set totrue
. This is useful during basic troubleshooting. Hibernate will use the prepared statement so that it does not display the parameter in the output window. If you want to see this parameter as well, you will have to enable the detailed log. Log4j is preferred for the detailed log.<property name="hbm2ddl.auto">create</property>
: The possible values arevalidate
,update
,create
orcreate-drop
. This is also an optional property. Here, we will set value=create
so that it will remove all the schemas and create a new one using the hibernate mapping on each build ofsessionfactory
. For value=update
, hibernate will update the new changes in the database.Note
Do not use the
hbm2ddl.auto
property in the production environment because it may remove all of the data and schema. So, it's best practice to avoid it in the production environment.<mapping resource="Employee.hbm.xml"/>
: All of the mapping file is declared in themapping
tag, and the mapping file is always namedxx.hbm.xml
. We can use multiple mapping tags for multiple mapping files.Here is an example:
<mapping resource="Employee.hbm.xml"/> <mapping resource="Department.hbm.xml"/>
There's more…
Here are some useful properties used in hibernate:
hibernate.format_sql
:- The possible values are
true
andfalse
- It shows the hibernate-generated queries in the pretty format if set as
true
- The possible values are
hibernate.connection.pool_size
:- The possible value is always greater than
1
(value >= 1)
- It limits the maximum number of pooled connections
- The possible value is always greater than
hibernate.connection.autocommit
:- The possible values are
true
andfalse
- It sets the
autocommit
mode for JDBC
- The possible values are