The legacy way to bootstrap a SessionFactory
is via the org.hibernate.cfg.Configuration
object.
Configuration
represents, essentially, a single point for specifying all aspects of building
the SessionFactory
: everything from settings, to mappings, to strategies, etc. I like to think of
Configuration
as a big pot to which we add a bunch of stuff (mappings, settings, etc) and from which
we eventually get a SessionFactory
.
There are some significant draw backs to this approach which led to its deprecation and the development
of the new approach, which is discussed in the Native Bootstrapping guide. Configuration is deprecated but
still available for use, in a limited form that eliminates these draw backs. "Under the covers", Configuration
uses the new bootstrapping code, so the things available there as also available here in terms of
auto-discovery.
|
Usage
You can obtain the Configuration
by instantiating it directly. You then specify mapping metadata (XML
mapping documents, annotated classes) that describe your applications object model and its mapping to a
SQL database.
If XML mapping files are in the classpath, use addResource(). For example:
Configuration cfg = new Configuration()
// addResource does a classpath resource lookup
.addResource("Item.hbm.xml")
.addResource("Bid.hbm.xml")
// calls addResource using "/org/hibernate/auction/User.hbm.xml"
.addClass(org.hibernate.auction.User.class)
// parses Address class for mapping annotations
.addAnnotatedClass( Address.class )
// reads package-level (package-info.class) annotations in the named package
.addPackage( "org.hibernate.auction" )
Configuration
also allows you to specify configuration properties. For example:
Configuration cfg = new Configuration()
.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect")
.setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/test")
.setProperty("hibernate.order_updates", "true");
There are other ways to specify configuration properties, including:
-
Place a file named hibernate.properties in a root directory of the classpath.
-
Place a file named hibernate.properties in a root directory of the classpath.
-
Pass an instance of java.util.Properties to
Configuration#setProperties
. -
Set System properties using java
-Dproperty=value
. -
Include
<property/>
elements inhibernate.cfg.xml
Migration
Mapping Configuration methods to the corresponding methods in the new API..
Mapping metadata
Configuration#addFile
-
MetadataSources#addFile
Configuration#add(XmlDocument)
-
No replacement.
Configuration#addXML
-
No replacement.
Configuration#addCacheableFile
-
MetadataSources#addCacheableFile
Configuration#addURL
-
MetadataSources#addURL
Configuration#addInputStream
-
MetadataSources#addInputStream
Configuration#addResource
-
MetadataSources#addResource
Configuration#addClass
-
MetadataSources#addClass
Configuration#addAnnotatedClass
-
MetadataSources#addAnnotatedClass
Configuration#addPackage
-
MetadataSources#addPackage
Configuration#addJar
-
MetadataSources#addJar
Configuration#addDirectory
-
MetadataSources#addDirectory
Configuration#registerTypeContributor
-
MetadataBuilder#applyTypes
Configuration#registerTypeOverride
-
MetadataBuilder#applyBasicType
Settings
Configuration#setProperty
-
StandardServiceRegistryBuilder#applySetting
Configuration#setProperties
-
No replacement.
Configuration#addProperties
-
StandardServiceRegistryBuilder#applySettings
Configuration#setNamingStrategy
-
No replacement. NamingStrategy split into implicit/physical strategies
Configuration#setImplicitNamingStrategy
-
MetadataBuilder#setImplicitNamingStrategy
Configuration#setPhysicalNamingStrategy
-
MetadataBuilder#setPhysicalNamingStrategy
Configuration#configure
-
StandardServiceRegistryBuilder#configure
Configuration#setInterceptor
-
SessionFactoryBuilder#applyInterceptor
Configuration#setEntityNotFoundDelegate
-
SessionFactoryBuilder#applyEntityNotFoundDelegate
Configuration#setSessionFactoryObserver
-
SessionFactoryBuilder#addSessionFactoryObservers
Configuration#setCurrentTenantIdentifierResolver
-
SessionFactoryBuilder#applyCurrentTenantIdentifierResolver