Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <!-- Conexão com o banco de dados -->
  4. <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
  5. <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
  6. <property name="hibernate.connection.url" value="jdbc:mysql://localhost/loja" />
  7. <property name="hibernate.connection.user" value="root"/>
  8. <property name="hibernate.connection.password" value="root" />
  9.  
  10. <!-- <property name="hibernate.hbm2ddl.auto" value="create" /> -->
  11. <property name="hibernate.hbm2ddl.auto" value="update"/>
  12.  
  13. <!--Configuracoes de Debug-->
  14. <property name="hibernate.show_sql" value="true" />
  15. <property name="hibernate.format_sql" value="true" />
  16. <property name="use_sql_comments" value="true" />
  17.  
  18. </properties>
  19. </persistence-unit>
  20.  
  21. package com.exemplo.entidade;
  22.  
  23. @Id
  24. @GeneratedValue(strategy=GenerationType.IDENTITY)
  25. private int id;
  26.  
  27. public int getId() {
  28. return id;
  29. }
  30. public void setId(int id) {
  31. this.id = id;
  32. }
  33. public String getNome() {
  34. return nome;
  35. }
  36. public void setNome(String nome) {
  37. this.nome = nome;
  38. }
  39. public int getIdade() {
  40. return idade;
  41. }
  42. public void setIdade(int idade) {
  43. this.idade = idade;
  44. }
  45. @Column
  46. private String nome;
  47. @Column
  48. private int idade;
  49.  
  50. package com.exemplo.repositorio;
  51.  
  52. EntityManagerFactory emf;
  53. EntityManager em;
  54.  
  55. public RepositorioCliente() {
  56. emf = Persistence.createEntityManagerFactory("mohr") ;
  57. em = emf.createEntityManager();
  58. }
  59.  
  60. public void salvar(Cliente cliente){
  61. em.getTransaction().begin();
  62. em.merge(cliente);
  63. em.getTransaction().commit();
  64. emf.close();
  65.  
  66. }
  67.  
  68. public void remover (Cliente c){
  69. em.getTransaction().begin();
  70. em.remove(c);
  71. em.getTransaction().commit();
  72. emf.close();
  73.  
  74. }
  75.  
  76. public List<Cliente> listarTodos(){
  77. em.getTransaction().begin();
  78. Query consulta = em.createQuery("select cliente from Cliente cliente");
  79. @SuppressWarnings("unchecked")
  80. List<Cliente> clientes = consulta.getResultList();
  81. em.getTransaction().commit();
  82. emf.close();
  83. return clientes;
  84. }
  85.  
  86. package com.exemplo.testes;
  87.  
  88. public static void main(String[] args) {
  89. RepositorioCliente repositorioCliente = new RepositorioCliente();
  90. Cliente cliente = new Cliente();
  91. cliente.setNome("Styles Stilinski");
  92. cliente.setIdade(18);
  93.  
  94. repositorioCliente.salvar(cliente);
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement