使用@OneToMany和@ManyToOne批注将更新一个实体表,而不更新多个表

时间:2020-07-18 15:08:26

标签: java spring jpa orm h2

我有一个Spring Restful API,它使用H2数据库和JPA填充一些表。我正在尝试使用字段@OneToMany设置一个实体,而另一个实体对象是@ManyToOne。在邮递员中发出POST请求时,我sout Quiz quiz是请求正文,并且字段是预期的。但是,当我登录到localhost上的数据库时,只有quiz_table被更新了正确的属性。

我执行POST请求时会在控制台中获得此输出

Hibernate: insert into quiz_table (answer, text, title, id) values (?, ?, ?, ?)

我的JSON请求正文是

{
    "title": "Tottenham Quiz",
    "text": "When did Tottenham Hotspur last win a trophy?",
    "options": ["2002", "2019"],
    "answer": 1
}

服务器响应为

{
    "id": 67,
    "title": "Tottenham Quiz",
    "text": "When did Tottenham Hotspur last win a trophy?",
    "options": [
        {
            "id": 0,
            "quiz": null,
            "option": "2002"
        },
        {
            "id": 0,
            "quiz": null,
            "option": "2019"
        }
    ]
}

据我所知"options"的值已传递给List<Option> 这是我的控制器,具有该端点的映射

 @PostMapping(path = "api/quizzes", produces = "application/json")
    public Quiz addQuiz(@Valid @RequestBody Quiz quiz) {
        quizRepository.save(quiz);
        return quiz;
    }

Quiz


@Component
@Entity(name = "quiz_table")
public class Quiz {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @NotBlank(message = "Title must not be blank")
    private String title;

    @NotBlank(message = "Text must not be blank")
    private String text;

    @Size(min = 2)
//    @NotNull
    @OneToMany(mappedBy = "quiz")
    private List<Option> options;

    @JsonIgnore
    private int answer;

    public Quiz() {}

    public Quiz(String title, String text, List<Option> options, int answer) {
        this.title = title;
        this.text = text;
        this.options = options;
        this.answer = answer;
    }

Option

@Component
@Entity(name = "options_table")
public class Option {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @ManyToOne
    @JoinColumn
    private Quiz quiz;

    private String option;

    // for Jackson deserielization
    public Option() {
    }

    /*public Option(Quiz quiz, String option) {
        this.quiz = quiz;
        this.option = option;

    }*/

    public Option(String option) {
        this.option = option;
    }
    // getters/setters

enter image description here

编辑:实施建议后出现异常的JSON响应

{
    "id": 1,
    "title": "Tottenham Quiz",
    "text": "When did Tottenham Hotspur last win a trophy?",
    "options": [
        {
            "id": 2,
            "quiz": {
                "id": 1,
                "title": "Tottenham Quiz",
                "text": "When did Tottenham Hotspur last win a trophy?",
                "options": [
                    {
                        "id": 2,
                        "quiz": {
                            "id": 1,
                            "title": "Tottenham Quiz",
                            "text": "When did Tottenham Hotspur last win a trophy?",
                            "options": [
                                {
                                    "id": 2,
                                    "quiz": {
                                        "id": 1,
                                        "title": "Tottenham Quiz",
                                        "text": "When did Tottenham Hotspur last win a trophy?",
                                        "options": [
                                            {
                                                "id": 2,
                                                "quiz": {
                                                    "id": 1,
                                                    "title": "Tottenham Quiz",
                                                    "text": "When did Tottenham Hotspur last win a trophy?",
                                                    "options": [
                                                        {
                                                            "id": 2,
                                                            "quiz": {
                                                                "id": 1,
                                                                "title": "Tottenham Quiz",
                                                                "text": "When did Tottenham Hotspur last win a trophy?",
                                                                "options": [
                                                                    {
                                                                        "id": 2,
                                                                        "quiz": {
                                                                            "id": 1,
                                                                            "title": "Tottenham Quiz",
                                                                            "text": "When did Tottenham Hotspur last win a trophy?",
                                                                            "options": [
                                                                                {
                                                                                    "id": 2,
                                                                                    "quiz": {
                                                                                        "id": 1,
                                                                                        "title": "Tottenham Quiz",
                                                                                        "text": "When did Tottenham Hotspur last win a trophy?",
                                                                                        "options": [
                                                                                            {
                                                                                                "id": 2,
                                                                                                "quiz": {
                                                                                                    "id": 1,
                                                                                                    "title": "Tottenham Quiz",
                                                                                                    "text": "When did Tottenham Hotspur last win a trophy?",
                                                                                                    "options": [
                                                                                                        {
                                                                                                            "id": 2,
                                                                                                            "quiz": {
                                                                                                                "id": 1,
                                                                                                                "title": "Tottenham Quiz",
                                                                                                                "text": "When did Tottenham Hotspur last win a trophy?",
                                                                                                                "options": [
                                                                                                                    {
                                                                                                                        "id": 2,
                                                                                                                        "quiz": {
                                                                                                                            "id": 1,
                                                                                                                            "title": "Tottenham Quiz",
                                                                                                                            "text": "When did Tottenham Hotspur last win a trophy?",
                                                                                                                            "options": [
                                                                                                                                {
                                                                                                                                    "id": 2,
                                                                                                                                    "quiz": {
                                                                                                                                        "id": 1,
                                                                                                                                        "title": "Tottenham Quiz",
                                                                                                                                        "text": "When did Tottenham Hotspur last win a trophy?",
                                                                                                                                        "options": [
                                                                                                                                            {
                                                                                                                                                "id": 2,
                                                                                                                                                "quiz": {
                                                                                                                                                    "id": 1,
                                                                                                                                                    "title": "Tottenham Quiz",
                                                                                                                                                    "text": "When did Tottenham Hotspur last win a trophy?",
                                                                                                                                                    "options": [
                                                                                                                                                        {
                                                                                                                                                            "id": 2,
                                                                                                                                                            "quiz": {
                                                                                                                                                                "id": 1,
                                                                                                                                                                "title": "Tottenham Quiz",
                                                                                                                                                                "text": "When did Tottenham Hotspur last win 
// ... continues for 4000 lines!

1 个答案:

答案 0 :(得分:3)

  • 使用JPA / Hibernate,您需要显式管理双向关系
    @PostMapping(path = "api/quizzes", produces = "application/json")
   public Quiz addQuiz(@Valid @RequestBody Quiz quiz) {
       quiz.getOptions().forEach(option -> option.setQuiz(quiz));
       quizRepository.save(quiz);
       return quiz;
   }
  • 而且,您还必须告诉休眠模式,它何时管理Quiz,也要级联Option
    @Size(min = 2)
    @OneToMany(mappedBy = "quiz", cascade = CascadeType.ALL)
    private List<Option> options;