简体   繁体   English

使用 spring boot 和 spring 数据 jpa 时 OneToMany 关系的复杂行为

[英]wiered behavior of OneToMany relationship while using spring boot and spring data jpa

I am developint simple Crud Application in Spring Boot with Data JPA, my goal is simple where I have two Entities: Foo.java我正在使用数据 JPA 在 Spring Boot 中开发简单的 Crud 应用程序,我的目标很简单,我有两个实体: Foo.java

@Data // lombok annotation
@Entity(name = "foos")
public class Foo{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(unique = true, nullable = false)
    private Integer fooId;

    private String FooName;

    @NonNull
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "bar_id")
    private List<Bar> barList;

}

Bar.java

@Data
@Entity(name = "bars")
public class Bar{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", unique = true, nullable = false)
    private Integer barId;

    private String barTitle;

}

In my controller I want to save Foo with List of Bars as:在我的控制器我要救Foo与列表Bars为:

@Controller
@RequestMapping(value = "/foo")    
public class FooController {

    private final FooService fooService;
    private final BarService barService;

    public FooController(FooService fooService, BarService barService) {
        this.fooService = fooService;
        this.barService = barService;
    }

    @GetMapping(value = "/{id}/add_bar")
    public String addBar(@PathVariable("id") Integer id,  Model model){
        model.addAttribute("foo", fooService.findById(id));
        model.addAttribute("bar", new Bar());
        return "add_bar";
    }

    @PostMapping(value = "/{id}/add_bar")
    public String saveBar(
            @PathVariable("id") Integer id,
            @ModelAttribute("bar") Bar bar,
            BindingResult result
    ){
        if (result.hasErrors()) {
            return "add_bar";
        }
        // update foo by adding new bar and save
        Foo foo = getFooAndAddBar(id, bar);
        fooService.save(foo);
        // save bar
        barService.save(bar);

        return "redirect:/foo/" + foo.getFooId();
    }

    // update foo by adding new bar and save
    private Foo getFooAndAddBar(Integer id, Bar bar) {
        Foo foo = fooService.findById(id);
        ArrayList<Bar> barList = new ArrayList<>();
        barList.add(bar);
        foo.setBarList(barList);
        return foo;
    }
}

First bar is saved and fetched by foo id, but when ever I want to add another bar it only updates the first bar instead of inserting a new bar record in DB.第一个 bar 由 foo id 保存和获取,但是当我想添加另一个 bar 时,它只会更新第一个 bar,而不是在 DB 中插入新的 bar 记录。 is there any thing missing for @OneToMany association? @OneToMany 关联是否缺少任何东西? or something is missing in other part of the program?或者程序的其他部分缺少什么? please.请。

you are setting your barlist every time you are calling your function.每次调用函数时,您都在设置barlist You have written foo.setBarList(barList);你已经写了foo.setBarList(barList); . . every time this will overwrite the previous barlist and then save it resulting in overwriting the previous values.每次这都会覆盖以前的栏列表,然后保存它,从而覆盖以前的值。 Instead of this try this foo.getBarList().add(Bar) .而不是这个试试这个foo.getBarList().add(Bar) It will fetch the previous list of bar and then will add new bar to the list.它将获取先前的 bar 列表,然后将新 bar 添加到列表中。 After this just save the entity在此之后只需保存实体

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM