Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
2.3.7. Startup and Helpers
It is time to load and store some
Event
objects, but first you have to complete the setup with some infrastructure code. You have to startup Hibernate by building a global org.hibernate.SessionFactory
object and storing it somewhere for easy access in application code. A org.hibernate.SessionFactory
is used to obtain org.hibernate.Session
instances. A org.hibernate.Session
represents a single-threaded unit of work. The org.hibernate.SessionFactory
is a thread-safe global object that is instantiated once.
We will create a
HibernateUtil
helper class that takes care of startup and makes accessing the org.hibernate.SessionFactory
more convenient.
Save this code as
src/main/java/org/hibernate/tutorial/util/HibernateUtil.java
This class not only produces the global
org.hibernate.SessionFactory
reference in its static initializer; it also hides the fact that it uses a static singleton. We might just as well have looked up the org.hibernate.SessionFactory
reference from JNDI in an application server or any other location for that matter.
If you give the
org.hibernate.SessionFactory
a name in your configuration, Hibernate will try to bind it to JNDI under that name after it has been built. Another, better option is to use a JMX deployment and let the JMX-capable container instantiate and bind a HibernateService
to JNDI. Such advanced options are discussed later.
You now need to configure a logging system. Hibernate uses commons logging and provides two choices: Log4j and JDK 1.4 logging. Most developers prefer Log4j: copy
log4j.properties
from the Hibernate distribution in the etc/
directory to your src
directory, next to hibernate.cfg.xml
. If you prefer to have more verbose output than that provided in the example configuration, you can change the settings. By default, only the Hibernate startup message is shown on stdout.
The tutorial infrastructure is complete and you are now ready to do some real work with Hibernate.