org.springframework.orm.hibernate4

## Class LocalSessionFactoryBuilder

- java.lang.Object
- - org.hibernate.cfg.Configuration
  - - org.springframework.orm.hibernate4.LocalSessionFactoryBuilder

- All Implemented Interfaces:
    Serializable

      public class LocalSessionFactoryBuilder
      extends Configuration

  A Spring-provided extension of the standard Hibernate `Configuration` class, adding `SpringSessionContext` as a default and providing convenient ways to specify a DataSource and an application class loader.

  This is designed for programmatic use, e.g. in `@Bean` factory methods. Consider using `LocalSessionFactoryBean` for XML bean definition files.

  Requires Hibernate 4.0 or higher. As of Spring 4.0, it is compatible with (the quite refactored) Hibernate 4.3 as well. We recommend using the latest Hibernate 4.2.x or 4.3.x version, depending on whether you need to remain JPA 2.0 compatible at runtime (Hibernate 4.2) or can upgrade to JPA 2.1 (Hibernate 4.3).

  NOTE: To set up Hibernate 4 for Spring-driven JTA transactions, make sure to either use the `setJtaTransactionManager(java.lang.Object)` method or to set the "hibernate.transaction.factory_class" property to `CMTTransactionFactory`. Otherwise, Hibernate's smart flushing mechanism won't work properly.

  Since:
    3.1
  Author:
    Juergen Hoeller
  See Also:
    `LocalSessionFactoryBean`, Serialized Form

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from class org.hibernate.cfg.Configuration

      `Configuration.MappingsImpl, Configuration.MetadataSourceQueue`

  - ### Field Summary

    - ### Fields inherited from class org.hibernate.cfg.Configuration

      `ARTEFACT_PROCESSING_ORDER, auxiliaryDatabaseObjects, classes, collections, columnNameBindingPerTable, DEFAULT_ARTEFACT_PROCESSING_ORDER, DEFAULT_CACHE_CONCURRENCY_STRATEGY, extendsQueue, fetchProfiles, filterDefinitions, imports, metadataSourceQueue, namedQueries, namedSqlQueries, namingStrategy, propertyReferences, secondPasses, settingsFactory, sqlFunctions, sqlResultSetMappings, tableNameBinding, tables, typeDefs, USE_NEW_ID_GENERATOR_MAPPINGS, xmlHelper`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `LocalSessionFactoryBuilder(DataSource dataSource)` Create a new LocalSessionFactoryBuilder for the given DataSource.
    `LocalSessionFactoryBuilder(DataSource dataSource, ClassLoader classLoader)` Create a new LocalSessionFactoryBuilder for the given DataSource.
    `LocalSessionFactoryBuilder(DataSource dataSource, ResourceLoader resourceLoader)` Create a new LocalSessionFactoryBuilder for the given DataSource.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `LocalSessionFactoryBuilder` | `addAnnotatedClasses(Class<?>... annotatedClasses)` Add the given annotated classes in a batch.
    `LocalSessionFactoryBuilder` | `addPackages(String... annotatedPackages)` Add the given annotated packages in a batch.
    `SessionFactory` | `buildSessionFactory()` Build the `SessionFactory`.
    `Settings` | `buildSettings(Properties props, ServiceRegistry serviceRegistry)`
    `LocalSessionFactoryBuilder` | `scanPackages(String... packagesToScan)` Perform Spring-based scanning for entity classes, registering them as annotated classes with this `Configuration`.
    `LocalSessionFactoryBuilder` | `setCacheRegionFactory(RegionFactory cacheRegionFactory)` Set the Hibernate RegionFactory to use for the SessionFactory.
    `LocalSessionFactoryBuilder` | `setCurrentTenantIdentifierResolver(Object currentTenantIdentifierResolver)` Set a Hibernate 4.1/4.2/4.3 `CurrentTenantIdentifierResolver` to be passed on to the SessionFactory: as an instance, a Class, or a String class name.
    `LocalSessionFactoryBuilder` | `setJtaTransactionManager(Object jtaTransactionManager)` Set the Spring `JtaTransactionManager` or the JTA `TransactionManager` to be used with Hibernate, if any.
    `LocalSessionFactoryBuilder` | `setMultiTenantConnectionProvider(Object multiTenantConnectionProvider)` Set a Hibernate 4.1/4.2/4.3 `MultiTenantConnectionProvider` to be passed on to the SessionFactory: as an instance, a Class, or a String class name.

    - ### Methods inherited from class org.hibernate.cfg.Configuration

      `add, addAnnotatedClass, addAuxiliaryDatabaseObject, addCacheableFile, addCacheableFile, addCacheableFileStrictly, addClass, addDirectory, addDocument, addFetchProfile, addFile, addFile, addFilterDefinition, addInputStream, addJar, addPackage, addProperties, addResource, addResource, addSqlFunction, addURL, addXML, buildMapping, buildMappings, buildSessionFactory, buildSettings, configure, configure, configure, configure, configure, createMappings, doConfigure, doConfigure, findPossibleExtends, generateDropSchemaScript, generateSchemaCreationScript, generateSchemaUpdateScript, generateSchemaUpdateScriptList, getClassMapping, getClassMappings, getCollectionMapping, getCollectionMappings, getConfigurationInputStream, getCurrentTenantIdentifierResolver, getEntityNotFoundDelegate, getEntityResolver, getEntityTuplizerFactory, getFilterDefinitions, getIdentifierGeneratorFactory, getImports, getInterceptor, getMappedSuperclassMappings, getNamedQueries, getNamedSQLQueries, getNamingStrategy, getProperties, getProperty, getReflectionManager, getSessionFactoryObserver, getSqlFunctions, getSqlResultSetMappings, getTableMappings, getTypeResolver, iterateFetchProfiles, mergeProperties, registerTypeContributor, registerTypeOverride, registerTypeOverride, registerTypeOverride, reset, secondPassCompile, secondPassCompileForeignKeys, setCacheConcurrencyStrategy, setCacheConcurrencyStrategy, setCacheConcurrencyStrategy, setCollectionCacheConcurrencyStrategy, setCollectionCacheConcurrencyStrategy, setCurrentTenantIdentifierResolver, setEntityNotFoundDelegate, setEntityResolver, setInterceptor, setNamingStrategy, setPrecedence, setProperties, setProperty, setSessionFactoryObserver, validateSchema`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### LocalSessionFactoryBuilder

          public LocalSessionFactoryBuilder(DataSource dataSource)

      Create a new LocalSessionFactoryBuilder for the given DataSource.
      Parameters:
        `dataSource` - the JDBC DataSource that the resulting Hibernate SessionFactory should be using (may be `null`)

    - #### LocalSessionFactoryBuilder

          public LocalSessionFactoryBuilder(DataSource dataSource,
                                            ClassLoader classLoader)

      Create a new LocalSessionFactoryBuilder for the given DataSource.
      Parameters:
        `dataSource` - the JDBC DataSource that the resulting Hibernate SessionFactory should be using (may be `null`)
        `classLoader` - the ClassLoader to load application classes from

    - #### LocalSessionFactoryBuilder

          public LocalSessionFactoryBuilder(DataSource dataSource,
                                            ResourceLoader resourceLoader)

      Create a new LocalSessionFactoryBuilder for the given DataSource.
      Parameters:
        `dataSource` - the JDBC DataSource that the resulting Hibernate SessionFactory should be using (may be `null`)
        `resourceLoader` - the ResourceLoader to load application classes from

  - ### Method Detail

    - #### setJtaTransactionManager

          public LocalSessionFactoryBuilder setJtaTransactionManager(Object jtaTransactionManager)

      Set the Spring `JtaTransactionManager` or the JTA `TransactionManager` to be used with Hibernate, if any. Allows for using a Spring-managed transaction manager for Hibernate 4's session and cache synchronization, with the "hibernate.transaction.jta.platform" automatically set to it. Also sets "hibernate.transaction.factory_class" to `CMTTransactionFactory`, instructing Hibernate to interact with externally managed transactions.

      A passed-in Spring `JtaTransactionManager` needs to contain a JTA `TransactionManager` reference to be usable here, except for the WebSphere case where we'll automatically set `WebSphereExtendedJtaPlatform` accordingly.

      Note: If this is set, the Hibernate settings should not contain a JTA platform setting to avoid meaningless double configuration.

    - #### setMultiTenantConnectionProvider

          public LocalSessionFactoryBuilder setMultiTenantConnectionProvider(Object multiTenantConnectionProvider)

      Set a Hibernate 4.1/4.2/4.3 `MultiTenantConnectionProvider` to be passed on to the SessionFactory: as an instance, a Class, or a String class name.

      Note that the package location of the `MultiTenantConnectionProvider` interface changed between Hibernate 4.2 and 4.3. This method accepts both variants.

      See Also:
        `AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER`

    - #### setCurrentTenantIdentifierResolver

          public LocalSessionFactoryBuilder setCurrentTenantIdentifierResolver(Object currentTenantIdentifierResolver)

      Set a Hibernate 4.1/4.2/4.3 `CurrentTenantIdentifierResolver` to be passed on to the SessionFactory: as an instance, a Class, or a String class name.
      See Also:
        `AvailableSettings.MULTI_TENANT_IDENTIFIER_RESOLVER`

    - #### setCacheRegionFactory

          public LocalSessionFactoryBuilder setCacheRegionFactory(RegionFactory cacheRegionFactory)

      Set the Hibernate RegionFactory to use for the SessionFactory. Allows for using a Spring-managed RegionFactory instance.

      Note: If this is set, the Hibernate settings should not define a cache provider to avoid meaningless double configuration.

      See Also:
        `RegionFactory`

    - #### addAnnotatedClasses

          public LocalSessionFactoryBuilder addAnnotatedClasses(Class<?>... annotatedClasses)

      Add the given annotated classes in a batch.
      See Also:
        `Configuration.addAnnotatedClass(java.lang.Class)`, `scanPackages(java.lang.String...)`

    - #### addPackages

          public LocalSessionFactoryBuilder addPackages(String... annotatedPackages)

      Add the given annotated packages in a batch.
      See Also:
        `Configuration.addPackage(java.lang.String)`, `scanPackages(java.lang.String...)`

    - #### scanPackages

          public LocalSessionFactoryBuilder scanPackages(String... packagesToScan)
                                                  throws HibernateException

      Perform Spring-based scanning for entity classes, registering them as annotated classes with this `Configuration`.
      Parameters:
        `packagesToScan` - one or more Java package names
      Throws:
        `HibernateException` - if scanning fails for any reason

    - #### buildSettings

          public Settings buildSettings(Properties props,
                                        ServiceRegistry serviceRegistry)
                                 throws HibernateException

      Overrides:
        `buildSettings` in class `Configuration`
      Throws:
        `HibernateException`

    - #### buildSessionFactory

          public SessionFactory buildSessionFactory()
                                             throws HibernateException

      Build the `SessionFactory`.
      Overrides:
        `buildSessionFactory` in class `Configuration`
      Throws:
        `HibernateException`