Class LocalSessionFactoryBean

    • Method Detail

      • setDataSource

        public void setDataSource​(DataSource dataSource)
        Set the DataSource to be used by the SessionFactory. If set, this will override corresponding settings in Hibernate properties.

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

      • setConfigLocation

        public void setConfigLocation​(Resource configLocation)
        Set the location of a single Hibernate XML config file, for example as classpath resource "classpath:hibernate.cfg.xml".

        Note: Can be omitted when all necessary properties and mapping resources are specified locally via this bean.

        See Also:
        Configuration.configure(java.net.URL)
      • setConfigLocations

        public void setConfigLocations​(Resource... configLocations)
        Set the locations of multiple Hibernate XML config files, for example as classpath resources "classpath:hibernate.cfg.xml,classpath:extension.cfg.xml".

        Note: Can be omitted when all necessary properties and mapping resources are specified locally via this bean.

        See Also:
        Configuration.configure(java.net.URL)
      • setMappingLocations

        public void setMappingLocations​(Resource... mappingLocations)
        Set locations of Hibernate mapping files, for example as classpath resource "classpath:example.hbm.xml". Supports any resource location via Spring's resource abstraction, for example relative paths like "WEB-INF/mappings/example.hbm.xml" when running in an application context.

        Can be used to add to mappings from a Hibernate XML config file, or to specify all mappings locally.

        See Also:
        Configuration.addInputStream(java.io.InputStream)
      • setCacheableMappingLocations

        public void setCacheableMappingLocations​(Resource... cacheableMappingLocations)
        Set locations of cacheable Hibernate mapping files, for example as web app resource "/WEB-INF/mapping/example.hbm.xml". Supports any resource location via Spring's resource abstraction, as long as the resource can be resolved in the file system.

        Can be used to add to mappings from a Hibernate XML config file, or to specify all mappings locally.

        See Also:
        Configuration.addCacheableFile(java.io.File)
      • setMappingJarLocations

        public void setMappingJarLocations​(Resource... mappingJarLocations)
        Set locations of jar files that contain Hibernate mapping resources, like "WEB-INF/lib/example.hbm.jar".

        Can be used to add to mappings from a Hibernate XML config file, or to specify all mappings locally.

        See Also:
        Configuration.addJar(java.io.File)
      • setEntityTypeFilters

        public void 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.

        Since:
        4.1
        See Also:
        setPackagesToScan(java.lang.String...)
      • setHibernateProperties

        public void setHibernateProperties​(Properties hibernateProperties)
        Set Hibernate properties, such as "hibernate.dialect".

        Note: Do not specify a transaction provider here when using Spring-driven transactions. It is also advisable to omit connection provider settings and use a Spring-set DataSource instead.

        See Also:
        setDataSource(javax.sql.DataSource)
      • getHibernateProperties

        public Properties getHibernateProperties()
        Return the Hibernate properties, if any. Mainly available for configuration through property paths that specify individual keys.
      • buildSessionFactory

        protected SessionFactory buildSessionFactory​(LocalSessionFactoryBuilder sfb)
        Subclasses can override this method to perform custom initialization of the SessionFactory instance, creating it via the given Configuration object that got prepared by this LocalSessionFactoryBean.

        The default implementation invokes LocalSessionFactoryBuilder's buildSessionFactory. A custom implementation could prepare the instance in a specific way (e.g. applying a custom ServiceRegistry) or use a custom SessionFactoryImpl subclass.

        Parameters:
        sfb - LocalSessionFactoryBuilder prepared by this LocalSessionFactoryBean
        Returns:
        the SessionFactory instance
        See Also:
        LocalSessionFactoryBuilder.buildSessionFactory()
      • getConfiguration

        public final Configuration getConfiguration()
        Return the Hibernate Configuration object used to build the SessionFactory. Allows for access to configuration metadata stored there (rarely needed).
        Throws:
        IllegalStateException - if the Configuration object has not been initialized yet
      • getObject

        public SessionFactory getObject()
        Description copied from interface: FactoryBean
        Return an instance (possibly shared or independent) of the object managed by this factory.

        As with a BeanFactory, this allows support for both the Singleton and Prototype design pattern.

        If this FactoryBean is not fully initialized yet at the time of the call (for example because it is involved in a circular reference), throw a corresponding FactoryBeanNotInitializedException.

        As of Spring 2.0, FactoryBeans are allowed to return null objects. The factory will consider this as normal value to be used; it will not throw a FactoryBeanNotInitializedException in this case anymore. FactoryBean implementations are encouraged to throw FactoryBeanNotInitializedException themselves now, as appropriate.

        Specified by:
        getObject in interface FactoryBean<SessionFactory>
        Returns:
        an instance of the bean (can be null)
        See Also:
        FactoryBeanNotInitializedException
      • getObjectType

        public Class<?> getObjectType()
        Description copied from interface: FactoryBean
        Return the type of object that this FactoryBean creates, or null if not known in advance.

        This allows one to check for specific types of beans without instantiating objects, for example on autowiring.

        In the case of implementations that are creating a singleton object, this method should try to avoid singleton creation as far as possible; it should rather estimate the type in advance. For prototypes, returning a meaningful type here is advisable too.

        This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available.

        NOTE: Autowiring will simply ignore FactoryBeans that return null here. Therefore it is highly recommended to implement this method properly, using the current state of the FactoryBean.

        Specified by:
        getObjectType in interface FactoryBean<SessionFactory>
        Returns:
        the type of object that this FactoryBean creates, or null if not known at the time of the call
        See Also:
        ListableBeanFactory.getBeansOfType(java.lang.Class<T>)
      • isSingleton

        public boolean isSingleton()
        Description copied from interface: FactoryBean
        Is the object managed by this factory a singleton? That is, will FactoryBean.getObject() always return the same object (a reference that can be cached)?

        NOTE: If a FactoryBean indicates to hold a singleton object, the object returned from getObject() might get cached by the owning BeanFactory. Hence, do not return true unless the FactoryBean always exposes the same reference.

        The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there.

        NOTE: This method returning false does not necessarily indicate that returned objects are independent instances. An implementation of the extended SmartFactoryBean interface may explicitly indicate independent instances through its SmartFactoryBean.isPrototype() method. Plain FactoryBean implementations which do not implement this extended interface are simply assumed to always return independent instances if the isSingleton() implementation returns false.

        Specified by:
        isSingleton in interface FactoryBean<SessionFactory>
        Returns:
        whether the exposed object is a singleton
        See Also:
        FactoryBean.getObject(), SmartFactoryBean.isPrototype()