When i use DELETE in postman on adress http://localhost:8081/api/data/removedata/1
i get a message response saying:"Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead",
this is the java code
@DeleteMapping("/removedata/{id}")
public ResponseEntity deleteData(@PathVariable String id) {
long remInt = Long.parseLong(id);
Data dataRem = em.find(Data.class, remInt);
em.getTransaction().begin();
em.remove(dataRem);
em.getTransaction().commit();
return ResponseEntity.ok(new MessageResponse("Data removed"));
}
What am i doing wrong? ps im a novice still struggling with the basics.
This issue was resolved by adding @Transactional like so:
@Transactional
@DeleteMapping("/removedata/{id}")
public ResponseEntity deleteData(@PathVariable String id) {
long remInt = Long.parseLong(id);
Data dataRem = em.find(Data.class, remInt);
em.getTransaction().begin();
em.remove(dataRem);
em.getTransaction().commit();
return ResponseEntity.ok(new MessageResponse("Data removed"));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.