1
Good afternoon Personal I’m having trouble in a relationship @ManyToMany
. I have the class Equipamento
and have the class Fornecedor
, where I can have a supplier who has various equipment and where I can also have equipment that can have several suppliers.
The problem is when I will link my supplier to the equipment the relationship in the bank is not being persisted. Follow the code of my classes (getters and setters omitted).
Equipment
@Entity
@Table(name="equipamento")
public class Equipamento {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
@NotBlank
private String nome;
@Enumerated(EnumType.STRING)
private Categoria categoria;
@NotBlank
private String descricao;
@Min(1)
private Long quantidade;
@ManyToMany
@JoinTable(name="fornecedor_equipamento",
joinColumns= {@JoinColumn(name="fornecedor_id")},
inverseJoinColumns= {@JoinColumn(name="equipamento_id")})
private List<Fornecedor> fornecedores = new ArrayList<>();
public Long getId() {
return id;
}
// ....
}
Supplier
@Entity
@Table(name="fornecedor")
public class Fornecedor {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String nome;
private String telefoneComercial;
private String telefoneCelular;
private String email;
private String cnpj;
@ManyToMany(mappedBy = "fornecedores")
private List<Equipamento> equipamentos = new ArrayList<>();
public Fornecedor() {
}
public Fornecedor(Equipamento equipamento) { //...
}
Vendorcontroller
@Controller
@Transactional
@RequestMapping(value = "/fornecedor")
public class FornecedorController {
@Autowired
private FornecedorDao fornecedorDao;
@Autowired
private EquipamentoDao equipamentoDao;
@RequestMapping(value="/form/{equipamentoId}", method=RequestMethod.GET)
public ModelAndView form(@PathVariable ("equipamentoId") Long equipamentoId, Fornecedor fornecedor) {
ModelAndView mv = new ModelAndView("fornecedores/form");
mv.addObject("idEquipamento",equipamentoId);
return mv;
}
@RequestMapping(value="/form/{equipamentoId}/salvar",method=RequestMethod.POST)
public ModelAndView salvar(@PathVariable ("equipamentoId") Long equipamentoId, Fornecedor fornecedor) {
fornecedor.getEquipamentos().add(equipamentoDao.find(equipamentoId));
fornecedorDao.salvar(fornecedor);
return new ModelAndView("redirect:/fornecedor");
}
@RequestMapping(method=RequestMethod.GET)
public ModelAndView listar() {
ModelAndView mv = new ModelAndView("fornecedores/lista");
mv.addObject("fornecedores", fornecedorDao.getLista());
return mv;
}
}
Controller equipment
@Controller
@Transactional
@RequestMapping(value = "/equipamento")
public class EquipamentoController {
@Autowired
private EquipamentoDao equipamentoDao;
@RequestMapping(value = "/form")
public ModelAndView form(Equipamento equipamento) {
ModelAndView mv = new ModelAndView("equipamentos/form");
mv.addObject("categorias", Categoria.values());
return mv;
}
@RequestMapping(method = RequestMethod.POST)
public ModelAndView salvar(@Valid Equipamento equipamento, BindingResult bindingResult,
RedirectAttributes redirectAttributes) {
if (bindingResult.hasErrors()) {
return form(equipamento);
}
equipamentoDao.salvar(equipamento);
redirectAttributes.addFlashAttribute("sucesso", "cadastro realizado com sucesso!");
return new ModelAndView("redirect:equipamento");
}
@RequestMapping(method = RequestMethod.GET)
public ModelAndView listar() {
ModelAndView mv = new ModelAndView("equipamentos/lista");
mv.addObject("equipamentos", equipamentoDao.getLista());
return mv;
}
}
Gives some error or just not being persisted the record?
– Weslley Tavares
It’s just not persisting.
– Ivo
Could you give more details about your problem? The
Equipamento
without any relationship being persisted without problems?equipamentoDao.find(equipamentoId)
is returning something non-zero in controller supplier? What methodsalvar
Daos is doing exactly?– Anthony Accioly
I answered below, I had error in the logic of the Vendorcontroller.
– Ivo