org.unitils.orm.hibernate
Class HibernateModule

java.lang.Object
  extended by org.unitils.orm.common.OrmModule<org.hibernate.SessionFactory,org.hibernate.Session,org.hibernate.cfg.Configuration,HibernateSessionFactory,OrmConfig,HibernateAnnotationConfigLoader>
      extended by org.unitils.orm.hibernate.HibernateModule
All Implemented Interfaces:
Module, Flushable

public class HibernateModule
extends OrmModule<org.hibernate.SessionFactory,org.hibernate.Session,org.hibernate.cfg.Configuration,HibernateSessionFactory,OrmConfig,HibernateAnnotationConfigLoader>
implements Module, Flushable

Module providing support for unit tests for code that uses Hibernate. It offers an easy way of loading hibernate SessionFactories and having them injected them into the test. It also offers a test to check whether the hibernate mapping is consistent with the structure of the database.

A Hibernate SessionFactory is created when requested and injected into all fields or methods of the test annotated with HibernateSessionFactory.

It is highly recommended to write a unit test that invokes HibernateUnitils.assertMappingWithDatabaseConsistent(), This is a very useful test that verifies whether the mapping of all your Hibernate mapped objects still corresponds with the actual structure of the database.

Author:
Filip Neven, Tim Ducheyne

Nested Class Summary
protected  class HibernateModule.HibernateTestListener
          The TestListener for this module
 
Nested classes/interfaces inherited from class org.unitils.orm.common.OrmModule
OrmModule.OrmTestListener
 
Field Summary
static String PROPKEY_CONFIGURATION_CLASS_NAME
           
 
Fields inherited from class org.unitils.orm.common.OrmModule
configuredOrmPersistenceUnitCache, ormPersistenceUnitLoader, ormSpringSupport, persistenceUnitConfigLoader
 
Constructor Summary
HibernateModule()
           
 
Method Summary
 void afterInit()
          Gives the module the opportunity to performs initialization that can only work after all other modules have been initialized
 void assertMappingWithDatabaseConsistent(Object testObject)
          Checks if the mapping of the Hibernate managed objects with the database is correct.
protected  HibernateAnnotationConfigLoader createOrmConfigLoader()
           
protected  OrmPersistenceUnitLoader<org.hibernate.SessionFactory,org.hibernate.cfg.Configuration,OrmConfig> createOrmPersistenceUnitLoader()
           
protected  org.hibernate.Session doGetActivePersistenceContext(Object testObject)
          Implementations of this method must return the persistence context object that is associated with the current transaction, if any.
protected  org.hibernate.Session doGetPersistenceContext(Object testObject)
          Implementations of this method must return a persistence context object, and must be associated with the current transaction active in unitils.
protected  void flushOrmPersistenceContext(org.hibernate.Session activeSession)
          Flushes all pending update, using the given active persistence context
 Class<? extends org.hibernate.cfg.Configuration> getConfigurationObjectClass()
           
protected  org.hibernate.dialect.Dialect getDatabaseDialect(org.hibernate.cfg.Configuration configuration)
          Gets the database dialect from the given Hibernate Configuration
protected  DataSource getDataSource()
           
protected  String getOrmSpringSupportImplClassName()
           
protected  Class<org.hibernate.SessionFactory> getPersistenceUnitClass()
           
protected  Class<HibernateSessionFactory> getPersistenceUnitConfigAnnotationClass()
           
 TestListener getTestListener()
          Creates the test listener for this module.
 void init(Properties configuration)
          Initializes the module with the given configuration settings.
 
Methods inherited from class org.unitils.orm.common.OrmModule
flushDatabaseUpdates, getActivePersistenceContext, getConfigurationObject, getConfiguredPersistenceUnit, getDatabaseModule, getPersistenceContext, getPersistenceUnit, getPersistenceUnitConfig, initOrmSpringSupport, injectOrmPersistenceUnitIntoTestObject, isPersistenceUnitConfiguredFor, isSpringModuleEnabled
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.unitils.database.util.Flushable
flushDatabaseUpdates
 

Field Detail

PROPKEY_CONFIGURATION_CLASS_NAME

public static final String PROPKEY_CONFIGURATION_CLASS_NAME
See Also:
Constant Field Values
Constructor Detail

HibernateModule

public HibernateModule()
Method Detail

init

public void init(Properties configuration)
Description copied from interface: Module
Initializes the module with the given configuration settings.

Specified by:
init in interface Module
Overrides:
init in class OrmModule<org.hibernate.SessionFactory,org.hibernate.Session,org.hibernate.cfg.Configuration,HibernateSessionFactory,OrmConfig,HibernateAnnotationConfigLoader>
Parameters:
configuration - The Unitils configuration, not null

afterInit

public void afterInit()
Description copied from interface: Module
Gives the module the opportunity to performs initialization that can only work after all other modules have been initialized

Specified by:
afterInit in interface Module
Overrides:
afterInit in class OrmModule<org.hibernate.SessionFactory,org.hibernate.Session,org.hibernate.cfg.Configuration,HibernateSessionFactory,OrmConfig,HibernateAnnotationConfigLoader>

createOrmConfigLoader

