Guest User

Untitled

a guest
Nov 23rd, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. @Entity
  2. @Table(name = "produto")
  3. public class Produto implements Serializable {
  4.  
  5. private static final long serialVersionUID = 1L;
  6. private Long id;
  7. private Cliente cliente;
  8. private String nome;
  9. private BigDecimal valor;
  10. //getter e setter com anotacoes
  11. }
  12.  
  13. @Entity
  14. @Table(name = "cliente")
  15. public class Cliente implements Serializable {
  16.  
  17. private static final long serialVersionUID = -195972743343153998L;
  18.  
  19. private Long id;
  20.  
  21. private String nome;
  22.  
  23. private String endereco;
  24. //getter e setter com anotacoes
  25. }
  26.  
  27. @ManagedBean
  28. @ViewScoped
  29. public class CadastroProdutosBean implements Serializable {
  30.  
  31. private static final long serialVersionUID = 1L;
  32. private Produto produto = new Produto();
  33. private Cliente cliente = new Cliente();
  34. private List<Cliente> clientes;
  35. //metodo init
  36.  
  37. //nesse metodo recupero uma lista de cliente pra popular uma combo
  38. public void prepararCadastro() {
  39.  
  40. EntityManager manager = JpaUtil.getEntityManager();
  41.  
  42. try {
  43. ClientesRepository clientesRepository = new ClientesRepository(manager);
  44. this.clientes = clientesRepository.listarTodos();
  45.  
  46. } finally {
  47. manager.close();
  48. }
  49. }
  50.  
  51. //aqui de fato faço a persistencia
  52. public void salvar() {
  53.  
  54. EntityManager manager = JpaUtil.getEntityManager();
  55. EntityTransaction transaction = manager.getTransaction();
  56.  
  57. FacesContext context = FacesContext.getCurrentInstance();
  58.  
  59. try {
  60.  
  61. transaction.begin();
  62.  
  63. CadastroProdutos cadastroProdutos = new CadastroProdutos(new ProdutosRepository(manager));
  64. cadastroProdutos.salvar(this.produto);
  65.  
  66. CadastroClientes cadastroClientes = new CadastroClientes(new ClientesRepository(manager));
  67. //aqui cai na exception
  68. cadastroClientes.salvar(this.cliente);
  69.  
  70. this.produto = new Produto();
  71. this.cliente = new Cliente();
  72.  
  73. context.addMessage(null, new FacesMessage("Venda cadastrada com sucesso."));
  74.  
  75. transaction.commit();
  76.  
  77. } catch (Exception e) {
  78.  
  79. transaction.rollback();
  80.  
  81. FacesMessage mensagem = new FacesMessage(e.getMessage());
  82. mensagem.setSeverity(FacesMessage.SEVERITY_ERROR);
  83.  
  84. context.addMessage(null, mensagem);
  85. } finally {
  86. manager.close();
  87. }
  88. }
  89.  
  90. org.hibernate.PropertyValueException: not-null property references a null or transient value : com.br.modelos.Cliente.nome
Add Comment
Please, Sign In to add comment