Results 1 to 3 of 3

Thread: Problem with Spring / Hibernate and Proxy

  1. #1
    Join Date
    May 2011
    Posts
    9

    Default Problem with Spring / Hibernate and Proxy

    Hi all,

    I am using Hibernate to insert data in a logging database.

    I have got four classes to do that :

    DAOLog
    Code:
    public class DAOLog extends AbstractDAOHibernate implements IDAOLog
    It's interface : IDAOLog
    Code:
    	public void doSomething();
    	public LogHBM createLog(Session pSession);
    	public void updateLog(Session pSession, LogHBM logHBM);
    	public void saveLog(Session pSession, LogHBM logHBM);
    An abstract class AbstractDAOHibernate
    Code:
    public class AbstractDAOHibernate extends HibernateDaoSupport
    And a factory : DAOHibernateFactory
    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 the UML class diagram:


    Here is my applicationContext.xml
    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>
    The problem:

    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
    Code:
    $Proxy56 cannot be cast to com.xxx.consumer.data.Impl.AbstractDAOHibernate
    at com.xxx.consumer.data.DAOHibernateFactory.getDAO(DAOHibernateFactory.java:25)
    And AbstractDAOHibernate has no Interface. So I am blocked. How should I do it? Is create an Interface for AbstractDAOHibernate the good way?

    PS.: Sorry for my bad english...
    Last edited by TorTukiTu; May 13th, 2011 at 03:49 AM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Ehrm... Argh... Why on earth are you using hibernate, with spring, in such an ugly way... Simply use dependency injection instead of the long winded, ugly, easily breakable , hard to test lookups... Next to that don't use HibernateDaoSupport/HibernateTemplate those are deprecated in favor of a plain hibernate api. If you use spring and hibernate (and use spring to manage the transactions) don't use openSession, use getCurrentSession.

    However to answer your question, enable class proxies instead of interface based proxies.

    However in short, I would run from the current code base....
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    May 2011
    Posts
    9

    Default

    Tank you, Marten Deinum.

    Do you have some links to tutorials or interresting books about it ?

    I followed the http://static.springsource.org/sprin...rence/orm.html , where HibernateTemplate is still used.

    I am afraid all the tutorials I made an the book I have is not up to date at all...

    Tortue 974.
    Last edited by TorTukiTu; May 13th, 2011 at 05:53 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •