Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Hibernate configuration


May 17, 2021 Hibernate


Table of contents


Configuration

Hibernate needs to know in advance where to find mapping information that defines how Java classes are associated with database tables. H ibernate also requires a set of configuration settings for the relevant database and other related parameters. A ll of this information is usually provided as a standard Java property file called hibernate.properties. Or it's available as an XML file called hibernate .cfg.xml.

We'll consider hibernate .cfg.xml this XML format file to decide to specify the Hibernate application properties that you need in my case. M ost of the properties in this XML file do not need to be modified. This file is saved in the root of the application's class path.

Hibernate property

Below is an important list of properties that you may need in the table to configure the database individually.

S.N. Properties and descriptions
1 hibernate.dialect
This property enables the Hibernate app to generate the appropriate SQL for the selected database.
2 hibernate.connection.driver_class
JDBC driver class.
3 hibernate.connection.url
The JDBC URL of the DB instance.
4 hibernate.connection.username
The database user name.
5 hibernate.connection.password
The database password.
6 hibernate.connection.pool_size
Limit the number of connections in the Hibernate app database connection pool.
7 hibernate.connection.autocommit
Allows automatic submission mode to be used in JDBC connections.

If you are using JNDI and database application servers then you must configure the following properties:

S.N. Properties and descriptions
1 hibernate.connection.datasource
The application JNDI name that you are using in the application server environment.
2 hibernate.jndi.class
JNDI's InitialContext class.
3 hibernate.jndi.<JNDIpropertyname>
In JNDI's InitialContext class, pass any Java naming and directory interface properties you want.
4 hibernate.jndi.url
Provides a URL for JNDI.
5 hibernate.connection.username
The database user name.
6 hibernate.connection.password
The database password.

Hibernate and MySQL databases

The MySQL database is one of the most popular databases available in open source database systems. W e're going to create .cfg.xml profile and place it in the root of the application's CLASSPATH. You want to make sure that the testdb database is available in your MySQL database, and that you want to have a user test that you can use to access the database.

XML profiles must comply with Hibernate 3 Configuration DTD, http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd . This URL can be found.

<?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>

   <!-- Assume test is the database name -->
   <property name="hibernate.connection.url">
      jdbc:mysql://localhost/test
   </property>
   <property name="hibernate.connection.username">
      root
   </property>
   <property name="hibernate.connection.password">
      root123
   </property>

   <!-- List of XML mapping files -->
   <mapping resource="Employee.hbm.xml"/>

</session-factory>
</hibernate-configuration> 

The profile above contains the hashtags associated with the hibernate-mapping file, and we'll look at what the hibernate mapping file is in the next chapter and find out why and how to use it. The following is a list of the various important database isodes property types:

Database Dialect properties
DB2 org.hibernate.dialect.DB2Dialect
Hsqldb org.hibernate.dialect.HSQLDialect
HypersonicSQL org.hibernate.dialect.HSQLDialect
Informix org.hibernate.dialect.InformixDialect
Ingres org.hibernate.dialect.IngresDialect
Interbase org.hibernate.dialect.InterbaseDialect
Microsoft SQL Server 2000 org.hibernate.dialect.SQLServerDialect
Microsoft SQL Server 2005 org.hibernate.dialect.SQLServer2005Dialect
Microsoft SQL Server 2008 org.hibernate.dialect.SQLServer2008Dialect
Mysql org.hibernate.dialect.MySQLDialect
Oracle (any version) org.hibernate.dialect.OracleDialect
Oracle 11g org.hibernate.dialect.Oracle10gDialect
Oracle 10g org.hibernate.dialect.Oracle10gDialect
Oracle 9i org.hibernate.dialect.Oracle9iDialect
PostgreSQL org.hibernate.dialect.PostgreSQLDialect
Progress org.hibernate.dialect.ProgressDialect
SAP DB org.hibernate.dialect.SAPDBDialect
Sybase org.hibernate.dialect.SybaseDialect
Sybase Anywhere org.hibernate.dialect.SybaseAnywhereDialec