Native Hibernate configuration


Native Hibernate configuration

Although basic (and extensive) configuration is standardized in JPA, you can’t access all the configuration features of Hibernate with properties in persistence.xml. Note that most applications, even quite sophisticated ones, don’t need such special configuration options and hence don’t have to access the bootstrap API we show in this section. If you aren’t sure, you can skip this section and come back to it later, when you need to extend Hibernate type adapters, add custom SQL functions, and so on.
The native equivalent of the standard JPA EntityManagerFactory is the org.hibernate.SessionFactory. You have usually one per application, and it’s the same pairing of class mappings with database connection configuration.
Hibernate’s native bootstrap API is split into several stages, each giving you access to certain configuration aspects. In its most compact form, building a Session-Factory looks like this:
SessionFactory sessionFactory = new MetadataSourcesnew StandardServiceRegistryBuilder()
.configure("hibernate.cfg.xml").build()).buildMetadata().buildSessionFactoryO;
This loads all settings from a Hibernate configuration file. If you have an existing Hibernate project, you most likely have this file on your classpath. Similar to persistence.xml,
this configuration file contains database connection details, as well as a list of persistent classes and other configuration properties.
Let’s deconstruct this bootstrap snippet and look at the API in more detail. First, create a ServiceRegistry:

1 - This builder helps you create the immutable service registry with chained method calls. 
2 -  Configure the services registry by applying settings.
If you want to externalize your service registry configuration, you can load settings from a properties file on the classpath with StandardServiceRegistryBuilder#load-Properties(file).
With the ServiceRegistry built and immutable, you can move on to the next stage: telling Hibernate which persistent classes are part of your mapping metadata. Configure the metadata sources as follows:


1 -  This builder helps you create the immutable service registry with chained method calls. 
2 -  Configure the services registry by applying settings.
The MetadataSources API has many methods for adding mapping sources; check the Javadoc for more information. The next stage of the boot procedure is building all the metadata needed by Hibernate, with the MetadataBuilder you obtained from the metadata sources.
You can then query the metadata to interact with Hibernate’s completed configuration programmatically, or continue and build the final SessionFactory:

Creating an EntityManagerFactory from a SessionFactory
At the time of writing, Hibernate has no convenient API to build an EntityManagerFactory programmatically. You can use an internal API for this purpose: the org.hibernate.jpa.internal.EntityManagerFactoryImpl has a constructor that accepts a SessionFactory.
Let’s see if this configuration works by storing and loading a message with Hiber-nate’s native equivalent of EntityManager, org.hibernate.Session. You can create a Session with the SessionFactory, and you must close itjust as you have to close your own EntityManager.
Or, using another Hibernate feature, you can let Hibernate take care of creating and closing the Session with SessionFactory#getCurrentSession( ) :

1 -  Get access to the standard transaction API UserTransaction, and begin a transaction on this thread of execution.
2 - Whenever you call getCurrentSession() in the same thread, you get the same org.hibernate.Session. It’s bound automatically to the ongoing transaction and is closed for you automatically when that transaction commits or rolls back.

3 - The native Hibernate API is very similar to the standard Java Persistence API, and most methods have the same names.

4 - Hibernate synchronizes the session with the database and automatically closes the “current” session on commit of the bound transaction.
Accessing the current Session results in compact code:

1 -  A Hibernate criteria query is a type-safe programmatic way to express queries, automatically translated into SQL.
Most of the examples in this article don’t use the SessionFactory or Session API. From time to time, when a particular feature is only available in Hibernate, we show you how to unwrap() the native interface given a standard API.

Comments

Popular posts from this blog

The example CaveatEmptor application

Hibernate, Mapping entities with identity