3. Bootstrap
The term bootstrapping refers to initializing and starting a software component. In Hibernate, we are specifically talking about the process of building a fully functional SessionFactory
instance or EntityManagerFactory
instance, for JPA. The process is very different for each.
During the bootstrap process, you might want to customize Hibernate behavior so make sure you check the Configurations section as well. |
3.1. Native Bootstrapping
This section discusses the process of bootstrapping a Hibernate SessionFactory
. Specifically, it addresses the bootstrapping APIs as redesigned in 5.0. For a discussion of the legacy bootstrapping API, see Legacy Bootstrapping.
3.1.1. Building the ServiceRegistry
The first step in native bootstrapping is the building of a ServiceRegistry
holding the services Hibernate will need during bootstrapping and at run time.
Actually, we are concerned with building 2 different ServiceRegistries. First is the org.hibernate.boot.registry.BootstrapServiceRegistry
. The BootstrapServiceRegistry
is intended to hold services that Hibernate needs at both bootstrap and run time. This boils down to 3 services:
org.hibernate.boot.registry.classloading.spi.ClassLoaderService
which controls how Hibernate interacts with ClassLoader
s.
org.hibernate.integrator.spi.IntegratorService
which controls the management and discovery of org.hibernate.integrator.spi.Integrator
instances.
org.hibernate.boot.registry.selector.spi.StrategySelector
which controls how Hibernate resolves implementations of various strategy contracts. This is a very powerful service, but a full discussion of it is beyond the scope of this guide.
If you are ok with the default behavior of Hibernate in regards to these |
If you wish to alter how the BootstrapServiceRegistry
is built, that is controlled through the org.hibernate.boot.registry.BootstrapServiceRegistryBuilder
:
Example 266. Controlling BootstrapServiceRegistry
building
BootstrapServiceRegistryBuilder bootstrapRegistryBuilder =
new BootstrapServiceRegistryBuilder();
// add a custom ClassLoader
bootstrapRegistryBuilder.applyClassLoader( customClassLoader );
// manually add an Integrator
bootstrapRegistryBuilder.applyIntegrator( customIntegrator );
BootstrapServiceRegistry bootstrapRegistry = bootstrapRegistryBuilder.build();
The services of the |
The second ServiceRegistry is the org.hibernate.boot.registry.StandardServiceRegistry
. You will almost always need to configure the StandardServiceRegistry
, which is done through org.hibernate.boot.registry.StandardServiceRegistryBuilder
:
Example 267. Building a BootstrapServiceRegistryBuilder
// An example using an implicitly built BootstrapServiceRegistry
StandardServiceRegistryBuilder standardRegistryBuilder =
new StandardServiceRegistryBuilder();
// An example using an explicitly built BootstrapServiceRegistry
BootstrapServiceRegistry bootstrapRegistry =
new BootstrapServiceRegistryBuilder().build();
StandardServiceRegistryBuilder standardRegistryBuilder =
new StandardServiceRegistryBuilder( bootstrapRegistry );
A StandardServiceRegistry
is also highly configurable via the StandardServiceRegistryBuilder API. See the StandardServiceRegistryBuilder
Javadocs for more details.
Some specific methods of interest:
Example 268. Configuring a MetadataSources
ServiceRegistry standardRegistry =
new StandardServiceRegistryBuilder().build();
MetadataSources sources = new MetadataSources( standardRegistry );
// alternatively, we can build the MetadataSources without passing
// a service registry, in which case it will build a default
// BootstrapServiceRegistry to use. But the approach shown
// above is preferred
// MetadataSources sources = new MetadataSources();
// add a class using JPA/Hibernate annotations for mapping
sources.addAnnotatedClass( MyEntity.class );
// add the name of a class using JPA/Hibernate annotations for mapping.
// differs from above in that accessing the Class is deferred which is
// important if using runtime bytecode-enhancement
sources.addAnnotatedClassName( "org.hibernate.example.Customer" );
// Read package-level metadata.
sources.addPackage( "hibernate.example" );
// Read package-level metadata.
sources.addPackage( MyEntity.class.getPackage() );
// Adds the named hbm.xml resource as a source: which performs the
// classpath lookup and parses the XML
sources.addResource( "org/hibernate/example/Order.hbm.xml" );
// Adds the named JPA orm.xml resource as a source: which performs the
// classpath lookup and parses the XML
sources.addResource( "org/hibernate/example/Product.orm.xml" );
// Read all mapping documents from a directory tree.
// Assumes that any file named *.hbm.xml is a mapping document.
sources.addDirectory( new File( ".") );
// Read mappings from a particular XML file
sources.addFile( new File( "./mapping.xml") );
// Read all mappings from a jar file.
// Assumes that any file named *.hbm.xml is a mapping document.
sources.addJar( new File( "./entities.jar") );
// Read a mapping as an application resource using the convention that a class named foo.bar.MyEntity is
// mapped by a file named foo/bar/MyEntity.hbm.xml which can be resolved as a classpath resource.
sources.addClass( MyEntity.class );
3.1.2. Event Listener registration
The main use cases for an org.hibernate.integrator.spi.Integrator
right now are registering event listeners and providing services (see org.hibernate.integrator.spi.ServiceContributingIntegrator
). With 5.0 we plan on expanding that to allow altering the metamodel describing the mapping between object and relational models.
Example 269. Configuring an event listener
public class MyIntegrator implements org.hibernate.integrator.spi.Integrator {
@Override
public void integrate(
Metadata metadata,
SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) {
// As you might expect, an EventListenerRegistry is the thing with which event
// listeners are registered
// It is a service so we look it up using the service registry
final EventListenerRegistry eventListenerRegistry =
serviceRegistry.getService( EventListenerRegistry.class );
// If you wish to have custom determination and handling of "duplicate" listeners,
// you would have to add an implementation of the
// org.hibernate.event.service.spi.DuplicationStrategy contract like this
eventListenerRegistry.addDuplicationStrategy( new CustomDuplicationStrategy() );
// EventListenerRegistry defines 3 ways to register listeners:
// 1) This form overrides any existing registrations with
eventListenerRegistry.setListeners( EventType.AUTO_FLUSH,
DefaultAutoFlushEventListener.class );
// 2) This form adds the specified listener(s) to the beginning of the listener chain
eventListenerRegistry.prependListeners( EventType.PERSIST,
DefaultPersistEventListener.class );
// 3) This form adds the specified listener(s) to the end of the listener chain
eventListenerRegistry.appendListeners( EventType.MERGE,
DefaultMergeEventListener.class );
}
@Override
public void disintegrate(
SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) {
}
}
3.1.3. Building the Metadata
The second step in native bootstrapping is the building of an org.hibernate.boot.Metadata
object containing the parsed representations of an application domain model and its mapping to a database. The first thing we obviously need to build a parsed representation is the source information to be parsed (annotated classes, hbm.xml
files, orm.xml
files). This is the purpose of org.hibernate.boot.MetadataSources
.
MetadataSources
has many other methods as well. Explore its API and Javadocs for more information. Also, all methods on MetadataSources
offer fluent-style call chaining::
Example 270. Configuring a MetadataSources
with method chaining
ServiceRegistry standardRegistry =
new StandardServiceRegistryBuilder().build();
MetadataSources sources = new MetadataSources( standardRegistry )
.addAnnotatedClass( MyEntity.class )
.addAnnotatedClassName( "org.hibernate.example.Customer" )
.addResource( "org/hibernate/example/Order.hbm.xml" )
.addResource( "org/hibernate/example/Product.orm.xml" );
Once we have the sources of mapping information defined, we need to build the Metadata
object. If you are ok with the default behavior in building the Metadata then you can simply call the buildMetadata
method of the MetadataSources
.
Notice that a |
However, if you wish to adjust the process of building Metadata
from MetadataSources
, you will need to use the MetadataBuilder
as obtained via MetadataSources#getMetadataBuilder
. MetadataBuilder
allows a lot of control over the Metadata
building process. See its Javadocs for full details.
Example 271. Building Metadata via MetadataBuilder
ServiceRegistry standardRegistry =
new StandardServiceRegistryBuilder().build();
MetadataSources sources = new MetadataSources( standardRegistry );
MetadataBuilder metadataBuilder = sources.getMetadataBuilder();
// Use the JPA-compliant implicit naming strategy
metadataBuilder.applyImplicitNamingStrategy(
ImplicitNamingStrategyJpaCompliantImpl.INSTANCE );
// specify the schema name to use for tables, etc when none is explicitly specified
metadataBuilder.applyImplicitSchemaName( "my_default_schema" );
// specify a custom Attribute Converter
metadataBuilder.applyAttributeConverter( myAttributeConverter );
Metadata metadata = metadataBuilder.build();
3.1.4. Building the SessionFactory
The final step in native bootstrapping is to build the SessionFactory
itself. Much like discussed above, if you are ok with the default behavior of building a SessionFactory
from a Metadata
reference, you can simply call the buildSessionFactory
method on the Metadata
object.
However, if you would like to adjust that building process, you will need to use SessionFactoryBuilder
as obtained via Metadata#getSessionFactoryBuilder
. Again, see its Javadocs for more details.
Example 272. Native Bootstrapping - Putting it all together
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
.configure( "org/hibernate/example/hibernate.cfg.xml" )
.build();
Metadata metadata = new MetadataSources( standardRegistry )
.addAnnotatedClass( MyEntity.class )
.addAnnotatedClassName( "org.hibernate.example.Customer" )
.addResource( "org/hibernate/example/Order.hbm.xml" )
.addResource( "org/hibernate/example/Product.orm.xml" )
.getMetadataBuilder()
.applyImplicitNamingStrategy( ImplicitNamingStrategyJpaCompliantImpl.INSTANCE )
.build();
SessionFactory sessionFactory = metadata.getSessionFactoryBuilder()
.applyBeanManager( getBeanManager() )
.build();
The bootstrapping API is quite flexible, but in most cases it makes the most sense to think of it as a 3 step process:
Build the
StandardServiceRegistry
Build the
Metadata
Use those 2 to build the
SessionFactory
Example 273. Building SessionFactory
via SessionFactoryBuilder
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
.configure( "org/hibernate/example/hibernate.cfg.xml" )
.build();
Metadata metadata = new MetadataSources( standardRegistry )
.addAnnotatedClass( MyEntity.class )
.addAnnotatedClassName( "org.hibernate.example.Customer" )
.addResource( "org/hibernate/example/Order.hbm.xml" )
.addResource( "org/hibernate/example/Product.orm.xml" )
.getMetadataBuilder()
.applyImplicitNamingStrategy( ImplicitNamingStrategyJpaCompliantImpl.INSTANCE )
.build();
SessionFactoryBuilder sessionFactoryBuilder = metadata.getSessionFactoryBuilder();
// Supply a SessionFactory-level Interceptor
sessionFactoryBuilder.applyInterceptor( new CustomSessionFactoryInterceptor() );
// Add a custom observer
sessionFactoryBuilder.addSessionFactoryObservers( new CustomSessionFactoryObserver() );
// Apply a CDI BeanManager ( for JPA event listeners )
sessionFactoryBuilder.applyBeanManager( getBeanManager() );
SessionFactory sessionFactory = sessionFactoryBuilder.build();
3.2. JPA Bootstrapping
Bootstrapping Hibernate as a JPA provider can be done in a JPA-spec compliant manner or using a proprietary bootstrapping approach. The standardized approach has some limitations in certain environments, but aside from those, it is highly recommended that you use JPA-standardized bootstrapping.
3.2.1. JPA-compliant bootstrapping
In JPA, we are ultimately interested in bootstrapping a javax.persistence.EntityManagerFactory
instance. The JPA specification defines two primary standardized bootstrap approaches depending on how the application intends to access the javax.persistence.EntityManager
instances from an EntityManagerFactory
.
It uses the terms EE and SE for these two approaches, but those terms are very misleading in this context. What the JPA spec calls EE bootstrapping implies the existence of a container (EE, OSGi, etc), who’ll manage and inject the persistence context on behalf of the application. What it calls SE bootstrapping is everything else. We will use the terms container-bootstrapping and application-bootstrapping in this guide.
For compliant container-bootstrapping, the container will build an EntityManagerFactory
for each persistent-unit defined in the META-INF/persistence.xml
configuration file and make that available to the application for injection via the javax.persistence.PersistenceUnit
annotation or via JNDI lookup.
Example 274. Injecting the default EntityManagerFactory
@PersistenceUnit
private EntityManagerFactory emf;
Or, in case you have multiple Persistence Units (e.g. multiple persistence.xml
configuration files), you can inject a specific EntityManagerFactory
by Unit name:
Example 275. Injecting a specific EntityManagerFactory
@PersistenceUnit(
unitName = "CRM"
)
private EntityManagerFactory entityManagerFactory;
The META-INF/persistence.xml
file looks as follows:
Example 276. META-INF/persistence.xml configuration file
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="CRM">
<description>
Persistence unit for Hibernate User Guide
</description>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>org.hibernate.documentation.userguide.Document</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE" />
<property name="javax.persistence.jdbc.user"
value="sa" />
<property name="javax.persistence.jdbc.password"
value="" />
<property name="hibernate.show_sql"
value="true" />
<property name="hibernate.hbm2ddl.auto"
value="update" />
</properties>
</persistence-unit>
</persistence>
For compliant application-bootstrapping, rather than the container building the EntityManagerFactory
for the application, the application builds the EntityManagerFactory
itself using the javax.persistence.Persistence
bootstrap class. The application creates an EntityManagerFactory
by calling the createEntityManagerFactory
method:
Example 277. Application bootstrapped EntityManagerFactory
// Create an EMF for our CRM persistence-unit.
EntityManagerFactory emf = Persistence.createEntityManagerFactory( "CRM" );
If you don’t want to provide a |
To inject the default Persistence Context, you can use the @PersistenceContext
annotation.
Example 278. Inject the default EntityManager
@PersistenceContext
private EntityManager em;
To inject a specific Persistence Context, you can use the @PersistenceContext
annotation, and you can even pass EntityManager
-specific properties using the @PersistenceProperty
annotation.
Example 279. Inject a configurable EntityManager
@PersistenceContext(
unitName = "CRM",
properties = {
@PersistenceProperty(
name="org.hibernate.flushMode",
value= "MANUAL"
)
}
)
private EntityManager entityManager;
If you would like additional details on accessing and using |
3.2.2. Externalizing XML mapping files
JPA offers two mapping options:
annotations
XML mappings
Although annotations are much more common, there are projects where XML mappings are preferred. You can even mix annotations and XML mappings so that you can override annotation mappings with XML configurations that can be easily changed without recompiling the project source code. This is possible because if there are two conflicting mappings, the XML mappings take precedence over its annotation counterpart.
The JPA specification requires the XML mappings to be located on the classpath:
An object/relational mapping XML file named
orm.xml
may be specified in theMETA-INF
directory in the root of the persistence unit or in theMETA-INF
directory of any jar file referenced by thepersistence.xml
.Alternatively, or in addition, one or more mapping files may be referenced by the mapping-file elements of the persistence-unit element. These mapping files may be present anywhere on the classpath.
— Section 8.2.1.6.2 of the JPA 2.1 Specification
Therefore, the mapping files can reside in the application jar artifacts, or they can be stored in an external folder location with the cogitation that that location be included in the classpath.
Hibernate is more lenient in this regard so you can use any external location even outside of the application configured classpath.
Example 280. META-INF/persistence.xml configuration file for external XML mappings
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="CRM">
<description>
Persistence unit for Hibernate User Guide
</description>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<mapping-file>file:///etc/opt/app/mappings/orm.xml</mapping-file>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE" />
<property name="javax.persistence.jdbc.user"
value="sa" />
<property name="javax.persistence.jdbc.password"
value="" />
<property name="hibernate.show_sql"
value="true" />
<property name="hibernate.hbm2ddl.auto"
value="update" />
</properties>
</persistence-unit>
</persistence>
In the persistence.xml
configuration file above, the orm.xml
XML file containing all JPA entity mappings is located in the /etc/opt/app/mappings/
folder.
3.2.3. Configuring the SessionFactory
Metadata
via the JPA bootstrap
As previously seen, the Hibernate native bootstrap mechanism allows you to customize a great variety of configurations which are passed via the Metadata
object.
When using Hibernate as a JPA provider, the EntityManagerFactory
is backed by a SessionFactory
. For this reason, you might still want to use the Metadata
object to pass various settings which cannot be supplied via the standard Hibernate configuration settings.
For this reason, you can use the MetadataBuilderContributor
class as you can see in the following examples.
Example 281. Implementing a MetadataBuilderContributor
public class SqlFunctionMetadataBuilderContributor
implements MetadataBuilderContributor {
@Override
public void contribute(MetadataBuilder metadataBuilder) {
metadataBuilder.applySqlFunction(
"instr", new StandardSQLFunction( "instr", StandardBasicTypes.STRING )
);
}
}
The above MetadataBuilderContributor
is used to register a SqlFuction
which is not defined by the currently running Hibernate Dialect
, but which we need to reference in our JPQL queries.
By having access to the MetadataBuilder
class that’s used by the underlying SessionFactory
, the JPA bootstrap becomes just as flexible as the Hibernate native bootstrap mechanism.
You can then pass the custom MetadataBuilderContributor
via the hibernate.metadata_builder_contributor
configuration property as explained in the Configuration chapter.