Questo contenuto non è disponibile nella lingua selezionata.
4.7. XML configuration file
An alternative approach to configuration is to specify a full configuration in a file named
hibernate.cfg.xml
. This file can be used as a replacement for the hibernate.properties
file or, if both are present, to override properties.
The XML configuration file is by default expected to be in the root of your
CLASSPATH
. Here is an example:
The advantage of this approach is the externalization of the mapping file names to configuration. The
hibernate.cfg.xml
is also more convenient once you have to tune the Hibernate cache. It is your choice to use either hibernate.properties
or hibernate.cfg.xml
. Both are equivalent, except for the above mentioned benefits of using the XML syntax.
With the XML configuration, starting Hibernate is then as simple as:
SessionFactory sf = new Configuration().configure().buildSessionFactory();
SessionFactory sf = new Configuration().configure().buildSessionFactory();
You can select a different XML configuration file using:
SessionFactory sf = new Configuration() .configure("catdb.cfg.xml") .buildSessionFactory();
SessionFactory sf = new Configuration()
.configure("catdb.cfg.xml")
.buildSessionFactory();