Creating a Hibernate configuration file

The Hibernate configuration can be specified in the Hibernate Configuration File (cfg.xml), which has more configuration parameters than the properties file.

Note

Either the properties file or the configuration file can be used to specify the configuration, or both can be used. If both are provided, the configuration file overrides the properties file for the configuration parameters specified in both.

The Hibernate XML configuration file has the following advantages over the properties file:

  • The Hibernate configuration file is more convenient when tuning the Hibernate cache. The Hibernate configuration file has the provision to configure the Hibernate XML Mapping files.
  • For exporting a schema to a database using the SchemaExport tool, just the properties file would suffice, but for object/relational mapping of a persistence class, the Hibernate XML configuration file is a better option.

The following are the steps to create a Hibernate configuration file:

  1. Select File | New | Other. In New, select Hibernate | Hibernate Configuration File (cfg.xml) and click on Next, as shown in the following screenshot:
    Creating a Hibernate configuration file

    The Create Hibernate Configuration file wizard gets started.

  2. Select the jboss-hibernate | src | main | resources folder, specify File name as hibernate.cfg.xml, and click on Next, as shown in the following screenshot:
    Creating a Hibernate configuration file
  3. In the Hibernate Configuration File wizard, specify Session factory name (HibernateSessionFactory). A session factory is a factory that is used to generate client sessions to Hibernate. A session factory stores the metadata for the object/relational mapping.
  4. Select Database dialect as MySQL 5 (InnoDB). Select Driver class as com.mysql.jdbc.Driver.
  5. Specify Connection URL as jdbc:mysql://localhost:3306/test.
  6. Specify the Username and Password and click on Finish, as shown in the following screenshot:
    Creating a Hibernate configuration file

Hibernate provides transaction-level caching of persistence data in a session by default. Hibernate has the provision for a query-level cache, which is turned off by default, to frequently run queries. Hibernate also has the provision for a second-level cache on the SessionFactory level or on the cluster level. The second-level cache is configured in the hibernate.cfg.xml file using the hibernate.cache.provider_class property. Classes that specify <cache/> mapping have the second-level cache enabled by default. The second-level cache can be turned off by setting the cache.provider_class property to org.hibernate.cache.NoCacheProvider. Specify the Hibernate XML Mapping file using the <mapping/> element with the resource attribute set to catalog.hbm.xml.

The hibernate.cfg.xml file is listed in the following code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory name="HibernateSessionFactory">
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">mysql16</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <mapping resource="catalog.hbm.xml" />
  </session-factory>
</hibernate-configuration>

The hibernate.cfg.xml file is shown in the jboss-hibernate project folder, as follows:

Creating a Hibernate configuration file
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset