org.springframework.orm.hibernate3.annotation
Class AnnotationSessionFactoryBuilder

java.lang.Object
  extended by org.springframework.orm.hibernate3.SessionFactoryBuilderSupport<AnnotationSessionFactoryBuilder>
      extended by org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBuilder
Direct Known Subclasses:
AnnotationSessionFactoryBean

public class AnnotationSessionFactoryBuilder
extends SessionFactoryBuilderSupport<AnnotationSessionFactoryBuilder>

Hibernate AnnotationConfiguration builder suitable for use within Spring @Configuration class @Bean methods. For complete details on features, see the JavaDoc for the SessionFactoryBuilderSupport superclass. For use in Spring XML configuration, see the AnnotationSessionFactoryBean subclass.

As noted in SessionFactoryBuilderSupport JavaDoc, this class requires Hibernate 3.2 or later; it additionally requires that the Java Persistence API and Hibernate Annotations add-ons are present.

Setter methods return the builder instance in order to facilitate a concise and convenient method-chaining style. For example:

 @Configuration
 public class DataConfig {
     @Bean
     public SessionFactory sessionFactory() {
         return new AnnotationSessionFactoryBuilder()
             .setDataSource(dataSource())
             .setPackagesToScan("com.myco"})
             .buildSessionFactory();
     }
 }
 

Most Hibernate configuration operations can be performed directly against this API; however you may also access access and configure the underlying AnnotationConfiguration object by using the SessionFactoryBuilderSupport.doWithConfiguration(org.springframework.orm.hibernate3.HibernateConfigurationCallback) method and providing a HibernateConfigurationCallback as follows:

 SessionFactory sessionFactory =
     new AnnotationSessionFactoryBuilder()
         // ...
         .doWithConfiguration(new HibernateConfigurationCallback<AnnotationConfiguration>() {
             public void configure(AnnotationConfiguration cfg) {
                 cfg.addAnnotatedClass(Foo.class);
             }
          })
         .buildSessionFactory();
 

Since:
3.1
Author:
Juergen Hoeller, Chris Beams
See Also:
SessionFactoryBuilderSupport, SessionFactoryBuilder, AnnotationSessionFactoryBean

Field Summary
 
Fields inherited from class org.springframework.orm.hibernate3.SessionFactoryBuilderSupport
logger
 
Constructor Summary
AnnotationSessionFactoryBuilder()
          Construct a new AnnotationSessionFactoryBuilder
AnnotationSessionFactoryBuilder(DataSource dataSource)
          Construct a new AnnotationSessionFactoryBuilder with the given Spring-managed DataSource instance.
 
Method Summary
protected  Class<? extends org.hibernate.cfg.Configuration> getDefaultConfigurationClass()
          Return the default Configuration type used by this instance.
protected  void postProcessAnnotationConfiguration()
          To be implemented by subclasses which want to to perform custom post-processing of the AnnotationConfiguration object after this FactoryBean performed its default initialization.
protected  void postProcessConfiguration()
          Delegates to postProcessAnnotationConfiguration().
protected  void postProcessMappings()
          Reads metadata from annotated classes and packages into the AnnotationConfiguration instance.
protected  void scanPackages()
          Perform Spring-based scanning for entity classes.
 AnnotationSessionFactoryBuilder setAnnotatedClasses(Class<?>... annotatedClasses)
          Specify annotated classes, for which mappings will be read from class-level JDK 1.5+ annotation metadata.
 AnnotationSessionFactoryBuilder setAnnotatedPackages(String... annotatedPackages)
          Specify the names of annotated packages, for which package-level JDK 1.5+ annotation metadata will be read.
 AnnotationSessionFactoryBuilder setEntityTypeFilters(TypeFilter... entityTypeFilters)
          Specify custom type filters for Spring-based scanning for entity classes.
 AnnotationSessionFactoryBuilder setPackagesToScan(String... packagesToScan)
          Set whether to use Spring-based scanning for entity classes in the classpath instead of listing annotated classes explicitly.
 AnnotationSessionFactoryBuilder setResourcePatternResolver(ResourcePatternResolver resourcePatternResolver)
           
 
Methods inherited from class org.springframework.orm.hibernate3.SessionFactoryBuilderSupport
afterSessionFactoryCreation, beforeSessionFactoryDestruction, buildSessionFactory, createDatabaseSchema, doBuildSessionFactory, doWithConfiguration, dropDatabaseSchema, executeSchemaScript, executeSchemaStatement, getBeanClassLoader, getCacheRegionFactory, getConfigTimeDataSource, getConfigTimeLobHandler, getConfigTimeTransactionManager, getConfiguration, getDataSource, getHibernateProperties, getSessionFactory, isExposeTransactionAwareSessionFactory, isUseTransactionAwareDataSource, newConfiguration, newSessionFactory, postBuildSessionFactory, preBuildSessionFactory, setCacheableMappingLocations, setCacheRegionFactory, setClassLoader, setCollectionCacheStrategies, setConfigLocation, setConfigLocations, setConfigurationClass, setDataSource, setEntityCacheStrategies, setEntityInterceptor, setEventListeners, setExposeTransactionAwareSessionFactory, setFilterDefinitions, setHibernateProperties, setJtaTransactionManager, setLobHandler, setMappingDirectoryLocations, setMappingJarLocations, setMappingLocations, setMappingResources, setNamingStrategy, setSchemaUpdate, setTypeDefinitions, setUseTransactionAwareDataSource, updateDatabaseSchema, validateDatabaseSchema, wrapSessionFactoryIfNecessary
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AnnotationSessionFactoryBuilder

public AnnotationSessionFactoryBuilder()
Construct a new AnnotationSessionFactoryBuilder


AnnotationSessionFactoryBuilder

public AnnotationSessionFactoryBuilder(DataSource dataSource)
Construct a new AnnotationSessionFactoryBuilder with the given Spring-managed DataSource instance.

See Also:
SessionFactoryBuilderSupport.setDataSource(javax.sql.DataSource)
Method Detail

getDefaultConfigurationClass

protected Class<? extends org.hibernate.cfg.Configuration> getDefaultConfigurationClass()
Return the default Configuration type used by this instance. May be overridden with SessionFactoryBuilderSupport.setConfigurationClass(Class).

This implementation returns Configuration if Hibernate 3.6 or greater is available on the runtime classpath, otherwise AnnotationConfiguration. This accommodates the consolidation of these two types and deprecation of the latter in Hibernate 3.6.

Specified by:
getDefaultConfigurationClass in class SessionFactoryBuilderSupport<AnnotationSessionFactoryBuilder>
See Also:
SessionFactoryBuilderSupport.doWithConfiguration(org.springframework.orm.hibernate3.HibernateConfigurationCallback)

setPackagesToScan

public AnnotationSessionFactoryBuilder setPackagesToScan(String... packagesToScan)
Set whether to use Spring-based scanning for entity classes in the classpath instead of listing annotated classes explicitly.

Default is none. Specify packages to search for autodetection of your entity classes in the classpath. This is analogous to Spring's component-scan feature (ClassPathBeanDefinitionScanner).


setAnnotatedClasses

public AnnotationSessionFactoryBuilder setAnnotatedClasses(Class<?>... annotatedClasses)
Specify annotated classes, for which mappings will be read from class-level JDK 1.5+ annotation metadata.

See Also:
AnnotationConfiguration.addAnnotatedClass(java.lang.Class)

setAnnotatedPackages

public AnnotationSessionFactoryBuilder setAnnotatedPackages(String... annotatedPackages)
Specify the names of annotated packages, for which package-level JDK 1.5+ annotation metadata will be read.

See Also:
AnnotationConfiguration.addPackage(java.lang.String)

setEntityTypeFilters

public AnnotationSessionFactoryBuilder setEntityTypeFilters(TypeFilter... entityTypeFilters)
Specify custom type filters for Spring-based scanning for entity classes.

Default is to search all specified packages for classes annotated with @javax.persistence.Entity, @javax.persistence.Embeddable or @javax.persistence.MappedSuperclass, as well as for Hibernate's special @org.hibernate.annotations.Entity.

See Also:
setPackagesToScan(java.lang.String...)

setResourcePatternResolver

public AnnotationSessionFactoryBuilder setResourcePatternResolver(ResourcePatternResolver resourcePatternResolver)

scanPackages

protected void scanPackages()
Perform Spring-based scanning for entity classes.

See Also:
setPackagesToScan(java.lang.String...)

postProcessMappings

protected void postProcessMappings()
                            throws org.hibernate.HibernateException
Reads metadata from annotated classes and packages into the AnnotationConfiguration instance.

Overrides:
postProcessMappings in class SessionFactoryBuilderSupport<AnnotationSessionFactoryBuilder>
Throws:
org.hibernate.HibernateException - in case of Hibernate initialization errors
See Also:
Configuration.buildMappings()

postProcessConfiguration

protected void postProcessConfiguration()
                                 throws org.hibernate.HibernateException
Delegates to postProcessAnnotationConfiguration().

Overrides:
postProcessConfiguration in class SessionFactoryBuilderSupport<AnnotationSessionFactoryBuilder>
Throws:
org.hibernate.HibernateException - in case of Hibernate initialization errors
See Also:
Configuration.buildMappings()

postProcessAnnotationConfiguration

protected void postProcessAnnotationConfiguration()
                                           throws org.hibernate.HibernateException
To be implemented by subclasses which want to to perform custom post-processing of the AnnotationConfiguration object after this FactoryBean performed its default initialization.

Note: As of Hibernate 3.6, AnnotationConfiguration's features have been rolled into Configuration itself. Simply overriding #postProcessConfiguration(org.hibernate.cfg.Configuration) becomes an option as well then.

Parameters:
config - the current AnnotationConfiguration object
Throws:
org.hibernate.HibernateException - in case of Hibernate initialization errors