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.


ostgresql://127.0.0.1:5432/mars001</value>
Reply With Quote