Problem:
I have read many posts on andromda forum about How to obtain Hibernat sessionFactory and I tried two approaches which ddn't work with me then I make some solution wihch I don't know if it is dirty solution or not but at least it works with me so I guess it is not ;)
the following approaches I tried to apply them inside the ServiceImpl
first I tried the following approach:
|
Code:
org.hibernate.Session session =
org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(
(org.hibernate.SessionFactory)ServiceLocator.instance().getService("sessionFactory"), true); |
and it didn't work with me.
then I followed the following approach:
|
Code:
HibernateInterceptor hibernateInterceptor =
(HibernateInterceptor) ServiceLocator.instance().getService("hibernateInterceptor");
SessionFactory sessionFactory = hibernateInterceptor.getSessionFactory();
Session session = sessionFactory.getCurrentSession(); |
and it didn't work with me also.
and both approaches fails and throw MyServiceException from it. when it reaches the execution of the following line
|
Code:
this.beanFactoryReference = beanFactoryLocator.useBeanFactory(this.beanRefFactoryReferenceId); |
from inside the ServiceLocator Class from
|
Code:
protected synchronized org.springframework.context.ApplicationContext getContext() |
method
Solution:
as I said above I need to make sessionFactory available in the services so I did the following:
- first extracted the andromda-spring-cartrige which is located under the following path
| Quote: |
| $M2_HOME/repository/org/andromda/cartridges/andromda-spring-cartridge/3.3-SNAPSHOT |
and copied the template directory to my
| Quote: |
| project/mda/src/main/config/cartridge |
folder
then I created SpringTemplateMergeMappings.xml file under
| Quote: |
| project/mda/src/main/config/mappings |
which contains the following mapping
|
Code:
<mappings name="SpringTemplateMerge">
<mapping>
<from><![CDATA[<!-- cartridge-template merge-point -->]]></from>
<to>
<![CDATA[
<template path="templates/spring/applicationContext.xml.vsl"
outputPattern="$applicationContext"
outlet="spring-configuration"
overwrite="true" outputToSingleFile="true"
outputOnEmptyElements="false">
<modelElements>
<modelElement variable="entities">
<type name="org.andromda.cartridges.spring.metafacades.SpringEntity"/>
</modelElement>
<modelElement variable="services">
<type name="org.andromda.cartridges.spring.metafacades.SpringService">
<property name="abstract">false</property>
</type>
</modelElement>
<modelElement variable="manageables">
<type name="org.andromda.cartridges.spring.metafacades.SpringManageableEntity"/>
</modelElement>
</modelElements>
</template>
]]>
</to>
</mapping>
</mappings>
|
then I have inserted the following two lines:
|
Code:
<property name="mergeLocation">${conf.dir}/cartridge</property>
<property name="mergeMappingsUri">
file:${conf.dir}/mappings/SpringTemplateMergeMappings.xml
</property> |
inside the
| Quote: |
| mda/src/main/config/andromda.xml |
inside the spring namespace
and altered the applicationContext.vsl which is located under
| Quote: |
| /mda/src/main/config/cartridge/template/spring/ |
to add the following line in the
|
Code:
#foreach($service in $services)
<!-- $service.name Service Proxy with inner $service.name Service Implementation -->
<bean id="$service.getBeanName(false)" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<bean class="$service.fullyQualifiedImplementationName">
#if ($daoRefsEnabled)
<!-- sessionFactory being injected by -->
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
#foreach($entityRef in $service.allEntityReferences)
<property name="$entityRef.daoName"><ref bean="$entityRef.targetElement.getBeanName(false)"/></property>
#end
#end
|
<!-- ========================= Start of SERVICE DEFINITIONS ========================= --> section
at the end I have edited the ServiceBase.VSL to include the setter and getter for sessionFactory as follows
| Code: |
|
private SessionFactory sessionFactory; /** * * Sets the reference to <code>sessionFactory</code>. */ public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } /** * * Gets the reference to <code>sessionFactory</code>. */ protected SessionFactory getSessionFactory() {   return this.sessionFactory; } |

This is not a good practice.
after accomplishing this task I found that Its not a good practice to get the sessionFactory inside the serviceImpl as serviceImpl should just make calles to the entityDaos and inside the entityDaoImpl you can get reference to the sessionFactory.
Ali Abdel-Aziz.