Relationship problems @Manytomany

Asked

Viewed 61 times

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?

  • It’s just not persisting.

  • 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 method salvarDaos is doing exactly?

  • I answered below, I had error in the logic of the Vendorcontroller.

2 answers

0

Thank you so much for the help, actually it was wrong in the logic! the save was as follows in the class Vendorcontroller

public class Supplier controller {

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

0

Try adding Calendar in the relationship. Ex.:

@Manytomany(Scade={Cascadetype.PERSIST, Cascadetype.MERGE})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.