Ola Boa tarde! Estou com um problema, criei duas classes cliente e endereço e relacionei elas com Hibernate (um-para-um), em endereço coloquei opção cascade=CascadeType.Persist . Agora estou querendo atualizar os valores sendo que me da um erro, não consigo resolver procurei em tudo, não encontrei nenhuma solução. podem me ajuda!
@RequestMapping("/form")
public ModelAndView form(){
ModelAndView modelAndView = new ModelAndView("cliente/clientes-form");
//return "cliente/clientes-form";
Endereco endereco = new Endereco();
modelAndView.addObject("endereco", endereco);
return modelAndView;
}
@RequestMapping( method=RequestMethod.POST)
public String gravar(Cliente cliente,Endereco endereco){
endereco.setCliente(cliente);
enderecoDao.gravar(endereco);
return "redirect:/clientes";
}
@RequestMapping(method=RequestMethod.GET)
public ModelAndView listar(){
List<Cliente> clientes = clienteDao.listar();
ModelAndView modelAndView = new ModelAndView("cliente/listar-clientes");
modelAndView.addObject("lista", clientes);
return modelAndView;
}
@RequestMapping("/atualiza/{id}")
public ModelAndView alteraCliente(@PathVariable("id")Integer id){
ModelAndView modelAndView = new ModelAndView("cliente/atualiza-cliente");
Cliente cliente = clienteDao.buscaId(id);
modelAndView.addObject("cliente", cliente);
Endereco endereco = enderecoDao.buscaId(cliente);
modelAndView.addObject("endereco", endereco);
return modelAndView;
}
@RequestMapping("/atualiza-cliente")
public ModelAndView atualizar(Cliente cliente,Endereco endereco){
endereco.setCliente(cliente);
enderecoDao.atualiza(endereco);
return new ModelAndView("redirect:/clientes");
}
Classe Cliente
@Entity
public class Cliente {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(length = 25, nullable = false)
private String nome;
@Column(length = 60, nullable = false)
private String sobreNome;
@Column(length = 60, nullable = false)
private String razaoSocial;
@Column(length = 60, nullable = false)
private String nomeFantasia;
@Column(length = 11 , nullable = false)
private String cpf;
@Column(length = 16 , nullable = false)
private String cnpj;
@Column(length = 30, nullable = false)
private String tipoSistema;
private int quantidade;
@Column(length = 10 , nullable = false)
private String telefone1;
@Column(length = 10 , nullable = true)
private String telefone2;
get e set...
Classe Endereço
@Entity
public class Endereco {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(length = 25, nullable = false)
private String logradouro;
@Column(length = 25, nullable = false)
private String complemento;
@Column(length = 25, nullable = false)
private String bairro;
@Column(length = 8, nullable = false)
private String cep;
@Column(length = 25, nullable =false)
private String cidade;
@OneToOne(optional=false, cascade =CascadeType.PERSIST)
private Cliente cliente;
o erro é esse.
GRAVE: Servlet.service() for servlet [dispatcher] in context with path [/adnautomacao] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : br.com.adnautomacao.modelos.Endereco.cliente -> br.com.adnautomacao.modelos.Cliente] with root cause
org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : br.com.adnautomacao.modelos.Endereco.cliente -> br.com.adnautomacao.modelos.Cliente
at org.hibernate.action.internal.UnresolvedEntityInsertActions.checkNoUnresolvedActionsAfterOperation(UnresolvedEntityInsertActions.java:137)
at org.hibernate.engine.spi.ActionQueue.checkNoUnresolvedActionsAfterOperation(ActionQueue.java:314)
at org.hibernate.internal.SessionImpl.checkNoUnresolvedActionsAfterOperation(SessionImpl.java:654)
at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:874)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:854)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:859)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:1196)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)