写了个测试类,然后向数据库添加数据,发现无法更新,发现没有添加@Transactional,事物没有提交,之后 就在 dao方法上加上@Transactional  ,可以 就报错了:
java.lang.ClassCastException: com.sun.proxy.$Proxy22 cannot be cast to com.pp.dao.account.AccountDaoImpl
at com.pp.tt.T1.testUpdate(T1.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
小弟 ssh框架 整合 新手 ,不知道 是不是 applicationContext.xml 配置有问题:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
                    http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://www.springframework.org/schema/tx 
                    http://www.springframework.org/schema/tx/spring-tx.xsd
                    http://www.springframework.org/schema/context   
                        http://www.springframework.org/schema/context/spring-context-2.5.xsd
                        http://www.springframework.org/schema/aop   
                        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
                        ">
<aop:aspectj-autoproxy />
<context:annotation-config />
<context:component-scan base-package="com.pp" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/wy" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="pp" />
<property name="dataSource" ref="dataSource"></property>
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
再贴上dao方法:package com.pp.dao.account;import java.util.List;import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;import org.hibernate.validator.constraints.Email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import com.pp.beans.Account;
import com.pp.dao.BaseDao;
import com.pp.dao.Dao;@Transactional
@Service("accountDao")
public class AccountDaoImpl implements AccountDao {
@Resource
private BaseDao baseDao;
@PersistenceContext
private EntityManager entityManager; @SuppressWarnings("unchecked")
@Override
public Account loginDao(Account account) {
// TODO Auto-generated method stub
return null;
}
@SuppressWarnings("unchecked") @Override
public void regDao(Account account) {
// TODO Auto-generated method stub
entityManager.persist(account);
System.out.println(account.getId());
} @SuppressWarnings("unchecked")
@Override
public List<Account> getAccounts() {
// TODO Auto-generated method stub
final String hql = "select o from Account o";
List<Account> list = baseDao.getList(hql);
return list;
} @Override
public Account getAccountById(int id) {
// TODO Auto-generated method stub
Account account = entityManager.find(Account.class, id);
;
return account;
} @Override
public Account getAccountByName(String name) {
// TODO Auto-generated method stub
String hql = "select a from Account a where name =?";
Query query = null;
query = entityManager.createQuery(hql);
Account account = (Account) query.setParameter(1, name)
.getResultList().get(0);
return account;
}
}大神们 ,看看 到底怎么回事??SpringStrutsJPAJavassh