在ConstraintValidator isValid()方法中使用@Service之后的RollbackException

时间:2015-07-12 19:11:24

标签: java spring exception constraints transactional

我正在创建自定义验证注释,因此我需要创建自己的ConstraintValidator实现类。

public class SomethingValidator implements ConstraintValidator<Annotation, String>{
    @Autowired
    private SomeService someService;

    public boolean isValid(...){
        //here I need to use the service
        someService.someMethod();
        return true;
    }
}

在这个类中,在isValid方法中我需要使用注释为@Service

的SomeService类
@Service
public class SomeService {
    public Something someMethod(){
        // stuff
    }
}

我的申请流程是这样的:

  1. 用户进入注册页面,填写表格并点击注册
  2. 在@Controller类中,我检查bindingResult.hasErrors()
  3. 如果错误存在:返回注册页面并显示错误消息
  4. 如果错误不存在:在@Controller类中调用autowired someService实例并保存实体。
  5. 问题是,如果在我的ConstrainValidation类中,在isValid方法中我使用someService然后我收到RollbackException,即使返回始终为true(在上面的代码中静态添加)

    最后: 我无法在isValid方法中使用SomeService,因为它会导致RollbackException (下面的stacktrace) 一切似乎都正常,但使用SomeService。

    我知道问题可能是交易,也许我管理EntityManager错了?或者我应该在服务中添加@Transactional方法的任何参数吗?

    我正在使用Spring Data JPA和JpaRepository(所以我没有获得@PersistenceContext EntityManager entityManager)

    我将非常感谢任何帮助..

    Stacktraces :(我会把它全部粘贴到pastebin中)http://pastebin.com/FTjD5gRR

0 个答案:

没有答案