Spring boot + Hibernate + JPA No transactional EntityManager available

前端 未结 3 1937
再見小時候
再見小時候 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:04

    First, I make a quote of the Spring-Data JPA Documentation to justify why the delete method works in your case (I mean the option 2).

    CRUD methods on repository instances are transactional by default. For reading operations the transaction configuration readOnly flag is set to true, all others are configured with a plain @Transactional so that default transaction configuration applies. For details see JavaDoc of CrudRepository

    The delete method is actually a method of the CrudRepository. Your repository extends JpaRepository which extends CrudRespository, so it belongs to CrudRepository interface and according the quote above is transactional.

    If you read the section Transactional Query Method you will see that is the same that the option 4 and you will know how to apply a custom transactional behavior for all methods of your repository. Also, the Example 61 of the documentation shows the same scenario that the option 3.

    Now keep in mind that you aren't working with JDBC logic, in which case the database take care about the transactions, but within a ORM-based framework. ORM frameworks require a transaction in order to trigger the synchronization between the object cache and the database. So you must be aware and provide a transaction context for methods that do ORM logic like deleteByCustomerId.

    By default @Transactional (I mean without any parameter) set propagation mode to REQUIREDand readOnly flag to false. When you invoke a method annotated within, a transaction is intialized if no-one exists. This is the reason of why the workaround of @LucasSaldanha (the same as example Using a facade to define transactions for multiple repository calls) and the option 4 works. Other wise, without a transaction, you fall in the thrown exception of the option 1.

提交回复
热议问题