Java 使用spring boot和spring data jpa时OneToMany关系的wiered行为
我正在用数据JPA在Spring Boot中开发简单的Crud应用程序,我的目标很简单,因为我有两个实体:Java 使用spring boot和spring data jpa时OneToMany关系的wiered行为,java,spring-boot,spring-data-jpa,hibernate-onetomany,Java,Spring Boot,Spring Data Jpa,Hibernate Onetomany,我正在用数据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 In
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;
}
在我的控制器中,我想将Foo
和条列表保存为:
@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;
}
}
@控制器
@请求映射(value=“/foo”)
公共类FooController{
私人最终餐饮服务;
私人最终酒吧服务;
公共FooController(FooService FooService、BarService BarService){
this.fooService=fooService;
this.barService=barService;
}
@GetMapping(value=“/{id}/add_bar”)
公共字符串addBar(@PathVariable(“id”)整数id,模型){
model.addAttribute(“foo”,fooService.findById(id));
addAttribute(“bar”,新的bar());
返回“添加工具栏”;
}
@PostMapping(value=“/{id}/add_-bar”)
公共字符串存储栏(
@PathVariable(“id”)整数id,
@模型属性(“条”)条,
BindingResult结果
){
if(result.hasErrors()){
返回“添加工具栏”;
}
//通过添加新栏并保存更新foo
Foo-Foo=getfoo和ddbar(id,bar);
fooService.save(foo);
//保存栏
barService.save(bar);
返回“重定向:/foo/”+foo.getFooId();
}
//通过添加新栏并保存更新foo
私有Foo getFooAndAddBar(整数id,Bar){
Foo-Foo=fooService.findById(id);
ArrayList barList=新的ArrayList();
添加(条);
foo.setBarList(酒吧列表);
返回foo;
}
}
第一个条由foo id保存和获取,但当我想要添加另一个条时,它只更新第一个条,而不是在DB中插入新的条记录。@OneToMany协会缺少什么东西吗?或者程序的其他部分缺少了什么?求你了 每次调用函数时,您都在设置条形码列表。您已经编写了foo.setBarList(barList)代码>。每次此操作都会覆盖以前的条形图列表,然后将其保存,从而覆盖以前的值。不要这样做,试试这个foo.getBarList().add(Bar)
。它将获取以前的酒吧列表,然后将新酒吧添加到列表中。在这之后,只需保存实体事实上,这就是我一直缺少的东西!旁注:使用三层架构。这将是一个很好的编码实践。阅读关于MVC
你能简要描述一下三层体系结构吗?因为我是web开发领域的新手。这里有一个很好的视频,介绍了spring MVC的基本概念,你可以在youtube上找到更多信息
@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;
}
}