The Hibernate configuration can be specified in the Hibernate Configuration File (cfg.xml), which has more configuration parameters than the properties file.
The Hibernate XML configuration file has the following advantages over the properties file:
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:
The Create Hibernate Configuration file wizard gets started.
jboss-hibernate
| src
| main
| resources
folder, specify File name as hibernate.cfg.xml
, and click on Next
, as shown in the following screenshot: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.MySQL 5 (InnoDB)
. Select Driver class as com.mysql.jdbc.Driver
.jdbc:mysql://localhost:3306/test
.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: