”;
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 PUBLIC "-//Hibernate/Hibernate Configuration DTD 5.3//EN" "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hbm2ddl.auto">update</property> <property name="dialect">org.hibernate.dialect.MySQL8Dialect</property> <property name="connection.url">jdbc:mysql://localhost/TUTORIALSPOINT</property> <property name="connection.username">root</property> <property name="connection.password">guest123</property> <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property> <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?
hbm2ddl.auto Property
The hbm2ddl.auto property in Hibernate defines how your database schema is handled. Possible values are:
-
create − If the value is ”create”, Hibernate creates a new table in the database when the SessionFactory object is created. In case a table exists in the database with the same name, it deletes the table along with data and creates a new table.
-
update − If the value is ”update”, then Hibernate first validates whether the table is present in the database. If present, it alters that table as per the changes. If not, it creates a new one.
-
validate − If the value is ”validate”, then Hibernate only verifies whether the table is present. If the table does not exist then it throws an exception.
-
create-drop − If the value is ”create-drop”, then Hibernate creates a new table when SessionFactory is created, performs required operations, and deletes the table when SessionFactory is destroyed. This value is used for testing hibernate code.
-
none − It does not make any changes to the schema.
Hibernate Dialect
A database dialect is a configuration option that allows software to translate general SQL statements into vendor-specific DDL and DML. Different database products, such as PostgreSQL, MySQL, Oracle, and SQL Server, have their own variant of SQL, which are called SQL dialects.
Following is the list of various important databases dialect property type −
Sr.No. | Database & Dialect Property |
---|---|
1 |
Caché 2007.1 org.hibernate.dialect.Cache71Dialect |
2 |
DB2 org.hibernate.dialect.DB2Dialect |
3 |
DB2/390 org.hibernate.dialect.DB2390Dialect |
4 |
DB2/400 org.hibernate.dialect.DB2400Dialect |
5 |
Cloudscape 10 – aka Derby. org.hibernate.dialect.DerbyDialect |
6 |
Firebird org.hibernate.dialect.FirebirdDialect |
7 |
FrontBase org.hibernate.dialect.FrontBaseDialect |
8 |
H2 org.hibernate.dialect.H2Dialect |
9 |
HSQLDB(HyperSQL) org.hibernate.dialect.HSQLDialect |
10 |
Informix org.hibernate.dialect.InformixDialect |
11 |
Ingres 9.2 org.hibernate.dialect.IngresDialect |
12 |
Ingres 9.3 and later org.hibernate.dialect.Ingres9Dialect |
13 |
Ingres 10 and later org.hibernate.dialect.Ingres10Dialect |
14 |
Interbase org.hibernate.dialect.InterbaseDialect |
15 |
Microsoft SQL Server 2000 org.hibernate.dialect.SQLServerDialect |
16 |
Microsoft SQL Server 2005 org.hibernate.dialect.SQLServerDialect |
17 |
Microsoft SQL Server 2008 org.hibernate.dialect.SQLServer2008Dialect |
18 |
MySQL (prior to 5.x) org.hibernate.dialect.MySQLDialect |
19 |
MySQL 5.x org.hibernate.dialect.MySQL5Dialect |
20 |
Oracle 8i org.hibernate.dialect.Oracle8iDialect
|
21 |
Oracle 9i org.hibernate.dialect.Oracle9iDialect
|
22 |
Oracle 10g org.hibernate.dialect.Oracle10gDialect
|
23 |
Oracle 11g org.hibernate.dialect.Oracle10gDialect
|
24 |
Pointbase org.hibernate.dialect.PointbaseDialect
|
25 |
PostgreSQL org.hibernate.dialect.PostgreSQLDialect
|
26 |
PostgreSQL Plus org.hibernate.dialect.PostgrePlusDialect
|
27 |
Progress org.hibernate.dialect.ProgressDialect |
28 |
Unisys 2200 Relational Database (RDMS) org.hibernate.dialect.RDMSOS2200Dialect |
29 |
SAP DB org.hibernate.dialect.SAPDBDialect |
30 |
Sybase 11.9.2
org.hibernate.dialect.Sybase11Dialect
|
31 |
Sybase Anywhere
org.hibernate.dialect.SybaseAnywhereDialect
|
32 |
Sybase Adaptive Server Enterprise (ASE) 15
org.hibernate.dialect.SybaseASE15Dialect
|
33 |
Teradata
org.hibernate.dialect.TeradataDialect
|
34 |
TimesTen 5.1
org.hibernate.dialect.TimesTenDialect
|
”;