Spring Configuration File
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test290711" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory" class="common.spring.hibernate.SessionFactoryBean">
<property name="sessionFactory" ref="mySessionFactory"></property>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="exposeTransactionAwareSessionFactory"><value>false</value></property>
<property name="mappingLocations">
<list>
<value>classpath:common/hibernate/bf/user/User.hbm.xml</value>
<value>classpath:common/hibernate/bf/user/UserRole.hbm.xml</value>
<value>classpath:common/hibernate/bf/user/Activity.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.current_session_context_class=thread
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider
defaultAutoCommit=false
hibernate.transaction.factory_class=org.hibernate.transaction.JDBCTransactionFactory
</value>
</property>
</bean>
</beans>
<value>
hibernate.current_session_context_class=thread
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider
defaultAutoCommit=false
hibernate.transaction.factory_class=org.hibernate.transaction.JDBCTransactionFactory
</value>
</property>
</bean>
</beans>
-----------------------------------------------
Creating the Sesshion factory
static ApplicationContext ctx = null;
// Create the initial SessionFactory from the default configuration files
static
{
try
sessionFactory = ((SessionFactoryBean) ctx.getBean("sessionFactory")).getSessionFactoty();
}
catch (Throwable ex)
{
log.error("Building SessionFactory failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
// Create the initial SessionFactory from the default configuration files
static
{
try
// getting the connection from Spring FrameWork.
ctx = new ClassPathXmlApplicationContext("common/spring/hibernate/session_factory_config.xml");sessionFactory = ((SessionFactoryBean) ctx.getBean("sessionFactory")).getSessionFactoty();
}
catch (Throwable ex)
{
log.error("Building SessionFactory failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
--------------------------------------------------
Getting the Session
public static Session getSession() {
log.debug("Entering in to getSession().");
Session session = (Session) threadSession.get();
try
{
if (session == null)
{
session = getSessionFactory().openSession();
threadSession.set(session);
}
else
{
log.debug(" Session Found for Current Thread.");
}
log.debug("Entering in to getSession().");
Session session = (Session) threadSession.get();
try
{
if (session == null)
{
session = getSessionFactory().openSession();
threadSession.set(session);
}
else
{
log.debug(" Session Found for Current Thread.");
}
}
catch (HibernateException ex)
{
// throw new InfrastructureException(ex);
}
log.debug("Exiting in to getSession().");
return session;
catch (HibernateException ex)
{
// throw new InfrastructureException(ex);
}
log.debug("Exiting in to getSession().");
return session;
}