Quantcast
Channel: GUJ - Tópicos com a tag hibernate
Viewing all articles
Browse latest Browse all 759

Hibernate + Spring MVC problema no Update

$
0
0

@castelhano escreveu:

Ola, por algum motivo o hibernate está tentando fazer um insert quando estou tentado atualizar algum registro, vi alguns exemplos e não entendi o que estou fazendo de errado.
Meu modelo:

@Entity
public class Funcionario {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String matricula;
private String nome;

A classe do DAO:

    @Repository
    @Transactional
    public class FuncionarioDAO {
	@PersistenceContext
	private EntityManager manager;
	
	public void save(Funcionario funcionario){
		manager.persist(funcionario);
	}

	public List<Funcionario> findAll() {
		return manager.createQuery("select f from Funcionario f", Funcionario.class).getResultList();
	}

	public Funcionario find(String matricula) {
		return manager.createQuery("select distinct(f) from Funcionario f where f.matricula = :matricula", Funcionario.class).setParameter("matricula", matricula).getSingleResult();
	}
}

Meu controlador

@Controller
@RequestMapping("rhm/funcionario")
public class FuncionarioController {
@Autowired
private FuncionarioDAO funcionarioDAO;
@RequestMapping("/{matricula}")`
    	public ModelAndView edit(@PathVariable("matricula") String matricula){
    	    ModelAndView modelAndView = new ModelAndView("/rhm/funcionarioUpdate");
    	    Funcionario funcionario = funcionarioDAO.find(matricula);
    	    modelAndView.addObject("funcionario", funcionario);
    	    return modelAndView;
    	}	

    	@RequestMapping(value = "/save", method = RequestMethod.POST)
    	public ModelAndView save(MultipartFile fotoPath ,@Valid Funcionario funcionario, BindingResult result, RedirectAttributes redirectAttributes) {
    		if(result.hasErrors()) {
    			redirectAttributes.addFlashAttribute("message", "Erro ao salvar funcionário");
    			if(!funcionario.getMatricula().isEmpty()) {
    				return edit(funcionario.getMatricula());
    			}
    			return add(funcionario);
    		}
    		if(!fotoPath.isEmpty()) {
    			String path = fileSaver.write("fotos", fotoPath);
    			funcionario.setFoto(path);
    		}

    		funcionarioDAO.save(funcionario);
    		redirectAttributes.addFlashAttribute("message", "Funcionário cadastrado com Sucesso");
    		return new ModelAndView("redirect:/rhm/funcionario/all");
    	}

Meu form está assim:

<form action="/dome/rhm/funcionario/save" method="post" enctype="multipart/form-data">
Matrícula*
<form:input path="funcionario.matricula"  />
<form:hidden path="funcionario.id" />
Nome* <form:input path="funcionario.nome" />
Nascimento <input type="date" name="dtaNascimento" />
Foto <input type="file" name="fotoPath" />
</form>

Para criar novo registro vai normal, quando tento atualizar um registro:
GRAVE: Servlet.service() for servlet [dispatcher] in context with path [/dome] threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: br.com.dome.model.Funcionario] with root cause

Mensagens: 1

Participantes: 1

Ler tópico completo


Viewing all articles
Browse latest Browse all 759

Trending Articles


Girasoles para colorear


mayabang Quotes, Torpe Quotes, tanga Quotes


Tagalog Quotes About Crush – Tagalog Love Quotes


OFW quotes : Pinoy Tagalog Quotes


Long Distance Relationship Tagalog Love Quotes


Tagalog Quotes To Move on and More Love Love Love Quotes


5 Tagalog Relationship Rules


Best Crush Tagalog Quotes And Sayings 2017


Re:Mutton Pies (lleechef)


FORECLOSURE OF REAL ESTATE MORTGAGE


Sapos para colorear


tagalog love Quotes – Tiwala Quotes


Break up Quotes Tagalog Love Quote – Broken Hearted Quotes Tagalog


Patama Quotes : Tagalog Inspirational Quotes


Pamatay na Banat and Mga Patama Love Quotes


Tagalog Long Distance Relationship Love Quotes


BARKADA TAGALOG QUOTES


“BAHAY KUBO HUGOT”


Vimeo 10.7.0 by Vimeo.com, Inc.


Vimeo 10.7.1 by Vimeo.com, Inc.



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>