Hibernate provides two alternative configuration files: a standard Java properties file called hibernate.properties and an XML formatted file called hibernate.cfg.xml. It is important to realize that both configuration files perform the same function: configuring the Hibernate service. If both the hibernate.properties and hibernate.cfg.xml files are found in the application classpath, then hibernate.cfg.xml overrides the settings found in the hibernate.properties file.
Before configuring Hibernate, you should first determine how the service obtains database connections. Database connections may be provided by the Hibernate framework or from a JNDI DataSource. A third method, user-provided JDBC connections, is also available, but it is rarely used. The sample configuration file in listing below uses Hibernate-managed JDBC connections.
uid
pwd
jdbc:mysql://localhost/db
com.mysql.jdbc.Driver
org.hibernate.dialect.MySQLDialect
To use Hibernate-provided JDBC connections, the configuration file
requires the following five properties:
connection.driver_class—The JDBC connection class for thespecific database
connection.url—The full JDBC URL to the database
connection.username—The username used to connect to the database
connection.password—The password used to authenticate the username
dialect—The name of the SQL dialect for the database
The connection properties are common to any Java developer who has worked with JDBC in the past. Since you’re not specifying a connection pool, which we cover later in this chapter, Hibernate uses its own rudimentary connection-pooling mechanism. The internal pool is fine for basic testing, but you shouldn’t use it in production.
The dialect property tells Hibernate which SQL dialect to use for certain operations. Although not strictly required, it should be used to ensure Hibernate Query Language (HQL) statements are correctly converted into the proper SQL dialect for the underlying database. The dialect property tells the framework whether the given database supports identity columns, altering relational tables, and unique indexes, among other database-specific details. Hibernate ships with more than 20 SQL dialects, supporting each of the major database vendors, including Oracle, DB2, MySQL, and PostgreSQL. Hibernate also needs to know the location and names of the mapping files describing the persistent classes. The mapping element provides the name of each mapping file as well as its location relative to the application classpath.