Results 1 to 5 of 5

Thread: Spring + Hibernate Proxy Problem

  1. #1
    Join Date
    Sep 2005
    Posts
    3

    Default Spring + Hibernate Proxy Problem

    I am using spring + hibernate. I have written following code in application context.xml.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName">
    <value>org.postgresql.Driver</value>
    </property>
    <property name="url">
    <value>jdbcostgresql://127.0.0.1:5432/mars001</value>
    </property>
    <property name="username">
    <value>ehchina</value>
    </property>
    <property name="password">
    <value>2345wert</value>
    </property>
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource">
    <ref local="dataSource" />
    </property>
    <property name="mappingResources">
    <list>
    <value>com/mars/hibernate/User.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.PostgreSQLDialect
    </prop>
    <prop key="hibernate.show_sql">
    true
    </prop>
    </props>
    </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory">
    <ref local="sessionFactory"/>
    </property>
    </bean>

    <bean id="userDAO" class="com.mars.dao.UserDAO">
    <property name="sessionFactory">
    <ref local="sessionFactory"/>
    </property>
    </bean>
    <bean id="userDAOProxy" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager">
    <ref bean="transactionManager"/>
    </property>
    <property name="target">
    <ref local="userDAO"/>
    </property>
    <property name="proxyInterfaces">
    <value>com.mars.dao.IUserDAO</value>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    </props>
    </property>
    </bean>
    </beans>

    my java code ,as followed

    UserDAO.java
    public class UserDAO extends HibernateDaoSupport implements IUserDAO{

    public void insertUser(User user) {
    getHibernateTemplate().saveOrUpdate(user);
    }

    }

    public class testSpringHibernate extends TestCase{

    public void testInsert() {

    AbstractXmlApplicationContext ctx = new ClassPathXmlApplicationContext("Hibernate-Context.xml");
    UserDAO userDAO = (UserDAO)ctx.getBean("userDAOProxy");
    User user = new User();
    user.setUsername("erica");
    user.setPassword("mypass");
    userDAO.insertUser(user);
    }

    }

    but when i run this junit testcase, it happened these errors ,as followed:

    java.lang.ClassCastException: $Proxy1
    at test.mars.hibernate.testSpringHibernate.testInsert (testSpringHibernate.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:154 )
    at junit.framework.TestCase.runBare(TestCase.java:127 )
    at junit.framework.TestResult$1.protect(TestResult.ja va:106)
    at junit.framework.TestResult.runProtected(TestResult .java:124)
    at junit.framework.TestResult.run(TestResult.java:109 )
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:2 08)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:478)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:344)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:196)

    What's wrong with my config file or java code,please help me.

  2. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    In your configuration you specify the interface implemented by the proxy as "IUserDAO". So the proxy does implement this interface but is no subtype of the target class itself.

    Solution: Use IUserDAO in your code.

    Regards,
    Andreas

  3. #3
    Join Date
    Sep 2005
    Posts
    3

    Default

    I has used the IUserDAO in my code,but it cann't solute the problem.

  4. #4
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Quote Originally Posted by emi.pan
    I has used the IUserDAO in my code,but it cann't solute the problem.
    Why not? What happens if you try this:

    Code:
     public void testInsert&#40;&#41; &#123;
    
    AbstractXmlApplicationContext ctx = new ClassPathXmlApplicationContext&#40;"Hibernate-Context.xml"&#41;;
    IUserDAO userDAO = &#40;IUserDAO&#41;ctx.getBean&#40;"userDAOProxy"&#41;;
    User user = new User&#40;&#41;;
    user.setUsername&#40;"erica"&#41;;
    user.setPassword&#40;"mypass"&#41;;
    userDAO.insertUser&#40;user&#41;;
    &#125;

  5. #5
    Join Date
    Sep 2005
    Posts
    3

    Default

    , Thank you very mush, Senft, I followed your code and soluted this problem.

Similar Threads

  1. Replies: 1
    Last Post: Jul 5th, 2005, 03:48 AM
  2. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM
  3. Other Hibernate DAO LazyInitializationExceptions
    By bernardsirius in forum Data
    Replies: 5
    Last Post: Feb 18th, 2005, 04:09 PM
  4. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM
  5. Replies: 5
    Last Post: Aug 27th, 2004, 07:13 PM

Posting Permissions

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