Advertisement
Guest User

Untitled

a guest
Aug 1st, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <!DOCTYPE hibernate-configuration PUBLIC
  4. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  5. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  6.  
  7. <hibernate-configuration>
  8.  
  9. <session-factory>
  10.  
  11. <!-- Database connection settings -->
  12. <property name="connection.drive_class">com.mysql.jdbc.Driver</property>
  13. <property name="connection.url">jdbc:mysql://127.0.0.1:3306/testebanco</property>
  14. <property name="connection.username">root</property>
  15. <property name="connection.password">102030</property>
  16.  
  17. <!-- JDBC connection pool(use the built-in) -->
  18. <property name="connection.pool_size">1</property>
  19.  
  20. <!-- SQL dialect -->
  21. <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
  22.  
  23. <!-- Enable Hibernate's automatic session context management -->
  24. <property name="current_session_context_class">thread</property>
  25.  
  26. <!-- Disable the second-level cache -->
  27. <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
  28.  
  29. <!-- Echo all executed SQL to stdout -->
  30. <property name="show_sql">true</property>
  31.  
  32. <!-- Drop and re-create the database schema on startup -->
  33. <property name="hbm2ddl.auto">create</property>
  34.  
  35. <!-- Mapeamento das entidades -->
  36. <mapping class="teste.DAO.Estado" />
  37.  
  38. </session-factory>
  39.  
  40. public class HibernateUtil {
  41. private static SessionFactory sf = createSessionFactory();
  42.  
  43. public static SessionFactory getSf() {
  44. return sf;
  45. }
  46.  
  47. private static SessionFactory createSessionFactory(){
  48. try{
  49. Configuration cfg = new Configuration().configure();
  50.  
  51. ServiceRegistry registro = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
  52.  
  53. SessionFactory f = cfg.buildSessionFactory(registro);
  54.  
  55. return f;
  56. }catch(Throwable ex){
  57. System.err.println("Erro ao criar a sessão " + ex);
  58. throw new ExceptionInInitializerError(ex);
  59. }
  60.  
  61. }
  62. }
  63.  
  64. public class HibernateUtilTeste {
  65.  
  66. @Test
  67. public void conectar(){
  68. Session sessao = HibernateUtil.getSf().openSession();
  69. sessao.close();
  70. HibernateUtil.getSf().close();
  71. }
  72. }
  73.  
  74. @Entity
  75. @Table
  76. public class Estado extends GenericID {
  77.  
  78. private String nome;
  79. private String sigla;
  80.  
  81. public String getNome() {
  82. return nome;
  83. }
  84. public void setNome(String nome) {
  85. this.nome = nome;
  86. }
  87. public String getSigla() {
  88. return sigla;
  89. }
  90. public void setSigla(String sigla) {
  91. this.sigla = sigla;
  92. }
  93.  
  94. @SuppressWarnings("serial")
  95. @MappedSuperclass
  96. public class GenericID implements Serializable {
  97.  
  98. @Id
  99. @GeneratedValue(strategy = GenerationType.AUTO)
  100. private Long id;
  101.  
  102. public Long getId() {
  103. return id;
  104. }
  105.  
  106. public void setId(Long id) {
  107. this.id = id;
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement