- Java Hibernate Cookbook
- Yogesh Prajapati Vishal Ranapariya
- 632字
- 2021-07-16 19:59:46
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.<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
: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
- 少兒人工智能趣味入門:Scratch 3.0動畫與游戲編程
- Arduino開發實戰指南:LabVIEW卷
- WordPress Plugin Development Cookbook(Second Edition)
- Mastering KnockoutJS
- Visual Basic程序設計實踐教程
- MATLAB 2020從入門到精通
- 微信小程序全棧開發技術與實戰(微課版)
- Go語言精進之路:從新手到高手的編程思想、方法和技巧(2)
- 智能搜索和推薦系統:原理、算法與應用
- .NET Standard 2.0 Cookbook
- 精通MySQL 8(視頻教學版)
- 零基礎學Python編程(少兒趣味版)
- AMP:Building Accelerated Mobile Pages
- Java程序設計教程
- 編程風格:程序設計與系統構建的藝術(原書第2版)