Spring Transactional无效

时间:2014-09-30 12:35:16

标签: spring hibernate dao transactional

我根据以下使用Spring 4.1和Hibernate 4.3.6的示例设置了一个Spring / Hibernate后端:

http://www.sivalabs.in/2011/02/springhibernate-application-with-zero.html

到目前为止我基本上已经完成了所有工作,但是有一个小配置问题我无法克服:如果我尝试保存实体,我会得到以下StackTrace:

org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
at org.springframework.orm.hibernate4.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1135)
at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:620)
at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:617)
at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:340)
at org.springframework.orm.hibernate4.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:308)
at org.springframework.orm.hibernate4.HibernateTemplate.save(HibernateTemplate.java:617)
at com.test.database.dao.UserSecurityQuestionDAOImpl.save(UserSecurityQuestionDAOImpl.java:40)
at com.test.database.service.UserSecurityQuestionService.save(UserSecurityQuestionService.java:20)
at com.test.database.UserSecurityQuestionTest.basicCreationAndRetrievalTest(UserSecurityQuestionTest.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:606)
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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
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.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
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)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我的事务指令放在Service Bean中,如下所示:

@Service
@Transactional
public class UserSecurityQuestionService implements UserSecurityQuestionDAO{

    @Autowired
    private UserSecurityQuestionDAO userSecurityQuerstionDAO;

    @Override
    @Transactional(readOnly=false)
    public void save(UserSecurityQuestion userSecurityQuestion) {
        userSecurityQuerstionDAO.save(userSecurityQuestion);        
    }

这是我的保存方法的DAO实现:

@Repository
public class UserSecurityQuestionDAOImpl implements UserSecurityQuestionDAO {

    @Autowired
    private HibernateTransactionManager transactionManager; 

    public void save(UserSecurityQuestion userSecurityQuestion) {
        Session session = transactionManager.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        session.persist(userSecurityQuestion);
        tx.commit();
        session.close();
    }

从数据库中选择工作正常,但拒绝插入/更新抛出错误消息。我错过了什么?

更新

UserSecurityQuestionDAOImpl:

public class UserSecurityQuestionDAOImpl implements UserSecurityQuestionDAO{

    @Autowired
    private SessionFactory sessionFactory;

    public void save(UserSecurityQuestion userSecurityQuestion) {
        this.sessionFactory.getCurrentSession().save(userSecurityQuestion);
    }

UserSecurityQuestionDAO / UserSecurityQuestionService - 接口:

public interface UserSecurityQuestionService {
    public void save(UserSecurityQuestion userSecurityQuestion);    
    public UserSecurityQuestion findById(Short id); 
    public List<UserSecurityQuestion> findAll();    
    public void delete(UserSecurityQuestion userSecurityQuestion);  
    public void deleteById(Short... id);    
}

服务实施:

@Service
public class UserSecurityQuestionServiceImpl implements UserSecurityQuestionService {

    @Autowired
    private UserSecurityQuestionDAO userSecurityQuestionDAO;    

    @Transactional
    public void save(UserSecurityQuestion userSecurityQuestion) {
        userSecurityQuestionDAO.save(userSecurityQuestion);     
    }

用于测试的单元测试代码:

public class UserSecurityQuestionTest {

    private AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();      
    private UserSecurityQuestionService usqService;

    @Before
    public void setUp(){
        //Setup App Context
        ctx.scan("com.test.database");
        ctx.refresh();

        //Initilize Service Bean
        usqService = ctx.getBean("userSecurityQuestionService",UserSecurityQuestionServiceImpl.class);
    }

新问题:如果我将@Autowired添加到private UserSecurityQuestionService usqService;并省略Setup-Part,我将获得Null指针异常。如果我保留代码,我会得到org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userSecurityQuestionService' is defined

1 个答案:

答案 0 :(得分:1)

我可以看到你的服务类和你的dao实现了相同的接口,并且你通过它的类自动装备服务中的dao。

我认为您的服务也由其控制器中的类自动装配。最好为服务和dao提供单独的接口,以允许使用接口按类型进行自动装配。这样,Spring将使用JDK代理(默认)来实现服务bean周围的事务代理。

替代方法是使用<tx:annotation-driven proxy-target-class="true/>&#34;,但是您需要在路径中使用CGLIB。