Advertisement
Guest User

Untitled

a guest
Feb 17th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. package testehibernate;
  2.  
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  6. import org.hibernate.cfg.Configuration;
  7.  
  8. public class TesteHibernate
  9. {
  10. public static void main(String[] args)
  11. {
  12. Configuration configuration = new Configuration().configure();
  13.  
  14. StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
  15.  
  16. SessionFactory factory = configuration.buildSessionFactory(builder.build());
  17. Session session = factory.openSession();
  18.  
  19. Teste teste = new Teste();
  20. teste.setNome("Alexandre");
  21. teste.setEmail("alexandre@email.com");
  22.  
  23. session.beginTransaction();
  24. session.save(teste);
  25. session.getTransaction().commit();
  26.  
  27. session.close();
  28. }
  29.  
  30. }
  31.  
  32. package testehibernate;
  33.  
  34. import javax.persistence.Entity;
  35. import javax.persistence.Id;
  36.  
  37. @Entity
  38. public class Teste
  39. {
  40. @Id
  41. private int id;
  42. private String nome;
  43. private String email;
  44.  
  45. public int getId()
  46. {
  47. return id;
  48. }
  49.  
  50. public void setId(int id)
  51. {
  52. this.id = id;
  53. }
  54.  
  55. public String getNome()
  56. {
  57. return nome;
  58. }
  59.  
  60. public void setNome(String nome)
  61. {
  62. this.nome = nome;
  63. }
  64.  
  65. public String getEmail()
  66. {
  67. return email;
  68. }
  69.  
  70. public void setEmail(String email)
  71. {
  72. this.email = email;
  73. }
  74.  
  75. }
  76.  
  77. CREATE TABLE `teste` (
  78. `id` int(11) NOT NULL,
  79. `nome` varchar(255) NOT NULL,
  80. `email` varchar(255) NOT NULL
  81. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  82.  
  83. ALTER TABLE `teste`
  84. ADD PRIMARY KEY (`id`);
  85. TO_INCREMENT for table `teste`
  86.  
  87. ALTER TABLE `teste`
  88. MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=14;
  89.  
  90. <?xml version='1.0' encoding='utf-8'?>
  91. <!DOCTYPE hibernate-configuration PUBLIC
  92. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  93. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  94.  
  95. <hibernate-configuration>
  96.  
  97. <session-factory>
  98. <!-- Database connection settings -->
  99. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  100. <property name="connection.url">jdbc:mysql://localhost:3306/testdb</property>
  101. <property name="connection.username">root</property>
  102. <property name="connection.password"></property>
  103.  
  104. <!-- JDBC connection pool (use the built-in) -->
  105. <property name="connection.pool_size">1</property>
  106.  
  107. <!-- SQL dialect -->
  108. <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  109.  
  110. <!-- Echo all executed SQL to stdout -->
  111. <property name="show_sql">true</property>
  112.  
  113. <!-- Mapping files -->
  114. <mapping class="testehibernate.Teste"/>
  115. </session-factory>
  116.  
  117. </hibernate-configuration>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement