Search code examples
springspring-bootjpajhipsterspring-transactions

Transaction - Springboot(JHipster) - RestService: how to


I have a microservice created with JHipster(SpringBoot+JPA) exposing a rest api.

During a save operation over an Entity I need to manage the transaction because I must execute another update over the DB (using other Entities).

How can I do this?

Using the tradictional approach (JDBC) I got the connection and create a transaction over it, make all the queries and finally close the transaction(commit/rollback).

With JPA I have an Entity, but I find no way to specify to begin/end(manage) the transaction programmatically.


Solution

  • You have many alternatives, here are few ones:

    • define a service (a class annotated with @Service) and annotate with @Transactional the public method that implements your logic
    • manage transaction manually through the EntityManager injected into your service class constructor
    • create a custom Repository

    Check the Spring docs