Java Reference
In-Depth Information
Creating a Hibernate Configuration File
There are several ways that Hibernate can be given all of the information that it needs to connect
to the database and determine its mappings. For our Message example, we used the configura-
tion file hibernate.cfg.xml placed in our project's src directory and given in Listing 3-4.
Listing 3-4. The Message Application's Mapping File
<?xml version='1.0' encoding='utf-8'?>
<session-factory>
<property name="hibernate.connection.url">
jdbc:hsqldb:file:testdb;shutdown=true
</property>
<property name="hibernate.connection.driver_class">
org.hsqldb.jdbcDriver
</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">0</property>
<property name="hibernate.dialect">
org.hibernate.dialect.HSQLDialect
</property>
<property name="hibernate.show_sql">false</property>
<!-- "Import" the mapping resources here -->
<mapping resource="sample/entity/Message.hbm.xml"/>
</session-factory>
</hibernate-configuration>
The various database-related fields ( hibernate.connection.* ) should look pretty familiar
from setting up JDBC connections, with the exception of the hibernate.connection.pool
property, which is used to disable a feature (connection pooling) that causes problems when
using the HSQL database. The show_sql value, set to false in our example, is extremely useful
when debugging problems with your programs—when set to true , all of the SQL prepared by
Hibernate is logged to the standard output stream (i.e., the console).
The SQL dialects, discussed in Chapter 2, allow you to select the database type that Hiber-
nate will be talking to. You must select a dialect, even if it is GenericDialect —most database
platforms accept a common subset of SQL, but there are inconsistencies and extensions spe-
cific to each. Hibernate uses the dialect class to determine the appropriate SQL to use when
creating and querying the database. If you elect to use GenericDialect , then Hibernate will
only be able to use a common subset of SQL to perform its operations, and will be unable to
take advantage of various database-specific features to improve performance.
n Caution Hibernate looks in the classpath for the configuration file. If you place it anywhere else,
Hibernate will complain that you haven't provided necessary configuration details.
Search WWH ::




Custom Search