<<Back to http://devdoc.net
Mine with nofee-ng to get DevFee back!

Hibernate.orgCommunity Documentation

Appendix A. Legacy Bootstrapping

Table of Contents

A.1. Migration

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.

Note

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 Section 4.1, “Native Bootstrapping”. Configuration is semi-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.

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.

Example A.1. Configuration usage

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" )

            .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 information, including:

  • Place a file named hibernate.properties in a root directory of the classpath
  • Pass an instance of java.util.Properties to Configuration#setProperties
  • Via a Hibernate cfg.xml file
  • System properties using java `-Dproperty=value`

Mapping Configuration methods to the corresponding methods in the new APIs..

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