The Service
@Service
@Transactional
public class PaiementServiceImpl {
@Autowired
PaiementRepository paiement_repo;
@Transactional(rollbackFor = { ResponseStatusException.class })
public ResponseEntity<Paiement> store(Paiement paiement ) throws ResponseStatusException {
Paiement newp = paiement_repo.save(paiement);
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "XXXXXXXXXXXXXXXXXXX");
}
}
The Controller
@RestController
@RequestMapping("/paiements")
@CrossOrigin("*")
@Transactional
@Slf4j
public class PaiementController {
@Autowired
public PaiementRepository paiement_repo;
@Autowired
public PaiementServiceImpl paiementService;
@PostMapping
@Transactional(rollbackFor = { ResponseStatusException.class })
public ResponseEntity<Paiement> store(@RequestBody Paiement paiement ) throws ResponseStatusException {
return paiementService.store(paiement);
}
}
Entity:
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Paiement {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String reference;
@Temporal(TemporalType.DATE)
private Date date_paiement;
private Double montant;
@Column(length = 10)
private String etat; //valide , en attente , annulé
@Column(nullable = true)
private String chemin;
@ManyToOne
private Utilisateur utilisateur;
@ManyToOne
private Exercice exercice;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@OneToMany(mappedBy = "paiement")
private List<Consommation> consommations;
}
Pom:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.auth0/java-jwt -->
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.19.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
application.properties:
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.DataSource.url= jdbc:mysql://localhost:3306/db_electreau?serverTimezone=UTC&createDatabaseIfNotExist=true
spring.DataSource.username = root
spring.DataSource.password = root
spring.jpa.generate-ddl=true
spring.jpa.hibernate.dll-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
Simple test for @Transactional in spring boot , not working.
It saves the object even after the exception
This has been edited multiple times, following your comments, so it's not the original post.
Text Description Text Description Text Description Text Description Text Description Text Description Text Description Text Description Text Description Text Description