Guest User

Untitled

a guest
Sep 2nd, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. <persistence-unit name="testePU" transaction-type="RESOURCE_LOCAL">
  2. </persistence-unit>
  3. <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
  4.  
  5. <properties>
  6. <property name="hibernate.archive.autodetection" value="class" />
  7.  
  8. <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/loja"/>
  9. <property name="hibernate.connection.user" value="root"/>
  10. <property name="hibernate.connection.password" value="lu121190"/>
  11. <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
  12.  
  13. <!-- validate | update | create | create-drop -->
  14. <property name="hibernate.hbm2ddl.auto" value="update"/>
  15. <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLSInnoDBDialect"/>
  16. </properties>
  17.  
  18. EntityManagerFactory emf;
  19. EntityManager em;
  20.  
  21. public RepositorioCliente() {
  22. emf = Persistence.createEntityManagerFactory("testePU");
  23. em = emf.createEntityManager();
  24. }
  25.  
  26. public void salvar(Cliente c) {
  27. em.getTransaction().begin();
  28.  
  29. em.merge(c);
  30. em.getTransaction().commit();
  31. emf.close();
  32. }
  33.  
  34. public void remover(Cliente c) {
  35. em.getTransaction().begin();
  36.  
  37. em.remove(c);
  38. em.getTransaction().commit();
  39. emf.close();
  40. }
  41.  
  42. @SuppressWarnings("unchecked")
  43. public List<Cliente> listarTodos(){
  44. em.getTransaction().begin();
  45.  
  46. Query consulta = em.createQuery("select cliente from Cliente cliente");
  47. List<Cliente> clientes = consulta.getResultList();
  48. em.getTransaction().commit();
  49. emf.close();
  50. return clientes;
  51. }
  52.  
  53. @Column(name= "codigo", nullable= false, unique= true, updatable= false)
  54. @Id
  55. @GeneratedValue(strategy=GenerationType.IDENTITY)
  56. private Long codigo;
  57. @Column
  58. private String nome;
  59. @Column
  60. private Integer idade;
  61. @Column
  62. private char sexo;
  63. @Column
  64. private String profissao;
  65. public Long getCodigo() {
  66. return codigo;
  67. }
  68. public void setCodigo(Long codigo) {
  69. this.codigo = codigo;
  70. }
  71. public String getNome() {
  72. return nome;
  73. }
  74. public void setNome(String nome) {
  75. this.nome = nome;
  76. }
  77. public Integer getIdade() {
  78. return idade;
  79. }
  80. public void setIdade(Integer idade) {
  81. this.idade = idade;
  82. }
  83. public char getSexo() {
  84. return sexo;
  85. }
  86. public void setSexo(char sexo) {
  87. this.sexo = sexo;
  88. }
  89. public String getProfissao() {
  90. return profissao;
  91. }
  92. public void setProfissao(String profissao) {
  93. this.profissao = profissao;
  94. }
  95.  
  96. public static void main(String[] args) {
  97.  
  98. RepositorioCliente repositorioCliente = new RepositorioCliente();
  99.  
  100. Cliente cliente = new Cliente();
  101. cliente.setNome("João da Silva");
  102. cliente.setIdade(30);
  103. cliente.setSexo('M');
  104. cliente.setProfissao("Engenheiro");
  105.  
  106. repositorioCliente.salvar(cliente);
  107. }
Add Comment
Please, Sign In to add comment