Recently I started learning Spring and I ran into a problem. In my application there are 2 tables. Pc and PcCharact, these tables are interconnected by Id. How do I add a new PcCharact? In a regular (unrelated) table, I took the parameters from the designer and inserted them. But in this situation it is impossible to do this, because there is no column with pc_id as a field, it is created by Hibernate itself. Here is the controller code:

@Controller public class PcCharactsController { final private PcRepo pcRepo; final private PcCharRepo pcCharRepo; public PcCharactsController(PcRepo pcRepo, PcCharRepo pcCharRepo) { this.pcRepo = pcRepo; this.pcCharRepo = pcCharRepo; } //СПИСОК ХАРАКТЕРИСТИК ПО ID КОМПЬЮТЕРА @GetMapping("pc/{id}/") public String pcCharList(@PathVariable int id, Model model) throws Exception{ Pc pc = pcRepo.findById(id).orElseThrow(() -> new Exception("PostId " + id + " not found")); List<PcChars> pcChars = pc.getChars(); model.addAttribute("model", pc.getName()); model.addAttribute("pcChars", pcChars); return "charList"; } //ДОБАВЛЕНИЕ ХАРАКТЕРИСТИКИ @PostMapping("pc/{id}/") public String addCharact(){ return "charList"; } 

Pc Model Code:

 @Entity @Table(name = "pc") public class Pc { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; private String name; private int price; public Pc(){} public Pc(String name, int price) { this.name = name; this.price = price; } @OneToMany @JoinColumn(name = "pc_id") private List<PcChars> chars = new ArrayList<>(); public List<PcChars> getChars() { return chars; } public void setChars(List<PcChars> chars) { this.chars = chars; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } 

And the PcChars code:

 @Entity @Table(name = "pcChars") public class PcChars { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; private String name; private String value; public PcChars(){} public PcChars(String name, String value) { this.name = name; this.value = value; } @ManyToOne private Pc pc; public Pc getPc() { return pc; } public void setPc(Pc pc) { this.pc = pc; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } 

}

What do I need to write to addcharact in the controller? I tried so many options, but nothing works fine.

  • At least two more bins should appear @Repository - Sergey Mitrofanov
  • They are. I need to somehow get @ RequestParameter by pc_id or something like that. But I don’t know how to add it to the table of characteristics, because I can’t use a constructor, because the pc_id field is not defined, but simply as a property in JoinColumn - DenProg

0