Spring boot + Hibernate + JPA No transactional EntityManager available

前端 未结 3 1943
再見小時候
再見小時候 2021-02-07 08:43

I am using spring boot 1.2.3.RELEASE version with JPA over hibernate. I am experiencing following exception

org.springframework.dao.InvalidDataAccessApiUsageExce         


        
3条回答
  •  不思量自难忘°
    2021-02-07 09:08

    Ok I found out a way of making it works.

    Just put a @Transactional annotation (org.springframework.transaction.annotation.Transactional) in your deleteOrder method at OrderService.

    @Transactional
    public void deleteOrder(long customerId){
        repo.deleteByCustomerId(customerId);
    }
    

    I really don't know why the second works. I guessing that since it is an direct method from the CrudRepository interface someway it knows how to execute it atomically.

    The former one is a call to the deleteByCustomerId. This call will be processed to find out the customer with the specified id and then deletes it. For some reason it makes the use of an explicit transaction.

    Again it is just a guess. I'll try to contact some spring developers and maybe open a issue to verify this behaviour.

    Hope it helps!

    Reference: http://spring.io/guides/gs/managing-transactions/

提交回复
热议问题