This is an example of a typical Hibernate startup procedure, in one line of code,
using automatic configuration file detection:
SessionFactory sessionFactory =
new Configuration().configure().buildSessionFactory();
Wait—how did Hibernate know where the configuration file was located and
which one to load?
When new Configuration() is called, Hibernate searches for a file named
hibernate.properties in the root of the classpath. If it’s found, all hibernate.*
properties are loaded and added to the Configuration object.
When configure() is called, Hibernate searches for a file named hibernate.
cfg.xml in the root of the classpath, and an exception is thrown if it can’t
be found. You don’t have to call this method if you don’t have this configuration
file, of course. If settings in the XML configuration file are duplicates of properties
set earlier, the XML settings override the previous ones.
--
Hibernate logs all interesting events through Apache commons-logging, a thin
abstraction layer that directs output to either Apache Log4j (if you put log4j.jar in
your classpath) or JDK 1.4 logging (if you’re running under JDK 1.4 or above and
Log4j isn’t present). We recommend Log4j because it’s more mature, more popular,
and under more active development.
using automatic configuration file detection:
SessionFactory sessionFactory =
new Configuration().configure().buildSessionFactory();
Wait—how did Hibernate know where the configuration file was located and
which one to load?
When new Configuration() is called, Hibernate searches for a file named
hibernate.properties in the root of the classpath. If it’s found, all hibernate.*
properties are loaded and added to the Configuration object.
When configure() is called, Hibernate searches for a file named hibernate.
cfg.xml in the root of the classpath, and an exception is thrown if it can’t
be found. You don’t have to call this method if you don’t have this configuration
file, of course. If settings in the XML configuration file are duplicates of properties
set earlier, the XML settings override the previous ones.
--
Hibernate logs all interesting events through Apache commons-logging, a thin
abstraction layer that directs output to either Apache Log4j (if you put log4j.jar in
your classpath) or JDK 1.4 logging (if you’re running under JDK 1.4 or above and
Log4j isn’t present). We recommend Log4j because it’s more mature, more popular,
and under more active development.