Problems with relationship @ManyToMany

1

Good afternoon, guys, I'm having trouble with a @ManyToMany relationship. I have the Equipamento class and I have the Fornecedor class, where I can have a vendor that has multiple equipment and where I can also have equipment that can have multiple vendors.

The problem is when I will link my vendor to the equipment the relationship in the bank is not being persisted. Here is the code for 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) { //...
}

ProviderController

@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;
    }

}

EquipmentController

@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;
    }
}
    
asked by anonymous 23.03.2018 / 21:49

2 answers

0

Try adding the cascade in the relationship. Ex.:

@ManyToMany (cascade = {CascadeType.PERSIST, CascadeType.MERGE})

    
25.03.2018 / 00:24
0

Thanks a lot for the help, I was actually wrong in logic! the save was as follows in the ProviderController class

public class ProviderController {

@Autowired
private FornecedorDao fornecedorDao;

@Autowired
private EquipamentoDao equipamentoDao;

@RequestMapping(value = "/form/{equipamentoId}")
public ModelAndView form(@PathVariable("equipamentoId") Long equipamentoId, Fornecedor fornecedor) {
    ModelAndView mv = new ModelAndView("fornecedores/form");
    mv.addObject("equipamentoId", equipamentoId);
    return mv;
}

@RequestMapping(value = "/salvar/{equipamentoId}", method = RequestMethod.POST)
public ModelAndView salvar(@Valid Fornecedor fornecedor, BindingResult bindingResult, @PathVariable("equipamentoId") Long equipamentoId) {

    if(bindingResult.hasErrors()) {
        return form(equipamentoId, fornecedor);
    }

    Equipamento equipamento = equipamentoDao.find(equipamentoId);
    equipamento.getFornecedores().add(fornecedor);
    equipamentoDao.salvar(equipamento);
    fornecedorDao.salvar(fornecedor);
    return new ModelAndView("redirect:/equipamento");
}
    
28.03.2018 / 22:39