Hi all,
I am using Hibernate to insert data in a logging database.
I have got four classes to do that :
DAOLog
It's interface : IDAOLogCode:public class DAOLog extends AbstractDAOHibernate implements IDAOLog
An abstract class AbstractDAOHibernateCode:public void doSomething(); public LogHBM createLog(Session pSession); public void updateLog(Session pSession, LogHBM logHBM); public void saveLog(Session pSession, LogHBM logHBM);
And a factory : DAOHibernateFactoryCode:public class AbstractDAOHibernate extends HibernateDaoSupport
Here is the UML class diagram:Code:public class DAOHibernateFactory implements IDAOHibernateFactory{ public static final DAOHibernateFactory instance = new DAOHibernateFactory(); private final static BeanFactory context = new ClassPathXmlApplicationContext(ResourceStringDAOHibernate.RES_SPRING_DAO_HIBERNATE_XML_FILE); private DAOHibernateFactory(){} public static DAOHibernateFactory getInstance(){ return DAOHibernateFactory.instance; } private AbstractDAOHibernate getDAO(ResourceStringDAOHibernate.DAOHibernateEnum pEnum){ return (AbstractDAOHibernate) context.getBean(pEnum.getLabel()); } public IDAOLog getDAOLogging(){ return (IDAOLog)getDAO(ResourceStringDAOHibernate.DAOHibernateEnum.LOGGING_SERVICE); } }
Here is my applicationContext.xml
The problem:Code:<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value> hibernate/hibernate.cfg.xml </value> </property> <property name="mappingResources"> <list> <value>hibernate/Log.hbm</value> </list> </property> </bean> <bean id="hibernateDaoSupport" abstract="true" class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- <bean id="hibernateDaoSupport" class=""></bean> --> <bean id="abstractDAOHibernate" class="com.xxx.consumer.data.Impl.AbstractDAOHibernate" abstract="true"></bean> <bean id="dAOLog" class="com.xxx.consumer.data.Impl.DAOLog" scope="singleton" parent="abstractDAOHibernate" factory-method="getInstance"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
I need to do something like this :
Code:IDAOLog idaoLog = (IDAOLog)FacadeBuisness.getInstance().getDAOFactory().getDAOHibernateFactory().getDAOLogging(); Session vSession = ((DAOLog)idaoLog).getSessionFactory().openSession();
But I can't directly cast idaoLog to DAOLog because of the Spring Proxy.
So I am trying to use the DAOHibernateFactory to return a general type of AbstractDAOHibernate and then cast it to IDAOLog.
So I could use after getSessionFactory() on it.
But, I got the error
And AbstractDAOHibernate has no Interface. So I am blocked. How should I do it? Is create an Interface for AbstractDAOHibernate the good way?Code:$Proxy56 cannot be cast to com.xxx.consumer.data.Impl.AbstractDAOHibernate at com.xxx.consumer.data.DAOHibernateFactory.getDAO(DAOHibernateFactory.java:25)
PS.: Sorry for my bad english...






Reply With Quote