protected HibernateAnnotationConfigLoader createOrmConfigLoader()
Specified by:
createOrmConfigLoader in class OrmModule<org.hibernate.SessionFactory,org.hibernate.Session,org.hibernate.cfg.Configuration,HibernateSessionFactory,OrmConfig,HibernateAnnotationConfigLoader>
Returns:
A new instance of the ResourceConfigLoader that scans a test object for a persistence unit configuration, and returns a specific subtype of OrmConfig that wraps this configuration

getPersistenceUnitConfigAnnotationClass

protected Class<HibernateSessionFactory> getPersistenceUnitConfigAnnotationClass()
Specified by:
getPersistenceUnitConfigAnnotationClass in class OrmModule<org.hibernate.SessionFactory,org.hibernate.Session,org.hibernate.cfg.Configuration,HibernateSessionFactory,OrmConfig,HibernateAnnotationConfigLoader>
Returns:
The class of the annotation that is used for configuring and requesting injection of the persistence unit

getPersistenceUnitClass

protected Class<org.hibernate.SessionFactory> getPersistenceUnitClass()
Specified by:
getPersistenceUnitClass in class OrmModule<org.hibernate.SessionFactory,org.hibernate.Session,org.hibernate.cfg.Configuration,HibernateSessionFactory,OrmConfig,HibernateAnnotationConfigLoader>
Returns:
The type of the persistence unit

createOrmPersistenceUnitLoader

protected OrmPersistenceUnitLoader<org.hibernate.SessionFactory,org.hibernate.cfg.Configuration,OrmConfig> createOrmPersistenceUnitLoader()
Specified by:
createOrmPersistenceUnitLoader in class OrmModule<org.hibernate.SessionFactory,org.hibernate.Session,org.hibernate.cfg.Configuration,HibernateSessionFactory,OrmConfig,HibernateAnnotationConfigLoader>
Returns:
A new instance of OrmPersistenceUnitLoader that can create a new persistence unit based on an OrmConfig object

getOrmSpringSupportImplClassName

protected String getOrmSpringSupportImplClassName()
Specified by:
getOrmSpringSupportImplClassName in class OrmModule<org.hibernate.SessionFactory,org.hibernate.Session,org.hibernate.cfg.Configuration,HibernateSessionFactory,OrmConfig,HibernateAnnotationConfigLoader>
Returns:
The fully qualified classname of the concrete implementation of OrmSpringSupport that is used by the ORM module implementation

doGetPersistenceContext

protected org.hibernate.Session doGetPersistenceContext(Object testObject)
Description copied from class: OrmModule
Implementations of this method must return a persistence context object, and must be associated with the current transaction active in unitils. The implementation can presume that a persistence unit is available, so the method may not return null;

Specified by:
doGetPersistenceContext in class OrmModule<org.hibernate.SessionFactory,org.hibernate.Session,org.hibernate.cfg.Configuration,HibernateSessionFactory,OrmConfig,HibernateAnnotationConfigLoader>
Parameters:
testObject - The test instance, not null
Returns:
An implementation specific persistence context, not null

doGetActivePersistenceContext

protected org.hibernate.Session doGetActivePersistenceContext(Object testObject)
Description copied from class: OrmModule
Implementations of this method must return the persistence context object that is associated with the current transaction, if any. The implementation can presume that a persistence unit is available. If no persistence context is currently active, null is returned.

Specified by:
doGetActivePersistenceContext in class OrmModule<org.hibernate.SessionFactory,org.hibernate.Session,org.hibernate.cfg.Configuration,HibernateSessionFactory,OrmConfig,HibernateAnnotationConfigLoader>
Parameters:
testObject - The test instance, not null
Returns:
The currently active persistence context, if any

flushOrmPersistenceContext

protected void flushOrmPersistenceContext(org.hibernate.Session activeSession)
Description copied from class: OrmModule
Flushes all pending update, using the given active persistence context

Specified by:
flushOrmPersistenceContext in class OrmModule<org.hibernate.SessionFactory,org.hibernate.Session,org.hibernate.cfg.Configuration,HibernateSessionFactory,OrmConfig,HibernateAnnotationConfigLoader>
Parameters:
activeSession - Active persistence context, associated with the current transaction, not null

assertMappingWithDatabaseConsistent

public void assertMappingWithDatabaseConsistent(Object testObject)
Checks if the mapping of the Hibernate managed objects with the database is correct.

Parameters:
testObject - The test instance, not null

getConfigurationObjectClass

public Class<? extends org.hibernate.cfg.Configuration> getConfigurationObjectClass()
Returns:
The subclass of org.hibernate.cfg.Configuration that is used for configuring hibernate

getDatabaseDialect

protected org.hibernate.dialect.Dialect getDatabaseDialect(org.hibernate.cfg.Configuration configuration)
Gets the database dialect from the given Hibernate Configuration
Parameters:
configuration - The hibernate config, not null
Returns:
the database Dialect, not null

getDataSource

protected DataSource getDataSource()

getTestListener

public TestListener getTestListener()
Description copied from interface: Module
Creates the test listener for this module.

Specified by:
getTestListener in interface Module
Returns:
The TestListener associated with this module


Copyright © 2011. All Rights Reserved.