Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
112 views
in Technique[技术] by (71.8m points)

java - Spring boot returning error regarding rollback and transactions when I have neither of these being used

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...