Problema ao tentar editar entidade usando SpringFrameWork

4 respostas
javawebjavaspring
Cassiozumbi

Estou usando o SpringBoot, quando vou editar a entidade não edita, mas pelo contrario cria uma nova.

package br.com.laboclinic.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.validation.BindingResult;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import br.com.laboclinic.model.Cliente;

import br.com.laboclinic.model.RepositoryCliente;

import br.com.laboclinic.model.TipoSexo;
@Controller

@RequestMapping("/cadastros/cliente")

public class ClienteController {
@Autowired
private RepositoryCliente repositoryCliente;

@GetMapping("/{id}")
public ModelAndView aditar(@PathVariable("id") Long id) {

	return novo(repositoryCliente.findOne(id));

}

@GetMapping
public ModelAndView listar() {
	ModelAndView modelAndView = new ModelAndView("cadastros/cliente/lista-clientes");

	modelAndView.addObject("clientes", repositoryCliente.findAll());

	return modelAndView;
}

@GetMapping("/novo")
public ModelAndView novo(Cliente cliente) {
	ModelAndView modelAndView = new ModelAndView("cadastros/cliente/cadastro-cliente");

	modelAndView.addObject(cliente);
	modelAndView.addObject("tiposSexo", TipoSexo.values());

	return modelAndView;

}

@PostMapping("/novo")
public ModelAndView salvar(@Valid Cliente cliente, BindingResult result, RedirectAttributes attributes) {

	if (result.hasErrors()) {
		return novo(cliente);

	}

	repositoryCliente.save(cliente);

	attributes.addFlashAttribute("mensagem", "Cliente salvo com sucesso!!!");

	return new ModelAndView("redirect:/cadastros/cliente/novo");
}

}

4 Respostas

emanuelbatista

You must try to get the user firtly using findOne and then edit it if exists (using save method), did you get it?

Cassiozumbi

Yeah, but it’s not editing.

emanuelbatista
@PostMapping("/novo")
public ModelAndView salvar(@Valid Cliente cliente, BindingResult result, RedirectAttributes attributes) {

	if (result.hasErrors()) {
		return novo(cliente);

	}

        Cliente clientRepository = repositoryCliente.findOne(cliente.id);

       if(clientRepository != null) {
          SpringUtils.copyProperties(cliente, clientRepository);
          repositoryCliente.save(clientRepository);
       } esle {
         repositoryCliente.save(cliente);
       }

	attributes.addFlashAttribute("mensagem", "Cliente salvo com sucesso!!!");

	return new ModelAndView("redirect:/cadastros/cliente/novo");
}

Can you try and see if it works?

Cassiozumbi

has an error in (SpringUtils) :disappointed_relieved:

Criado 29 de julho de 2018
Ultima resposta 31 de jul. de 2018
Respostas 4
Participantes 2