I have a simple application that should add an entity to a database. It's written in Spring Boot and I am running into an error I do not understand as the features that may cause these issues are not ones I am using.
My application adds "Initiatives" to a database. An initiative is basically an event with a User entity included in it, i.e. the person who is running it.
My Initiative entity is here:
public class Initiative {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
long id;
@NotNull
@Size(max = 250)
String title;
@NotNull
String description;
@NotNull
@Size(max = 50)
String category;
@NotNull
@Size(max = 50)
String club;
@NotNull
boolean active = true;
@NotNull
@Size(max = 25)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy", timezone = "Europe/London")
LocalDate createdDate;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "initiative_leads",
joinColumns = {@JoinColumn(name = "inititative_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")})
Set<User> initiativeLeads = new HashSet<>();
My InitiativeRepository is here:
public interface InitiativeRepository extends CrudRepository<Initiative, Long> {}
And lastly the controller. For now I have simply made a Get method which creates an Initiative, saves it using the save method from Crud repository and just returns it to the user.
public class InitiativesController {
private InitiativeRepository initiativeRepository;
public InitiativesController(InitiativeRepository initiativeRepository) {
this.initiativeRepository = initiativeRepository;
}
@GetMapping
public Initiative createInitiative() {
Initiative initiative = new Initiative();
return initiativeRepository.save(initiative);
}
However, when I run this method it returns the following errors:
"message": "Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction"
I have also tried with a POST method as follows:
public void createRepo(@Valid @RequestBody Initiative initiative)
{
initiativeRepository.save(initiative);
}
And then sending the following data via Postman:
{
"title":"hello",
"description":"hdufhs",
"category":"hi",
"club":"hi",
"active":true,
"createdDate":"2012-04-23T18:25:43.511Z",
"initiativeLeads":[ {
"id": 1,
"firstName": "John",
"lastName": "Stockton",
"email": "[email protected]",
"club": null,
"squad": null
}]
}
This also returns the same error.
There is a bigger stack trace but much longer, I can post if needed here.
I do not understand this error as reading through StackOverflow I see it has something to do with transactions. However I am not using them so do not understand the error. Any help will be appreciated.
question from:
https://stackoverflow.com/questions/66054185/spring-boot-returning-error-regarding-rollback-and-transactions-when-i-have-neit